mani deep
mani deep

Reputation: 1

How can I consider given date in string format as est and convert it to utc in java?

I have a date in the string format String date = "2021-04-26 08:28:56 "

Now, I want to consider this as an EST Date and convert it into UTC.

The UTC for this would be "2021-04-25 10:58:56"

So,

Input :

2021-04-26 08:28:56

Output :

2021-04-25 10:58:56

How can I do this in Java ?

Upvotes: 0

Views: 76

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 338564

tl;dr

LocalDateTime
.parse(
    "2021-04-26 08:28:56"
    .replace( " " , "T" )
)
.atZone(
    ZoneId.of( "America/New_York" )
)
.toInstant()
.toString()
.replace( "T" , " " )
.replace( "Z" , "" )

See this code run live at IdeOne.com.

2021-04-26 12:28:56

Your expected result is incorrect. If by EST you meant a time zone such as America/New_York, that zone at that moment is four hours behind UTC. So to get from 8 AM you must add 4 hours, to get 12 noon rather than your expected 10 AM hour on the previous date.

Details

Parse your input as a LocalDateTime because it lacks an indicator of time zone or offset-from-UTC. We convert your input to comply with ISO 8601 by replacing the SPACE in the middle with a T.

A LocalDateTime does not represent a moment, is not a point on the timeline. This class represents only a date and time-of-day. Without the context of a time zone or offset-from-UTC, we do not know what clock where strikes that time.

You claim to know this string was intended to represent a date-time as seen in EST. Unfortunately, EST is not a real time zone name. Did you mean east coast time of North America, such as America/New_York? If so, your expected output is incorrect.

If so, obtain a ZoneId for that time zone. Apply the time zone to get a ZonedDateTime. Now we have defined a moment, a specific point on the timeline.

You want to see the same moment in UTC. One easy way to adjust to UTC is to simply extract an Instant from our ZonedDateTime object. An Instant object is always in UTC, by definition.

Your desired output is similar to the standard ISO 8601 format used by default in the toString method of Instant. Just remove the T from between the date and the time-of-day, and remove the Z at the end that represents an offset of zero hours-minutes-seconds. By the way, I suggest not removing the Z to make the meaning crystal clear. Removing the Z introduces ambiguity.

String input = "2021-04-26 08:28:56".replace( " " , "T" ) ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;
ZoneId z = ZoneId.of( "America/New_York" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;
Instant instant = zdt.toInstant() ;  // Adjust to UTC by extracting an `Instant` object. `Instant` is always in UTC, by definition.
String output = instant.toString().replace( "T" , " " ).replace( "Z" , "" ) ;

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.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

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. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

Upvotes: 1

Related Questions