Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #18569
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2012-09-06 02:29 -0700 |
| References | <3a69eb4a-f3c0-4b56-9a67-6833ccb2a1c8@googlegroups.com> |
| Message-ID | <1619c434-6b01-4c16-8cf5-de6cdf8615fd@googlegroups.com> (permalink) |
| Subject | Re: Date/Calendar confusion |
| From | nogales <nogales.manuel@gmail.com> |
Try this:
package snippet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Snippet {
public static void main(String[] args) 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 = dateFormat.parse("1970-01-01T00:00:00.000");
System.out.println(date1);
calendar1.setTime(date1);
System.out.println(calendar1.getTimeInMillis()); // is 0
System.out.println(calendar1);
Calendar calendar2 = Calendar.getInstance(timeZone, Locale.US);
Date date2 = dateFormat.parse("0001-01-01T00:00:00.000");
System.out.println(date2);
calendar2.setTime(date2);
// adjust for the epoch 01.01.1970
//
calendar2.set(Calendar.YEAR, calendar2.get(Calendar.YEAR) + 1969);
calendar2.set(Calendar.MONTH, calendar2.get(Calendar.MONTH));
calendar2.set(Calendar.DAY_OF_MONTH, calendar2.get(Calendar.DAY_OF_MONTH));
System.out.println(calendar2);
System.out.println(calendar2.getTimeInMillis()); // should be 0 but is -124335907200000
}
}
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Date/Calendar confusion Ulrich Scholz <d7@thispla.net> - 2012-09-06 01:03 -0700
Re: Date/Calendar confusion nogales <nogales.manuel@gmail.com> - 2012-09-06 02:29 -0700
Re: Date/Calendar confusion Lew <lewbloch@gmail.com> - 2012-09-06 10:21 -0700
Re: Date/Calendar confusion "John B. Matthews" <nospam@nospam.invalid> - 2012-09-06 20:53 -0400
Re: Date/Calendar confusion Lew <lewbloch@gmail.com> - 2012-09-07 11:01 -0700
Re: Date/Calendar confusion "John B. Matthews" <nospam@nospam.invalid> - 2012-09-07 21:02 -0400
Re: Date/Calendar confusion Lew <lewbloch@gmail.com> - 2012-09-07 18:44 -0700
Re: Date/Calendar confusion "John B. Matthews" <nospam@nospam.invalid> - 2012-09-08 09:12 -0400
Re: Date/Calendar confusion Lew <lewbloch@gmail.com> - 2012-09-08 16:51 -0700
csiph-web