Converting string to Instant

We Are Going To Discuss About Converting string to Instant. So lets Start this Java Article.

Converting string to Instant

  1. Converting string to Instant

    It seems like Time Zone in your computer(server) is US Pacific DST (GMT-7), but you expect to have result for US Eastern DST (GMT-4).
    Instant.toString() returns UTC (GMT+0) DateTime in ISO-8601 format. ('Z' at the end means UTC).

  2. Converting string to Instant

    It seems like Time Zone in your computer(server) is US Pacific DST (GMT-7), but you expect to have result for US Eastern DST (GMT-4).
    Instant.toString() returns UTC (GMT+0) DateTime in ISO-8601 format. ('Z' at the end means UTC).

Solution 1

tl;dr

  • Fix your formatting pattern for unpadded month & day.
  • Use only java.time classes, never the legacy classes.

Contrived example:

LocalDateTime.parse(                   // Parse as an indeterminate `LocalDate`, devoid of time zone or offset-from-UTC. NOT a moment, NOT a point on the timeline.
    "04:30 PM, Sat 5/12/2018" ,        // This input uses a poor choice of format. Whenever possible, use standard ISO 8601 formats when exchanging date-time values as text. Conveniently, the java.time classes use the standard formats by default when parsing/generating strings.
    DateTimeFormatter.ofPattern( "hh:mm a, EEE M/d/uuuu" , Locale.US )  // Use single-character `M` & `d` when the number lacks a leading padded zero for single-digit values.
)                                      // Returns a `LocalDateTime` object.
.atZone(                               // Apply a zone to that unzoned `LocalDateTime`, giving it meaning, determining a point on the timeline.
    ZoneId.of( "America/Toronto" )     // Always specify a proper time zone with `Contintent/Region` format, never a 3-4 letter pseudo-zone such as `PST`, `CST`, or `IST`. 
)                                      // Returns a `ZonedDateTime`. `toString` → 2018-05-12T16:30-04:00[America/Toronto].
.toInstant()                           // Extract a `Instant` object, always in UTC by definition.
.toString()                            // Generate a String in standard ISO 8601 format representing the value within this `Instant` object. Note that this string is *generated*, not *contained*.

2018-05-12T20:30:00Z

Use single-digit formatting pattern

You used MM in your formatting pattern, to mean any single-digit value (months January-September) will appear with a padded leading zero.

But your input lacks that padded leading zero. So use a single M.

Ditto for day-of-month I expect: d rather than dd.

Use only java.time

You are using troublesome flawed old date-time classes (Date & SimpleDateFormat) that were supplanted years ago by the java.time classes. The new classes entirely supplant the old. No need to mix the legacy and modern.

LocalDateTime

Parse as a LocalDateTime because your input string lacks any indicator of time zone or offset-from-UTC. Such a value is not a moment, is not a point on the timeline. It is only a set of potential moments along a range of about 26-27 hours.

String input = "04:30 PM, Sat 5/12/2018";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "hh:mm a, EEE M/d/uuuu" , Locale.US );  // Specify locale to determine human language and cultural norms used in translating that input string.
LocalDateTime ldt = LocalDateTime.parse( input , f );

ldt.toString(): 2018-05-12T16:30

ZonedDateTime

If you know for certain that input was intended to represent a moment using the wall-clock time used by the people of the Toronto Canada region, apply a ZoneId to get a ZonedDateTime object.

Assigning a time zone gives meaning to your unzoned LocalDateTime. Now we have a moment, a point on the timeline.

ZoneId z = ZoneId.of( "America/Toronto" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;  // Give meaning to that `LocalDateTime` by assigning the context of a particular time zone. Now we have a moment, a point on the timeline.

zdt.toString(): 2018-05-12T16:30-04:00[America/Toronto]

Instant

To see that same moment as UTC, extract an Instant. Same moment, different wall-clock time.

Instant instant = zdt.toInstant() ;

instant.toString(): 2018-05-12T20:30:00Z


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.

Original Author Basil Bourque Of This Content

Solution 2

It seems like Time Zone in your computer(server) is US Pacific DST (GMT-7), but you expect to have result for US Eastern DST (GMT-4).

Instant.toString() returns UTC (GMT+0) DateTime in ISO-8601 format. (‘Z’ at the end means UTC).

SimpleDateFormat treats DateTime String in default Time Zone of computer when it is not specified. And your input does not specify time zone.

So, you need to do something about in what time zone your input is.

PS. on mine machine in Eastern DST your code gives me result exactly as you expected.

Original Author Vadim Of This Content

Solution 3

For the description you can read here [https://www.baeldung.com/java-string-to-date]

String requestTime = "04:30 PM, Sat 5/12/2018 America/Toronto";
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a, EEE M/dd/yyyy z");
            ZonedDateTime zonedDateTime = ZonedDateTime.parse(requestTime, formatter);
            System.out.println(zonedDateTime.toInstant());  

Original Author shila mosammami Of This Content

Solution 4

Instant.parse(String) appropriately formatted

Original Author Andrea Paolo Ferraresi Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment