Convert from java.util.TimeZone to org.joda.DateTimeZone

How can I convert an instance java.util.TimeZoneto in Java org.joda.DateTimeZoneand save daylight saving time?

+4
source share
2 answers

Joda-Time in maintenance mode

The Joda-Time project , now in maintenance mode , we recommend switching to the java.time classes .

java.time.ZoneId

Modern replacement and .java.util.TimeZone java.time.ZoneIdjava.time.ZoneOffset

. / java.time. , . TimeZone ZoneId.

java.util.TimeZone tz = java.util.TimeZone.getTimeZone( myZoneId );

... ...

java.time.ZoneId z = myLegacyTimeZone.toZoneId();

--UTC (DST) , ZoneRules. , .

+2

DateTimeZone.forTimeZone, , Joda , DST - TimeZone.

, "GMT", DST ( DateTimeZone.forID("GMT").isFixed() - true, , , AKA , AKA DST).

, DST ( , DST). DateTimeZone.forID("Europe/London").


DST, , DateTimeZone. DST, 27 /2013 31/Oct/2013, ( , ).

public class CustomTimeZone extends DateTimeZone {

    private DateTime dstStart;

    private DateTime dstEnd;

    protected CustomTimeZone(String id) {
        super(id);
        // DST starts at 27/Mar/2013 and ends at 31/Oct/2013
        this.dstStart = new DateTime(2013, 3, 27, 0, 0, 0, 0, DateTimeZone.UTC);
        this.dstEnd = new DateTime(2013, 10, 30, 23, 0, 0, 0, DateTimeZone.UTC);
    }

    @Override
    public String getNameKey(long instant) {
        return this.getID();
    }

    @Override
    public int getOffset(long instant) {
        // check if it in DST
        if (dstStart.getMillis() <= instant && instant < dstEnd.getMillis()) {
            // DST, offset is 1 hour ahead of UTC - value must be in milliseconds
            return 3600000;
        }
        return 0;
    }

    @Override
    public int getStandardOffset(long instant) {
        // assuming stardard offset is zero (same as UTC)
        return 0;
    }

    @Override
    public boolean isFixed() {
        return false;
    }

    @Override
    public long nextTransition(long instant) {
        if (instant < dstStart.getMillis()) {
            return dstStart.getMillis();
        }
        if (instant < dstEnd.getMillis()) {
            return dstEnd.getMillis();
        }
        return instant;
    }

    @Override
    public long previousTransition(long instant) {
        if (instant > dstEnd.getMillis()) {
            return dstEnd.getMillis();
        }
        if (instant > dstStart.getMillis()) {
            return dstStart.getMillis();
        }
        return instant;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof CustomTimeZone) {
            return getID().equals(((CustomTimeZone) obj).getID());
        }
        return false;
    }
}

:

CustomTimeZone customZone = new CustomTimeZone("Custom");
// date in DST (between March and October)
DateTime d = new DateTime(2013, 4, 20, 0, 0, 0, 0, customZone);
System.out.println(d); // 2013-04-20T00:00:00.000+01:00

// one minute before DST starts - offset is zero ("Z")
d = new DateTime(2013, 3, 26, 23, 59, 0, 0, customZone);
System.out.println(d); // 2013-03-26T23:59:00.000Z
// add 1 minute - DST starts and offset changes to +01:00 (clock moves 1 hour forward to 1 AM)
System.out.println(d.plusMinutes(1)); // 2013-03-27T01:00:00.000+01:00

// one minute before DST ends - offset is +01:00
d = new DateTime(1383173940000L, customZone);
System.out.println(d); // 2013-10-30T23:59:00.000+01:00
// add 1 minute - DST starts and offset changes to zero ("Z") (clock moves 1 hour back to 23 PM)
System.out.println(d.plusMinutes(1)); // 2013-10-30T23:00:00.000Z

DST, , , DST ( ) .

0

Source: https://habr.com/ru/post/1674254/


All Articles