0

I am reading a text time format as a string as follows. How can I convert this to unix epoch time in java. Can you help me?

String = "Feb 11 2023 9:35:41 PM"

Example: String = "Feb 11 2023 9:35:41 PM"

  • 1
    Your search engine can help you. I expect you to find a good answer faster than anyone can type an answer here. Only avoid search results that use `SimpleDateFormat`, `Date` and/or `Calendar` since they are troublesome and outdated. – Ole V.V. Feb 14 '23 at 07:59
  • `LocalDateTime.parse("Feb 11 2023 9:35:41 PM", DateTimeFormatter.ofPattern("MMM d u h:mm:ss a", Locale.ENGLISH)).atZone(ZoneId.systemDefault()).toEpochSecond()`. In my time zone (Europe/Copenhagen) I got `1676147741`. – Ole V.V. Feb 14 '23 at 08:03
  • Thanks Ole, Solved with your suggestion. LocalDateTime.parse("Feb 11 2023 9:35:41 PM", DateTimeFormatter.ofPattern("MMM d u h:mm:ss a", Locale.ENGLISH)).atZone(ZoneId.systemDefault()).toEpochSecond() – hknkaras Feb 14 '23 at 09:18
  • Good to read. You may use a named time zone instead of the default, like for example `ZoneId.of("Pacific/Norfolk")`. And you may well prefer to extract the formatter into a constant ( `private static final` field). – Ole V.V. Feb 14 '23 at 09:45

0 Answers0