4

I'm trying to create a java.sql.Time object to query time types in an SQL database, but I'm using joda to parse the string that I receive.

I've tried a few different approaches. This is my most recent.

protected Time startTime = null;
protected static final String dateFormat = "HH:mm:ssZ";
protected static final DateTimeFormatter formatter = DateTimeFormat.forPattern(dateFormat);
public TimeBetweenFilter(String fieldName, String type, String value) {
    super(fieldName, type, value);
    String [] times = value.split("\\|");
    if(times.length == 2 && !times[0].isEmpty() && !times[1].isEmpty()){
        try{
            LocalDateTime current = LocalDateTime.now();
            LocalDateTime timeFromEpoch= new LocalDateTime(formatter.parseDateTime(times[0]));
            startTime = new Time(timeFromEpoch.withDate(current.getYear(), current.getMonthOfYear(), current.getDayOfMonth()).toLocalTime().toDateTimeToday().getMillis());
        }catch (Exception e){
           ...
        }

But the output is always 1 hour behind the input received. For instance, if the input is 10:30:00 in UTC, startTime should be 4:30:00 local time. But instead I get 3:30.

Solved

DateTime beginning = new DateTime(DateTimeZone.UTC).toDateMidnight().toDateTime().plus(DateTime.parse(times[0], formatter).getMillis());
startTime = new Time(beginning.getMillis());

Creates a new date, this morning at midnight with time zone UTC, then adds the UTC time of day in milliseconds. Then it's converted to a java.sql.Time object.

Plant_Face
  • 41
  • 1
  • 3
  • FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Feb 12 '18 at 05:39

1 Answers1

3

A LocalDateTime doesn't logically have any time zone or DST information. That's the whole point - it's "local" but not to any specific time zone. Two people in different time zones might both wake up at the same local date/time, but that doesn't mean they're the same instants.

If you need a type which includes a time zone, you should be using DateTime.

EDIT: Okay, one approach is to take "today in the local time zone", parse the time to a LocalTime, combine the two to form a LocalDateTime, convert that to a DateTime in UTC, then convert that DateTime to the local time zone instead. The difficulty is that the end dates may not be the same, in which case you may need to add or subtract a day. That could then change the time of day due to DST changes, which makes it harder still. There are likely to be nasty corner cases to consider.

It feels like your requirement is odd to start with though - it's odd to have just a time which is in UTC, which you want to use in a date/time in the local time zone. Perhaps if you gave some more context, that would help.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Alright, I've tried using `DateTime`. Let me rephrase. I receive a time in UTC format with no date information at all. I want to convert it to local time today. Problem is 17:30 UTC is 11:30 local today but 10:30 local on january 01 1970. Joda time is defaulting to the epoch date and I can't seem to find a workaround. – Plant_Face Sep 04 '13 at 19:01
  • @Plant_Face: What do you mean by "in UTC format"? Milliseconds since midnight? By "local time today" do you mean "in your current time zone"? – Jon Skeet Sep 04 '13 at 19:03
  • For the way it's used UTC format is the same as GMT. By local time I mean the current time zone of the user, on the current date so that daylight savings times are set accurately. – Plant_Face Sep 04 '13 at 19:24
  • @Plant_Face: I don't know what you mean by "For the way it's used UTC format is the same as GMT" - although I think I roughly see from the example in the question. It's slightly trickier as the date in UTC won't always be the same as the date in the local time zone... – Jon Skeet Sep 04 '13 at 19:26