user2374573
user2374573

Reputation: 69

java.time.Clock in java 1.8 gives unexpected time value

I read that the time API is much better in java 1.8, so I decided to use it in my application. I try to get current date & time to be able to compare times(sth like after() or before() in Date class) but when I type this code:

java.time.Clock clock = java.time.Clock.system(ZoneId.of("Europe/Warsaw"));
System.out.println(clock.instant().toString());

I get the time that is 2 hours before the current time.
What am I doing wrong?

Upvotes: 1

Views: 604

Answers (1)

Ray
Ray

Reputation: 3201

Clock.instant() gives you an Instant which lacks time zone information. This means when you call Instant.toString(), the output will be in UTC. Since Europe is on Daylight Savings time at the moment, you are two hours ahead of this.

Upvotes: 3

Related Questions