Java calendar: why are UTC offsets reversed?

I am trying to process the time, and I came across something in Java, which somewhat puzzled me. Take this sample code:

public static void main(String[] args)
{
    //Calendar set to 12:00 AM of the current day (Eastern Daylight Time)
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-4"));
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    /////

    //Calendar set to 12:00 AM of the current day (UTC time)
    Calendar utcCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    utcCal.set(Calendar.HOUR_OF_DAY, 0);
    utcCal.set(Calendar.MINUTE, 0);
    utcCal.set(Calendar.SECOND, 0);
    utcCal.set(Calendar.MILLISECOND, 0);
    /////

    long oneHourMilliseconds = 3600000;
    System.out.println((cal.getTimeInMillis() - utcCal.getTimeInMillis()) / oneHourMilliseconds);
}

I present an algorithm for calculating the time presented cal, taking 1 of 2 forms:

  • Calculate milliseconds from Epoch, add offset (add -4)
  • Calculate the number of milliseconds (Epoch + offset). So, from milliseconds from ( Epoch - 4 * oneHourMilliseconds).

Both of these algorithms should produce a result that is 4 hours behind utcCal, but running the code returns 4.

- , cal, , 4 utcCal, 4 utcCal? -4?

+5
3

. "GMT-4" - , "UTC + 4", .. 4 UTC.

etcter tzdb:

# We use POSIX-style signs in the Zone names and the output abbreviations,
# even though this is the opposite of what many people expect.
# POSIX has positive signs west of Greenwich, but many people expect
# positive signs east of Greenwich.  For example, TZ='Etc/GMT+4' uses
# the abbreviation "GMT+4" and corresponds to 4 hours behind UTC
# (i.e. west of Greenwich) even though many people would expect it to
# mean 4 hours ahead of UTC (i.e. east of Greenwich).

:

, GMT...

+10

cal 2012-04-18 00:00:00 GMT-4.

2012-04-18 04:00:00 UTC ( , 12 AM GMT-4, 4 AM UTC).

utcCal 2012-04-18 00:00:00 UTC.

2012-04-18 04:00:00 2012-04-18 00:00:00 4 , , 4. .

+2

. +/- offset-from-UTC .

java.time

java.time, Java 8 :

  • UTC, +
  • UTC, -

, , (Asia/Kolkata), +05:30, UTC. America/Regina -06:00, UTC.

,

, , ,

offset-from-UTC - , ; . A - , , . , .

continent/region, America/Montreal, Africa/Casablanca Pacific/Auckland. 3-4 , EST IST, , (!).

ZoneId z = ZoneId.of( "Africa/Tunis" ) ; 

. . , - , "" .

LocalDate ld = LocalDate.now( z ) ;

UTC , Asia/Dubai.

ZonedDateTime zdt = ZonedDateTime.now( ZoneId.of( `Asia/Dubai` ) ) ;

zdt.toString(): 2018-03-01T02: 39: 18.801642 + 04: 00 [/]

UTC , America/Blanc-Sablon.

ZonedDateTime zdt = ZonedDateTime.now( ZoneId.of( `America/Blanc-Sablon` ) ) ;

zdt.toString(): 2018-02-28T18: 39: 18.801642-04: 00 [/-]

ZoneOffset

--UTC, ZoneOffset.

ZoneOffset offset = ZoneOffset.ofHoursMinutes( 4 , 30 ) ; // Positive hours is ahead of UTC (east), while negative hours is behind UTC (west). 

UTC ( ) ZoneOffset.UTC.


java.time

java.time Java 8 . legacy , java.util.Date, Calendar SimpleDateFormat.

Joda-Time, , java.time.

, . Oracle. Qaru . JSR 310.

java.time . JDBC, JDBC 4.2 , , java.sql.*.

java.time?

ThreeTen-Extra java.time . java.time. , Interval, YearWeek, YearQuarter .

0

All Articles