Skip to main content

CLDC and Calendar.getInstance()

1 reply [Last post]
Anonymous

Hi,

I have been informed by some people that they are having trouble getting the
current time (they get Jan 01 00:00:00 UTC 1970) with my pMEF Windows Mobile
builds. After some investigation, I found that the problem can easily be
reproduced as follows:

import java.util.*;

public class HelloWorld {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
System.out.println(cal.getTime() + ": Hello world!");
cal.setTime(new Date(System.currentTimeMillis()));
System.out.println(cal.getTime() + ": Goodbey, cruel world!");
}
}

I ran this code with CLDC, CDC and plain Java, and here are the results:
../cldc/bin/cldc_vm -cp HelloWorld.jar HelloWorld
Thu Jan 01 00:00:00 UTC 1970: Hello world!
Thu Oct 30 09:31:40 UTC 2008: Goodbey, cruel world!

../cdc/bin/cvm -jar HelloWorld.jar
Thu Oct 30 10:31:40 GMT+01:00 2008: Hello world!
Thu Oct 30 10:31:40 GMT+01:00 2008: Goodbey, cruel world!

java -jar HelloWorld.jar
Thu Oct 30 09:31:40 GMT 2008: Hello world!
Thu Oct 30 09:31:40 GMT 2008: Goodbey, cruel world!

Notice how the time for "Hello world" with CLDC is wrong!!!

It turns out that CLDC implements Calendar.getInstance() a bit differently:
cldc/src/javaapi/cldc1.1.1/java/util/Calendar.java: getInstance()

Class clazz = Class.forName("com.sun.cldc.util.j2me.CalendarImpl");
return (Calendar)clazz.newInstance();

cldc/src/javaapi/cldc1.1.1/com/sun/cldc/util/j2me/CalendarImpl.java:
public CalendarImpl() {
super();
}

Is this behavior according to spec, or should we call something like
setTime(new Date(System.currentTimeMillis())); in the constructor of
CalendarImpl?

Best regards,
Davy

---------------------------------------------------------------------
To unsubscribe, e-mail: feature-unsubscribe@phoneme.dev.java.net
For additional commands, e-mail: feature-help@phoneme.dev.java.net

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Davy Preuveneers

On Thursday 30 October 2008 10:46:24 Davy Preuveneers wrote:
> Hi,
>
> I have been informed by some people that they are having trouble getting
> the current time (they get Jan 01 00:00:00 UTC 1970) with my pMEF Windows
> Mobile builds. After some investigation, I found that the problem can
> easily be reproduced as follows:
>
> import java.util.*;
>
> public class HelloWorld {
> public static void main(String[] args) {
> Calendar cal = Calendar.getInstance();
> System.out.println(cal.getTime() + ": Hello world!");
> cal.setTime(new Date(System.currentTimeMillis()));
> System.out.println(cal.getTime() + ": Goodbey, cruel world!");
> }
> }
>
> I ran this code with CLDC, CDC and plain Java, and here are the results:
> ../cldc/bin/cldc_vm -cp HelloWorld.jar HelloWorld
> Thu Jan 01 00:00:00 UTC 1970: Hello world!
> Thu Oct 30 09:31:40 UTC 2008: Goodbey, cruel world!
>
> ../cdc/bin/cvm -jar HelloWorld.jar
> Thu Oct 30 10:31:40 GMT+01:00 2008: Hello world!
> Thu Oct 30 10:31:40 GMT+01:00 2008: Goodbey, cruel world!
>
> java -jar HelloWorld.jar
> Thu Oct 30 09:31:40 GMT 2008: Hello world!
> Thu Oct 30 09:31:40 GMT 2008: Goodbey, cruel world!
>
> Notice how the time for "Hello world" with CLDC is wrong!!!
>
> It turns out that CLDC implements Calendar.getInstance() a bit differently:
> cldc/src/javaapi/cldc1.1.1/java/util/Calendar.java: getInstance()
>
> Class clazz = Class.forName("com.sun.cldc.util.j2me.CalendarImpl");
> return (Calendar)clazz.newInstance();
>
> cldc/src/javaapi/cldc1.1.1/com/sun/cldc/util/j2me/CalendarImpl.java:
> public CalendarImpl() {
> super();
> }
>
> Is this behavior according to spec, or should we call something like
> setTime(new Date(System.currentTimeMillis())); in the constructor of
> CalendarImpl?

The patch in attach solves the problem for me. It sets the current time when
requesting a Calendar with Calendar.getInstance(). Setting the current time
with setTime(new Date(System.currentTimeMillis())) caused an infinite loop of
Calendar instantiations, but setTimeInMillis(System.currentTimeMillis())
seems to work. The patch was tested on Linux/qte and on WinCE. Though only
cldc 1.1 was tested, I guess the patch will also work for cldc 1.0 and 1.1.1.

Kind regards,
Davy
[Calendar.diff]
[signature.asc]