GregorianCalendar in Codename One

17 views
Skip to first unread message

Andrew Cameron

unread,
Sep 17, 2020, 7:31:26 PM9/17/20
to CodenameOne Discussions
I am trying to use some existing code with Codename one.

I need to be able to duplicate the following 
private final long BASE_TIME = new GregorianCalendar(2010, 1, 1).getTimeInMillis();

How must I define BASE_TIME in a Codename One App  so that it will function the same as the code above?

Shai Almog

unread,
Sep 18, 2020, 12:09:36 AM9/18/20
to CodenameOne Discussions
You need to used Calendar.getInstance() instead of GregorianCalendar.
With Calendar you can do something like this (writing from memory so might have compilation issues):

Calendar cld = Calendar.getInstance();
cld.set(Calendar.YEAR, 2010);
cld.set(Calendar.MONTH, Calendar.JANUARY);
cld.set(Calendar.DAY_OF_MONTH, 1);
long time = cal.getTime().getTime();

Andrew Cameron

unread,
Sep 20, 2020, 9:28:13 PM9/20/20
to CodenameOne Discussions
Unfortunately the example code you gave me  gives a value that changes every time.
I have changed it to use the following as the value calculated in Java seems to be a constant and gives me the value below.
  private final long BASE_TIME = 1265004000000L;

Shai Almog

unread,
Sep 20, 2020, 10:37:15 PM9/20/20
to CodenameOne Discussions
You can also explicitly set the hour, minute, second and millisecond to get the same result. There are some new methods in DateUtil that might help.

Andrew Cameron

unread,
Sep 21, 2020, 8:37:31 PM9/21/20
to CodenameOne Discussions
I was able to get it to duplicate the behaviour by using something like
    Calendar cld = Calendar.getInstance();
    cld.set(Calendar.YEAR,2010);
    cld.set(Calendar.MONTH,1);
    cld.set(Calendar.DAY_OF_MONTH,1);
    cld.set(Calendar.HOUR_OF_DAY,0);
    cld.set(Calendar.HOUR,0);
    cld.set(Calendar.MINUTE,0);
    cld.set(Calendar.SECOND,0);
    cld.set(Calendar.MILLISECOND,0);
    cld.set(Calendar.AM_PM,0);
    final long BASE_TIME = cld.getTime().getTime();

On Thursday, September 17, 2020 at 6:31:26 PM UTC-5, Andrew Cameron wrote:
Reply all
Reply to author
Forward
0 new messages