I am trying to create a java.sql.Time object to query time types in an SQL database, but I am using joda to parse the string I get.
I tried several different approaches. This is my last.
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 below the accepted input. For example, if you enter 10:30:00 in UTC, startTime should be 4:30:00 local time. But instead, I get 3:30.
solvable
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 the UTC time zone, then adds the UTC time of the day in milliseconds. Then it is converted to a java.sql.Time object.
source share