I want to convert this GMT time stamp to GMT+13:
2011-10-06 03:35:05
I have tried about 100 different combinations of DateFormat, TimeZone, Date, GregorianCalendar etc. to try to do this VERY basic task.
This code does what I want for the CURRENT TIME:
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
formatter.setTimeZone(TimeZone.getTimeZone("GMT+13"));
String newZealandTime = formatter.format(calendar.getTime());
But what I want is to set the time rather then using the current time.
I found that anytime I try to set the time like this:
calendar.setTime(new Date(1317816735000L));
the local machine's TimeZone is used. Why is that? I know that when "new Date()" returns UTC+0 time so why when you set the Time in milliseconds does it no longer assume the time is in UTC?
Is possible to:
- Set the time on an object (Calendar/Date/TimeStamp)
- (Possibly) Set the TimeZone of the initial time stamp (calendar.setTimeZone(...))
- Format the time stamp with a new TimeZone (formatter.setTimeZone(...)))
- Return a string with new time zone time. (formatter.format(calendar.getTime()))
Thanks in advance for any help :D
I have try this code
and getting this result
tl;dr
If given input of
1317816735000L
…If given input of
2011-10-06 03:35:05
…java.time
The Question and most Answers use outdated legacy date-time classes from the earliest versions of Java. These old classes have proven to be troublesome and confusing. Avoid them. Instead use the java.time classes.
ISO 8601
Your input string is nearly in standard ISO 8601 format. Just replace the SPACE in the middle with a
T
.The java.time classes use these standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.
LocalDateTime
Now parse as a
LocalDateTime
because the input lacks any information about offset-from-UTC or time zone. ALocalDateTime
has no concept of offset nor time zone, so it does not represent an actual moment on the timeline.ZoneOffset
You seem to be saying that from the business context you know the intention of this string is to represent a moment that is 13 hours ahead of UTC. So we instantiate a
ZoneOffset
.OffsetDateTime
Apply it to get an
OffsetDateTime
object. This becomes an actual moment on the timeline.ZoneId
But then you mention New Zealand. So you had a specific time zone in mind. A time zone is an offset-from-UTC plus a set of rules for handling anomalies such as Daylight Saving Time (DST). So we can specify a
ZoneId
to aZonedDateTime
rather than a mere offset.Specify a proper time zone name. Never use the 3-4 letter abbreviation such as
EST
orIST
as they are not true time zones, not standardized, and not even unique(!). For example,Pacific/Auckland
.ZonedDateTime
Apply the
ZoneId
.You can easily adjust into another zone for the very same moment on the timeline.
Count from epoch
I strongly recommend against handling date-time values as a count from epoch, such as milliseconds from the start of 1970 UTC. But if you must, create a
Instant
from such a number.Then assign a time zone as seen above, if desired, to move away from UTC.
Your value of
1_317_816_735_000L
is:2011-10-05T12:12:15Z
(Wed, 05 Oct 2011 12:12:15 GMT)2011-10-06T01:12:15+13:00[Pacific/Auckland]
(Thursday October 06, 2011 01:12:15 in Auckland New Zealand).Generate strings
To generate a string in standard ISO 8601 format, simply call
toString
. Note thatZonedDateTime
wisely extends the standard format by appending the name of the time zone in square brackets.For other formats, search Stack Overflow for
DateTimeFormatter
class. Already covered many times.Specify a
FormatStyle
and aLocale
.About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes.Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.Joda-Time
The java.util.Date/Calendar classes are a mess and should be avoided.
Update: The Joda-Time project is in maintenance mode. The team advises migration to the java.time classes.
Here's your answer using the Joda-Time 2.3 library. Very easy.
As noted in the example code, I suggest you use named time zones wherever possible so that your programming can handle Daylight Saving Time (DST) and other anomalies.
If you had placed a
T
in the middle of your string instead of a space, you could skip the first two lines of code, dealing with a formatter to parse the string. The DateTime constructor can take a string in ISO 8601 format.Dump those values…
When run…
A quick way is :
Output
NEW DATE: 2015-07-02T16:51:46
For me, the simplest way to do that is: