How can i get UTC DateTime from the following code, Right now with this line of code m getting an output like this Fri Dec 31 05:30:00 IST 9999. Is this output is correct. I mean to say is this time is the UTC time.Any sugeestion or help.
Code snapshot
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.set(9999, 11, 31, 0, 0, 0);
Date date = cal.getTime();
System.out.println(date);
Well, the output is correct in that it's what I'd expect for midnight UTC when you're running on a system in IST. Date.toString()
always uses your system local time zone - because it doesn't have any other information. A Calendar
knows its time zone, but a Date
doesn't. The underlying information is just "the number of milliseconds since the Unix epoch".
If you want to convert a Date
to a textual representation in a particular time zone, use SimpleDateFormat
and specify the time zone there.
See more on this question at Stackoverflow