SQL データベースで時間型を照会するために java.sql.Time オブジェクトを作成しようとしていますが、受け取った文字列を解析するために joda を使用しています。
私はいくつかの異なるアプローチを試みました。これは私の最新のものです。
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){
...
}
ただし、出力は受信した入力より常に 1 時間遅れます。たとえば、入力が UTC で 10:30:00 の場合、startTime は現地時間の 4:30:00 にする必要があります。しかし、代わりに 3:30 を取得します。
解決済み
DateTime beginning = new DateTime(DateTimeZone.UTC).toDateMidnight().toDateTime().plus(DateTime.parse(times[0], formatter).getMillis());
startTime = new Time(beginning.getMillis());
今朝の午前 0 時にタイム ゾーン UTC で新しい日付を作成し、UTC 時刻をミリ秒単位で追加します。次に、java.sql.Time オブジェクトに変換されます。