Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Date/Calendar confusion

Reply
Thread Tools

Date/Calendar confusion

 
 
Ulrich Scholz
Guest
Posts: n/a
 
      09-06-2012
Dear all,

have a look at the function below (Java 5). The first result is 0 as expected. But why is the second one different?

Thanks, Ulrich


private static void testDate() throws ParseException
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
TimeZone timeZone = TimeZone.getTimeZone("GMT");
timeZone.setRawOffset(0); // get GMT time zone for sure
dateFormat.setTimeZone(timeZone);

Calendar calendar1 = Calendar.getInstance(timeZone, Locale.US);
Date date1 = DATE_FORMAT.parse("1970-01-01T00:00:00.000");
calendar1.setTime(date1);
System.out.println(calendar1.getTimeInMillis()); // is 0

Calendar calendar2 = Calendar.getInstance(timeZone, Locale.US);
Date date2 = DATE_FORMAT.parse("0000-00-00T00:00:00.000");
calendar2.setTime(date2);

// adjust for the epoch 01.01.1970
//
calendar2.set(Calendar.YEAR, calendar2.get(Calendar.YEAR) + 1970);
calendar2.set(Calendar.MONTH, calendar2.get(Calendar.MONTH) + 1);
calendar2.set(Calendar.DAY_OF_MONTH, calendar2.get(Calendar.DAY_OF_MONTH) + 1);

System.out.println(calendar2.getTimeInMillis()); // should be 0 but is -124335907200000
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mozilla & Firefox Confusion listsubscriber@hotmail.com Firefox 9 01-20-2005 05:09 PM
Confusion about location of Mozilla files (Mandrake Linux 10.0) Hallvard Tangeraas Firefox 0 09-14-2004 09:46 AM
Procedures in testbench confusion Peter Hermansson VHDL 2 08-25-2004 02:15 PM
confusion when resetting registers martin f. krafft VHDL 2 08-19-2004 06:29 AM
defined() confusion Chris Perl 2 12-18-2003 06:06 PM



Advertisments