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.