• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.time.Instant not local time.

 
Ranch Hand
Posts: 153
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am just beginnig to learn the new Time API.
When I run

I get the correct date and time.


returns a time two hours earlier. I live in Germany.

From the api doc:
now()
Obtains the current instant from the system clock.

In the book I read nothing is mentioned about using a TimeZone or ZoneId with Instant.
What do I miss?

 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instant is UTC I think (can't check as I don't have Java 8 here).
If you want the local time then there's a LocalTime class.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The API for Instant.toString() says this:

A string representation of this instant using ISO-8601 representation.

The format used is the same as DateTimeFormatter.ISO_INSTANT.


The latter in turn says this:

The ISO instant formatter that formats or parses an instant in UTC, such as '2011-12-03T10:15:30Z'.


You probably missed that Z which is the same as +00:00, UTC and GMT.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want something that is in your local timezone by default, then use ZonedDateTime instead of Instant:

 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I presume you have read the Java™ Tutorials section about dates and times, and the Instant documentation. You find an Instant object represents seconds since 1st January 1970, but in GMT/UTC, along with a nanoseconds field, but one cannot be absolutely certain about its precision compared to an atomic clock somewhere. Least of all when it is taken from a computer's internal clock.
 
reply
    Bookmark Topic Watch Topic
  • New Topic