0

I want to convert a string to date and get epochtime from that date. following is my code which is giving me error

       DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-mm-dd");

       LocalDateTime localDate = LocalDateTime.parse(day, formatter);

       long TCepochtime = localDate.toEpochSecond(ZoneOffset.ofTotalSeconds(19800));

it gives me error at line 2

day string is "2019-12-25"

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Kedar Vaze
  • 109
  • 1
  • 2
  • 10
  • 1
    It looks like you are trying to parse a date in a LocalDateTime, I suggest you use the LocalDate instead as well as using the DateFormatter instead of the DateTimeFormatter. – Damian Willmer Mar 25 '20 at 10:23
  • 1
    Also I think your pattern formatting is incorrect, it is case sensitive, so it might need to be "yyyy-MM-dd", alternatively you could try using the ISO_LOCAL_DATE constant – Damian Willmer Mar 25 '20 at 10:31
  • It’s a bit simpler than you thought. `LocalDate.parse("2019-12-25").atStartOfDay(ZoneId.of("Asia/Colombo")).toEpochSecond()` yields 1577212200. Insert your desired time zone. Don’t use `ZoneOffset.ofTotalSeconds()` for a time zone, it’s unreadable and will give a wrong result when used with historic dates or once the politicians change the offset for your time zone. – Ole V.V. Mar 25 '20 at 16:36

0 Answers0