1

I am having difficulty using JodaTime to handle Daylight savings time.

 String time = "3:45-PM";   
 DateTimeFormatter formatter = DateTimeFormat.forPattern("KK:mm-a");
 DateTime dt = formatter.parseDateTime(time).withZone(DateTimeZone.forID("America/New_York"));
 dt = dt.toDateTime(DateTimeZone.UTC);

 startDate = startDate.withHourOfDay(dt.getHourOfDay()).withMinuteOfHour(dt.getMinuteOfHour());

Output from this code snippet ends up being:

2015-04-08 16:46:51.952  INFO 12244 --- [nio-8080-exec-1] VALUES PULLED                        : 03:45-PM
2015-04-08 16:46:51.952  INFO 12244 --- [nio-8080-exec-1] VALUES PULLED                        : 08:45-PM

Currently, the time is 5 hours off. Which is not handling Daylights saving time. How do I get Joda Time to take DLS into account?

user1420688
  • 67
  • 1
  • 9
  • Did you try calling `startDate.`[`toLocalDateTime()`](http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html#toLocalDateTime())? – Mick Mnemonic Apr 09 '15 at 17:59

2 Answers2

1

java.time

Quoted below is a notice at the Home Page of Joda-Time:

Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java.time (JSR-310).

Solution using java.time, the modern API:

You need to combine the current date with the time.

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strTime = "3:45-PM";
        ZoneId zoneId = ZoneId.of("America/New_York");

        LocalTime time = LocalTime.parse(strTime, DateTimeFormatter.ofPattern("h:m-a", Locale.ENGLISH));
        LocalDate date = LocalDate.now(zoneId);
        LocalDateTime ldt = date.atTime(time);
        System.out.println(ldt);

        // Date-time at UTC
        System.out.println(ZonedDateTime.of(ldt, zoneId).toInstant());
    }
}

Output:

2021-05-08T15:45
2021-05-08T19:45:00Z

Learn more about the the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

This happens because the DateTime object that you get after parsing is set to the date 01-01-1970. It looks like you expect that it would be set to today, but it isn't.

If you want the time to be interpreted as 3:45 PM today in the timezone America/New_York, do this:

String time = "3:45-PM";

DateTimeFormatter formatter = DateTimeFormat.forPattern("KK:mm-a")
        .withZone(DateTimeZone.forID("America/New_York"));

DateTime dt = formatter.parseDateTime(time)
        .withDate(LocalDate.now());

System.out.println(dt);

System.out.println(dt.withZone(DateTimeZone.UTC));

Note: You need to set the zone on the DateTimeFormatter.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • What you suggested work aside from 2 things. for .withDate(), it requires 3 ints, not a LocalDate.now(). But that was a simple fix. the other things is that one should not do this line: startDate = startDate.withHourOfDay(dt.getHourOfDay()).withMinuteOfHour(dt.getMinuteOfHour()); It loses the conversion and has abnormal behavior. Using localDate.now() was a better solution. Thank you. – user1420688 Apr 09 '15 at 21:50
  • @user1420688 about `withDate` needing 3 ints: maybe you're using a different (older) version of the Joda Time library that doesn't have the version of `withDate` that takes a `LocalDate`. – Jesper Apr 10 '15 at 07:07