以dd-MMM-yy HH:mm格式减去两个日期

时间:2018-04-25 16:33:09

标签: java localdate

日期如下:

  

25 / apr / 18 10:24 AM

这是我尝试使用

的代码
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)

1 个答案:

答案 0 :(得分:2)

首先,您的模式需要支持从您的示例判断AM / PM,其次,您现在不需要解析,因为您希望将其用作LocalDate实例。

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);