3

Is there any problem with java Date() class it always gives me wrong time for example if my system time is 11 it gives me 12 (it always adds one hour to current system time). How could I solve this problem? I want date to give me the same time as system time.is this related to timezone? I've tried:

Calendar startingTime = Calendar.getInstance(TimeZone.getDefault());
System.out.println(startingTime.getTime());

and

System.out.println(new Date());

Thanks

Feras Odeh
  • 9,136
  • 20
  • 77
  • 121

3 Answers3

6

This can be caused by one of two things:

  • The date in the underlying operating system is incorrect (but you may accidentally have configured it so you cannot tell).
  • The timezone determined by the JVM is incorrect (including that daylight savings information may be outdated).

Perhaps you could add the output of your new Date() along with information about what you were expecting and where you are?

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
2

Just to complement Basil's answer, updating TZdata is easy.

First download the Timezone Updater Tool from Oracle at: https://www.oracle.com/java/technologies/javase-tzupdater-downloads.html

Then run it using the target JVM. I have multiples JVMs in my computer. In my case the command was:

sudo /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/bin/java -jar tzupdater.jar -v -l https://www.iana.org/time-zones/repository/tzdata-latest.tar.gz
Chocksmith
  • 1,188
  • 2
  • 12
  • 40
1

tl;dr

ZonedDateTime.now(             // Capture the current moment as seen in the wall-clock time used by the people of a certain region (time zone).
    ZoneId.of( "Asia/Gaza" )   // Specify time zone by proper IANA-defined name, never a 3-4 letter pseudo-zone such as `PST` or `CST`.
)                              // Returns a `ZonedDateTime` object.
.toString()                    // Generate a `String` to represent the value of that `ZonedDateTime`.

Details

The Answer by Andersen is correct.

Here is some modern code to help with debugging.

Verify current moment in UTC

Regarding Andersen’s first bullet…

See if your computer’s clock was incorrectly set or has drifted. Ask for the current moment in UTC to avoid the complications of time zones.

Instant now = Instant.now() ;  // Capture current moment in UTC.

Compare your results to a live web site showing UTC time such as this:

https://time.is/UTC

If incorrect, connect your computer to a reliable time server either local to your network or over the internets.

Check default time zone

Regarding Andersen’s second bullet…

Check the JVM’s current default time zone.

ZoneId z = ZoneId.systemDefault() ;
String output = z.toString() ;

Specify desired time zone

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment during runtime(!), so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-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/Gaza" ) ;  
ZonedDateTime now = ZonedDateTime.now( z ) ;

Update tzdata

As Andersen mentioned, your time zone data source may be out-of-day. Politicians around the world have shown an odd predilection for redefining the offset used by their time zone(s).

Most systems get their time zone data from the tzdata published by the IANA. This data was formerly known as the “Olson Database”.

You must keep this data up-to-date if any time zones you care about have been changed. You may find time zone data in multiple places including

I looked for a history of changes to time zones in Palestine but that Wikipedia page, Time in the State of Palestine, is incomplete and needs some work.


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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154