0

Once more I am struggling with timestamps, dates etc.

I have following class:

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;
    }

}

I am calling getDateTimeFrom24hString with params:

  • timeString24h = "16:03
  • dateUTCseconds = 1547222580 (Fri, 11 Jan 2019 16:03:00 GMT)

returns:

2019-01-11T16:03:00.000+01:00

For the call to getDateTimeFromSeconds I am using:

  • timeSeconds = 1547222580
  • dateTimeZone = DateTimeZone.UTC

returns:

2019-01-11T16:03:00.000Z


What do I have to do, that those two DateTime objects are exactly representing the same time?

Edit: My suspicion is, that I have to do something here:

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);
AZOM
  • 265
  • 5
  • 15

1 Answers1

0

I just found a solution that seems to do the trick:

    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;
    }
AZOM
  • 265
  • 5
  • 15