You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何计算dd-MMM-yy HH:mm格式日期的差值?代码报错求助

Fixing Your Date Difference Calculation

Let's work through the issues in your code step by step—you've got a couple of small but critical mistakes with date types, formatting, and value handling that are causing the error.

Key Problems in Your Code

  1. Wrong date type choice
    You're using LocalDate, which only stores year/month/day, but your input date includes a time component (10:24 AM). You need to use LocalDateTime to capture the full date-time information.

  2. Incorrect formatter pattern
    Your example date has an AM marker, but your pattern uses HH:mm (24-hour format, which doesn't support AM/PM). The correct pattern should be dd-MMM-yy hh:mm a:

    • hh = 12-hour clock format
    • a = matches the AM/PM suffix
      Also, add Locale.ENGLISH to the formatter to ensure month abbreviations like apr are parsed correctly (avoids locale-specific parsing errors).
  3. Invalid now assignment
    dtf.format(now) returns a String, which can't be assigned back to a LocalDate variable. You don't need to format the current date-time to calculate the difference—just use the raw LocalDateTime.now() value.

  4. Parameter order in between()
    ChronoUnit.DAYS.between(start, end) returns the number of days from the start date to the end date. If you pass now first, you'll get a negative number if open is in the past. Swap the order to get a positive elapsed day count.

Corrected Code

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Locale;

public class DateDiffCalculator {
    public static void main(String[] args) {
        // Example input date string
        String openTime = "25/apr/18 10:24 AM";
        
        // Formatter with correct pattern and English locale for month parsing
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MMM-yy hh:mm a", Locale.ENGLISH);
        
        // Parse input to LocalDateTime (includes date + time)
        LocalDateTime open = LocalDateTime.parse(openTime, dtf);
        
        // Get current date-time directly (no formatting needed for calculation)
        LocalDateTime now = LocalDateTime.now();
        
        // Calculate elapsed days from open date to now
        long elapsedDays = ChronoUnit.DAYS.between(open, now);
        
        System.out.println("Elapsed days: " + elapsedDays);
    }
}

Bonus: If You Only Need Date (No Time) Difference

If you want to ignore the time component and calculate the difference between just the dates, convert both LocalDateTime objects to LocalDate:

LocalDate openDate = open.toLocalDate();
LocalDate nowDate = now.toLocalDate();
long elapsedDays = ChronoUnit.DAYS.between(openDate, nowDate);

内容的提问来源于stack exchange,提问作者A_Elric

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:27:32