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.