How to get DST offset from Joda Time

In my java program I am doing something like this

1).

LocalDateTime currentDateTime = new LocalDateTime();

LocalDateTime newDateTime = new LocalDateTime(currentDateTime);
newDateTime = newDateTime.plusDays(daysOffset);
newDateTime = newDateTime.plusHours(hoursOffset);
newDateTime = newDateTime.plusMinutes(minutesOffset);

      

Later in the code I do

2.)

boolean newDateTimeIsInWinter =
                    dateTimeZone.getOffset(newDateTime.toDateTime().getMillis()) == dateTimeZone.getStandardOffset(newDateTime.toDateTime().getMillis());

      

The call newDateTime.toDateTime()

can lead to java.lang.IllegalArgumentException: Illegal instant due to time zone offset transition

.

So I would like to add something like this between 1.) and 2.)

if (dateTimeZone.isLocalDateTimeGap(newDateTime))
{
    int dstOffsetMinutes = ???;
    newDateTime = newDateTime.plusMinutes(dstOffsetMinutes);
}

      

Can anyone tell me the correct replacement for ???

This is not as easy as installing it on 60

. For example, the LHST time zone only has a 30 minute offset.

+3


source to share


2 answers


Ask DateTimeZome

About DST

To determine if a particular moment is in Daylight Saving Time or not for a particular time zone, ask the <object DateTimeZone][1]

.

boolean isStandardTime = DateTimeZone.forID( "America/Montreal" ).isStandardOffset( DateTime.now().getMillis() );

      



When to use "local" classes

If you are concerned about time zone, offsets, and daylight saving time, do not use LocalDateTime

, LocalDate

or LocalTime

. For this there is DateTime

.

Use the Local classes when you mean a date and / or time not for a specific place or time zone at all. For example, if you want to say "Christmas starts on 2014-12-25T00: 00: 00.000", which means at midnight on the morning of the 25th in any particular place. But that LocalDateTime

could mean one DateTime

for Paris, but another DateTime

(another point) in Montreal.

+1


source


I solved my problem by using DateTime instead of LocalDateTime

1.) now

DateTime newDateTimeUTC = currentDateTime.toDateTime();
newDateTimeUTC = newDateTimeUTC.plusDays(daysOffset);
newDateTimeUTC = newDateTimeUTC.plusHours(hoursOffset);
newDateTimeUTC = newDateTimeUTC.plusMinutes(minutesOffset);

LocalDateTime newDateTime = newDateTimeUTC.toLocalDateTime();

      

2.) is still there



boolean newDateTimeIsInWinter =
    dateTimeZone.getOffset(newDateTime.toDateTime().getMillis()) == dateTimeZone.getStandardOffset(newDateTime.toDateTime().getMillis());

      

No need for isLocalDateTimeGap

or anything else.

But that still doesn't answer the original question. How to get DST offset from Joda Time?

0


source







All Articles