Reputation: 1336
I am using Java 8 and attempting to convert a timestamp from one time zone to another. The time stamp will be in the user's time zone e.g.
// 2016-04-11T12:00:00Z
long sourceTime = 1460372400000L;
timezoneConverter(sourceTime, "America/New_York");
However, when using Joda time, or Java 8 LocalDateTime/Instant, when I pass the timestamp it is adjusted to the user's time zone - I don't need this to happen, as it is already in the user's timezone (all timestamps will be sent in the user's local time).
ZonedDateTime zdt =
ZonedDateTime.ofInstant(Instant.ofEpochMilli(1460372400000L),
ZoneId.of("America/New_York"))
results in
2016-04-11T07:00-04:00[America/New_York]
instead of
2016-04-11T12:00-04:00[America/New_York]
Is there any way around this?
Thanks!
Upvotes: 0
Views: 97
Reputation: 3250
long sourceTime = 1460372400000L;
//Select your timezone
TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = new GregorianCalendar(utcTimeZone);
calendar.setTimeInMillis(sourceTime);
//Get Current Time Zone
LocalTime time = LocalTime.now();
System.out.println("Local Time Zone: "+ZoneId.systemDefault().toString());
System.out.println("Current local time : " + time);
Upvotes: 0
Reputation: 533432
1460372400000L
is Mon, 11 Apr 2016 11:00:00 GMT
or Mon, 11 Apr 2016 12:00:00 BST
2016-04-11T11:00:00Z
is 2016-04-11T07:00-04:00[America/New_York]
so you are converting from one time zone to another.
If you just want the local time zone, I suggest you use LocalDateTime
LocalDateTime dt = LocalDateTime.ofEpochSecond(1460376000L, 0 , ZoneOffset.ofHours(0));
System.out.println(dt);
prints
2016-04-11T12:00
Upvotes: 1