Using the Calendar

Hi I'm using the last version of the Sun Java Wirelless Toolkit (2.5).

I am writing an application and I need to get the system date and time.

I have tried with this:

calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT-05:00"));

calendar.setTime(new Date());

// to convert the current date to an String

int year = calendar.get(Calendar.YEAR);

int month = calendar.get(Calendar.MONTH) + 1;

int day = calendar.get(Calendar.DATE);

date = (month<10? "0": "")+ month+"/"+(day<10? "0": "") + day + "/" + (year<10? "0": "")+year;

// to convert the current time to an String

String[] AMPM = {"AM", "PM"};

int hour = calendar.get(Calendar.HOUR);

int minute = calendar.get(Calendar.MINUTE);

String am_pm = (AMPM[calendar.get(Calendar.AM_PM)]);

time = (hour + ":" + minute + am_pm);

but this code give me the time but one hour less.

Can someone send me a code that works or figure out what happen with this ?

[1024 byte] By [jonhya] at [2007-11-27 10:51:29]
# 1

The Calendar is not giving you the wrong time, you just live one our ahead, since it's summertime.

If you want to take account for this, you'll have to calculate it yourself, see here for some pointers: http://en.wikipedia.org/wiki/Daylight_saving_time#Computing

deepspacea at 2007-7-29 11:31:25 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

I had a similar problem a while ago and the way I solved it was this:

I made 4 int arrays:

int[] Mo_Beg = {0,0,0,0,0,0,0,3,3,3,3,3,3,3,3,3,3,3,3,3,3};

int[] Mo_End = {0,0,0,0,0,0,0,10,10,10,10,10,10,10,10,10,10,10,10,10,10};

int[] Day_Beg = {0,0,0,0,0,0,0,25,30,29,28,27,25,31,30,29,27,26,25,31,29};

int[] Day_End = {0,0,0,0,0,0,0,28,26,25,31,30,28,27,26,25,30,29,28,27,25 };

Those are the dates for the beggining and end of summer hours in europe from 2000 to 2020. So I get the year and get the corresponding month and day in the array. I then find out if the current date is within that period. If so, I add an hour.

It is actually pretty dumb but it works IF you don't move around or IF they don't change the dates in the following years...

I got them from this website:

http://webexhibits.org/daylightsaving/b.html

Hope this helps!!

murilo.pollaa at 2007-7-29 11:31:25 > top of Java-index,Java Mobility Forums,Java ME Technologies...