0

I have a dataset that is in EST time without any daylight saving. Each datetime is read from string and a zonedDatetime is created using

ZonedDateTime java.time.ZonedDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone)

with a ZoneId.of("America/New_York");

I need to convert these to an epoch second but the built in toEpochSecond method converts to my local time which is BST with day light saving. As a result the timestamps are four to five hours off depending on time of year. Is there a way to get a unix timestamp that does not take into account any local time so the timestamp matches the datetime in the original string?

daedalus
  • 105
  • 2
  • 10
  • What makes you think that `toEpochSecond` converts to BST? It converts to a timezoneless value. It's probable that whatever you're using to _check the value_ of `toEpochSecond` is inserting your local timezone. – Louis Wasserman Sep 28 '20 at 19:16
  • from javadoc>Converts this date-time to the number of seconds from the epoch of 1970-01-01T00:00:00Z. This uses the local date-time and offset to calculate the epoch-second value, which is the number of elapsed seconds from 1970-01-01T00:00:00Z. Instants on the time-line after the epoch are positive, earlier are negative. here is the actual code of toEpochSecond> default long toEpochSecond() { long epochDay = toLocalDate().toEpochDay(); long secs = epochDay * 86400 + toLocalTime().toSecondOfDay(); secs -= getOffset().getTotalSeconds(); return secs; } – daedalus Sep 28 '20 at 21:30
  • Yes, and? It uses the zone from the `ZonedDateTime`, "America/New_York", which is exactly the right behavior. – Louis Wasserman Sep 28 '20 at 22:14

2 Answers2

0

I need to convert these to an epoch second but the built in toEpochSecond method converts to my local time which is BST with day light saving. As a result the timestamps are four to five hours off depending on time of year.

You must be doing something fundamentally wrong. Check the output of the following code and you will find that there is no difference.

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Formatter ignoring nanoseconds
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                                        .appendPattern("uuuu-MM-dd'T'HH:mm:ssXXX")
                                        .optionalStart()
                                        .appendLiteral('[')
                                        .parseCaseSensitive()
                                        .appendZoneRegionId()
                                        .appendLiteral(']')
                                        .toFormatter(Locale.ENGLISH);

        // The given time-zone
        ZoneId zone = ZoneId.of("America/New_York");

        ZonedDateTime zdtNow = ZonedDateTime.now(zone);
        System.out.println(zdtNow.format(formatter));

        // Epoch seconds from ZonedDateTime
        long epochSecond = zdtNow.toEpochSecond();
        System.out.println(epochSecond);

        // ZonedDateTime from epoch seconds
        ZonedDateTime zdtFromEpochSeconds = Instant.ofEpochSecond(epochSecond).atZone(zone);
        System.out.println(zdtFromEpochSeconds.format(formatter));
    }
}

Output:

2020-09-28T17:31:23-04:00[America/New_York]
1601328683
2020-09-28T17:31:23-04:00[America/New_York]
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

To convert ZonedDateTime to Unix epoch time stamp

Convert first to java.time.Instant and then set zone offset to UTC, before converting it to epoch seconds, see below:

zonedDateTime.toInstant().atZone(ZoneOffset.UTC).toEpochSecond();

Note: The variable zonedDateTime is of type java.time.ZonedDateTime and can be in any time zone before converting it to "Unix epoch time stamp" in seconds.

DigitShifter
  • 801
  • 5
  • 12
  • Thanks, thats what I was looking for – daedalus Sep 28 '20 at 22:09
  • Great! If you want to read more about java.time, see: https://stackoverflow.com/questions/32437550/whats-the-difference-between-instant-and-localdatetime. The answer with 940+ votes. This reading has helped me in the passed, considering java.time. – DigitShifter Sep 28 '20 at 22:36