Джода анализирует 24-часовой шаблон времени с правильным DateTimeZone
Еще раз я борюсь с метками времени, датами и т. Д.
У меня есть следующий класс:
public class TimeConverter {
private static final String TAG = TimeConverter.class.getSimpleName();
public static final String PATTERN_TIME_24H = "HH:mm";
/**
* Returns a DateTime object for a given string in format HH:mm, assuming that the day is TODAY in UTC.
*
* @param timeString24h - The string representing the time in format HH:mm
* @param dateUTCseconds - The UTC timestamp to get the correct date for the given timeString24h
* @return DateTime object for the given time where actual date is derived from dateUTCseconds
*/
public static DateTime getDateTimeFrom24hString(final String timeString24h, final long dateUTCseconds) {
final LocalDate localDate = new LocalDate(dateUTCseconds * 1000, DateTimeZone.UTC);
final DateTimeFormatter formatter = DateTimeFormat.forPattern(PATTERN_TIME_24H);
DateTime dateTime = formatter.parseDateTime(timeString24h)
.withDate(localDate);
Log.d(TAG, "getDateTimeFrom24hString() --> input: " + timeString24h + ", output: " + dateTime.toString());
return dateTime;
}
public static DateTime getDateTimeFromSeconds(final long timeSeconds, final DateTimeZone dateTimeZone) {
final DateTime dateTime = new DateTime(timeSeconds * 1000, dateTimeZone);
Log.d(TAG, "getDateTimeFromSeconds() --> input: " + timeSeconds + ", output: " + dateTime.toString());
return dateTime;
}
}
я звоню getDateTimeFrom24hString
с параметрами:
timeString24h
= "16:03dateUTCseconds
= 1547222580 (пт, 11 января 2019 16:03:00 по Гринвичу)
возвращает:
2019-01-11T16: 03: 00,000+01:00
Для звонка getDateTimeFromSeconds
Я использую:
timeSeconds
= 1547222580dateTimeZone = DateTimeZone.UTC
возвращает:
2019-01-11T16: 03: 00.000Z
Что мне нужно сделать, чтобы эти два объекта DateTime точно представляли одно и то же время?
Изменить: Я подозреваю, что я должен сделать что-то здесь:
final DateTimeFormatter formatter = DateTimeFormat.forPattern(PATTERN_TIME_24H);
//TODO: Need to "tell" the dateTime object, that the parsed date is already UTC time
DateTime dateTime = formatter.parseDateTime(timeString24h)
.withDate(localDate);
1 ответ
Я только что нашел решение, которое, кажется, делает свое дело:
public static DateTime getDateTimeFrom24hString(final String timeString24h, final long dateUTCseconds) {
final LocalDate localDate = new LocalDate(dateUTCseconds * 1000, DateTimeZone.UTC);
final DateTimeFormatter formatter = DateTimeFormat.forPattern(PATTERN_TIME_24H);
DateTime dateTime = formatter.parseDateTime(timeString24h)
.withDate(localDate);
DateTime dateTimeUtc = new DateTime(DateTimeZone.UTC)
.withDate(localDate)
.withTime(dateTime.toLocalTime());
return dateTimeUtc;
}