Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.3 kB
1
Indexable
Never
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;

class Scratch {

    public static final DateTimeFormatter DEFAULT_FORMATS = new DateTimeFormatterBuilder()
            .appendOptional(DateTimeFormatter.ofPattern("EE d MMM yyyy h:mm a"))
            .optionalStart()
            .append(DateTimeFormatter.ofPattern("E d MMM y"))
            .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
            .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
            .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
            .optionalEnd()
            .toFormatter(Locale.US);

    public static void main(String[] args) {
        System.out.println(LocalDateTime.parse("Fri 12 May 2023 5:03 AM", DEFAULT_FORMATS).atZone(ZoneId.of("America/Toronto")));
        System.out.println(LocalDateTime.parse("Thu 11 May 2023", DEFAULT_FORMATS).atZone(ZoneId.of("America/Toronto")));

        System.out.println(LocalDateTime.parse("Fri 12 May 2023 5:03 AM", DEFAULT_FORMATS).atZone(ZoneId.of("America/Toronto")).toEpochSecond());
        System.out.println(LocalDateTime.parse("Thu 11 May 2023", DEFAULT_FORMATS).atZone(ZoneId.of("America/Toronto")).toEpochSecond());
    }

}