-2

The date looks like:

25/apr/18 10:24 AM

Here's the code I'm trying to use

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MMM-yy HH:mm");
LocalDate open = LocalDate.parse(openTime.toString(), dtf);
LocalDate now = LocalDate.now();
now = dtf.format(now); // <-- Can't do this as it's apparently a string and not a LocalDate
elapsedDays = ChronoUnit.DAYS.between(now, open)
Robert
  • 7,394
  • 40
  • 45
  • 64
A_Elric
  • 3,508
  • 13
  • 52
  • 85
  • 7
    So what exactly is the question? – Mureinik Apr 25 '18 at 16:34
  • @MureinikAdded some clarity, apparently formatting dates is hard – A_Elric Apr 25 '18 at 16:52
  • You may want to ask yourself: If today is, say, April 26, 2018, what would be the expected result of formatting it into your format? Since your format also includes time, this would not make sense to do. Also a `LocalDate`cannot have a format, a formatted date needs to be in a `String`. – Ole V.V. Apr 26 '18 at 09:34

1 Answers1

2

First of all your pattern needs to support AM/PM judging from your example and secondly, you don't need to parse now since you want to use it as a LocalDate instance.

String input = "22/Apr/18 10:24 AM";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MMM/yy hh:mm a", Locale.ENGLISH);
LocalDate open = LocalDate.parse(input, dtf);
LocalDate now = LocalDate.now();

long diff = ChronoUnit.DAYS.between(open, now);
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • Good answer. If you also want to take time of day into account when counting days, use `LocalDateTime` instead of `LocalDate`. – Ole V.V. Apr 26 '18 at 09:37