SimpleDateFormat and lenient

W

Willy Dockx

Hello,

I run the following code once with setLenient(true) and once with
setLenient(false) on a Win2003 and on a WindowsXP machine

java.text.ParsePosition pos = new java.text.ParsePosition(0);
java.text.SimpleDateFormat formatter = new
java.text.SimpleDateFormat("dd/MM/yyyy HH:mm");
formatter.setLenient(true);
Date date = formatter.parse("02/04/2004 15:10", pos);
Calendar c = Calendar.getInstance();
c.setTime(date);
int hour = c.get(Calendar.HOUR_OF_DAY);


I get the following results :

Lenient false : on Windows XP hour = 15, on Windows 2003 hour = 16
Lenient true : on Windows XP hour = 15, on Windows 2003 hour = 15

Any idea why the hour on Windows2003 with lenient false is 16?

Thanks,

Greetings,

Willy Dockx
 
P

P.Hill

Willy said:
I get the following results :

Lenient false : on Windows XP hour = 15, on Windows 2003 hour = 16
Lenient true : on Windows XP hour = 15, on Windows 2003 hour = 15

I would look into the timezones that are being used by each of the
various parts you are using.
The DateFormat has a timezone and so does the calendar.

One way to do this is to make up another date format
which includes timezone "zzz" (plus ALL other fields)
and then use it to format the date variable you have, plus
after you push the date into a calendar with:
c.setTime(date);
format that date too (it should be the same, but may be modified
by the TZ).

String allFieldsInCal = myLongFormat.format( c.getTime() );

and see if there are any differences.

Oh I remember now, what JDK are you running on each machine?
Older JDKs had older rules for defaulting timezones and
making up defaults if it couldn't figure out the OS timezone.
I'm sure this will become apparent when you show the TZ of
each time string.

-Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top