Reputation: 1025
I have a localdatetime
object that is in UTC. I want to convert into IST.
How can I do that?
LocalDateTime dateTimeOfAfterFiveDays = LocalDateTime.ofEpochSecond(after5,0,ZoneOffset.UTC);
Upvotes: 14
Views: 104709
Reputation: 183
package org.example;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
LocalDateTime ldt = LocalDateTime.now(); // assume this time is UTC
System.out.println(ldt);
ZonedDateTime zoned = ldt.atZone(ZoneId.of("UTC"));
Instant instant=zoned.toInstant();
ldt=instant.atZone(ZoneId.of("Asia/Kolkata")).toLocalDateTime();
System.out.println(ldt);
}
}
Upvotes: 0
Reputation: 338564
Use Instant
, not LocalDateTime
for tracking a moment.
Instant // Represents a moment in UTC with a resolution of nanoseconds.
.ofEpochSecond(
myCountOfWholeSecondsSinceStartOf1970UTC // Internally, time is tracked as a count of seconds since 1970-01-01T00:00Z plus a fractional second as nanoseconds.
) // Returns a moment in UTC.
.atZone( // Adjust from UTC to another time zone. Same moment, different wall-clock time.
ZoneId.of( "Asia/Kolkata" ) // Specify time zone name in `Continent/Region` format, never 2-4 letter pseudo-zone.
) // Returns a `ZonedDateTime` object.
.toString() // Generate a string representing the value of this `ZonedDateTime` in standard ISO 8601 format extended to append the name of the time zone in square brackets.
LocalDateTime dateTimeOfAfterFiveDays = LocalDateTime.ofEpochSecond(after5,0,ZoneOffset.UTC);
LocalDateTime
is the wrong class to use here. This class cannot represent a moment. It lacks any concept of time zone or offset-from-UTC. A LocalDateTime
holds just a date and a time-of-day, say noon on the 23rd of January this year. But we have no idea if what was intended was noon in Tokyo, noon in Kolkata, noon in Paris, or noon in Montréal — all of these are hours apart, very different moments.
Instant
To represent a moment in UTC, use Instant
.
Apparently you have a count of whole seconds since the epoch reference of first moment of 1970 in UTC.
Instant instant = Instant.ofEpochSecond( count ) ;
ZoneId
& ZonedDateTime
To see this value through the wall-clock time used by the people of a particular region (a time zone), apply a ZoneId
to get a ZonedDateTime
.
Specify a proper time zone name in the format of Continent/Region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 2-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
Upvotes: 14
Reputation: 325
In Java 8, you can use ZonedDateTime to convert LocalDateTime with respect to a specific ZoneId. Considering your example, the conversion could be implemented as follows:
ZoneId istZoneId = ZoneId.of("Asia/Kolkata");
LocalDateTime dateTimeOfAfterFiveDays = LocalDateTime.ofEpochSecond(after5,0,ZoneOffset.UTC);
ZonedDateTime zonedDateTimeOfAfterFiveDays = dateTimeOfAfterFiveDays.atZone(istZoneId);
Hope it helps!
Upvotes: 0
Reputation: 48404
Since Java 8 the date/time API is pretty easy to work with.
In your case:
// your local date/time with no timezone information
LocalDateTime localNow = LocalDateTime.now();
// setting UTC as the timezone
ZonedDateTime zonedUTC = localNow.atZone(ZoneId.of("UTC"));
// converting to IST
ZonedDateTime zonedIST = zonedUTC.withZoneSameInstant(ZoneId.of("Asia/Kolkata"));
You'll see a difference in the time (and possibly the date) between zonedUTC
and zonedIST
, reflecting the time zone offset between the two.
Note the usage of withZoneSameInstant here, e.g. as opposed to withZoneSameLocal
.
Upvotes: 34