0

I have a Date which is coming like this Thu Dec 31 16:00:00 EST 1969.

So i just need to add one day in this so that i will get Thu Jan 01 16:00:00 EST 1970.

any idea with java ?

Er KK Chopra
  • 1,834
  • 8
  • 31
  • 55

1 Answers1

4

If you use Java 8, you could use ZonedDateTime.

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");

ZonedDateTime zonedDateTime = ZonedDateTime.parse("Wed Dec 31 16:00:00 EST 1969", dtf);
zonedDateTime = zonedDateTime.plusDays(1);

System.out.println(dtf.format(zonedDateTime));
Lungu Daniel
  • 816
  • 2
  • 8
  • 15
  • getting java.time.format.DateTimeParseException: Text 'Thu Jan 01 16:00:00 EST 1970' could not be parsed at index 0 – Er KK Chopra Apr 23 '19 at 14:35
  • @Er KK Chopra, I edited the post, now it should be ok. – Lungu Daniel Apr 23 '19 at 17:03
  • 1
    **Incorrect.** `LocalDateTime` is precisely the *wrong* class to be using here. That class cannot be used to represent a moment. That class purposely ignores time zones. So your results will be incorrect when failing to account for anomalies such as Daylight Saving Time (DST). Use `ZonedDateTime`. – Basil Bourque Apr 23 '19 at 18:15
  • Thanks @BasilBourque for your remark, I changed the solution. – Lungu Daniel Apr 24 '19 at 06:22