我无法使用 Java 中的日历方法进行数学运算,但得到了“java:二元运算符“-”的错误操作数类型”错误

发布于 2025-01-11 06:31:40 字数 334 浏览 0 评论 0原文

我正在尝试编写一个代码来告诉我还剩多少天可以上大学,但我无法使用当前日期来做到这一点。我可以通过设置日期轻松完成,但我想要当前日期,所以我必须使用日历方法,但不能使用它进行数学计算。 我的代码:

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Calendar calendar = Calendar.getInstance();
    Date start = sdf.parse("10/06/2022");
    System.out.println(start - calendar.getTime());

I'm trying to make a code that tells me how many days left for me to go college, but I am not able to do it with the current date. I can easily make it by setting a date, but I want the current date, so I have to use the calendar method, but can't do math using it.
My code:

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Calendar calendar = Calendar.getInstance();
    Date start = sdf.parse("10/06/2022");
    System.out.println(start - calendar.getTime());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独岁月 2025-01-18 06:31:40

tl;dr

ChronoUnit.DAYS.between( 
    LocalDate.now( ZoneId.of( "Pacific/Auckland" ) ) , 
    LocalDate.parse( "10/06/2022" , DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ) 
) 

详细信息

您正在使用可怕的日期时间类,这些类在几年前已被 JSR 310 中定义的现代 java.time 类取代。切勿使用 Date/日历

此外,您还尝试使用日期时间类来表示具有 UTC 时间(偏移量为零)的日期来保存仅日期值。方钉,圆孔。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate graduationDate = LocalDate.parse( "10/06/2022" , f ) ;

确定今天的日期。这需要一个时区。对于任何特定时刻,全球各地的日期因时区而异。

ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;  // Or ZoneId.systemDefault()
LocalDate today = LocalDate.now( z ) ;

使用 java.time.temporal.ChronoUnit

long days = ChronoUnit.DAYS.between( today , graduationDate ) ;

请参阅在 IdeOne.com 上实时运行的代码

graduationDate: 2022-06-10
today: 2022-03-05
days: 97

提示:了解用于将日期时间值交换为文本的 ISO 8601 标准。

tl;dr

ChronoUnit.DAYS.between( 
    LocalDate.now( ZoneId.of( "Pacific/Auckland" ) ) , 
    LocalDate.parse( "10/06/2022" , DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ) 
) 

Details

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310. Never use Date/Calendar.

Also, you are attempting to use a date-time class representing a date with time-of-day as seen in UTC (offset of zero) to hold a date-only value. Square peg, round hole.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate graduationDate = LocalDate.parse( "10/06/2022" , f ) ;

Determine today's date. That requires a time zone. For any given moment, the date varies around the globe by time zone.

ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;  // Or ZoneId.systemDefault()
LocalDate today = LocalDate.now( z ) ;

Calculate elapsed time using java.time.temporal.ChronoUnit.

long days = ChronoUnit.DAYS.between( today , graduationDate ) ;

See this code run live at IdeOne.com.

graduationDate: 2022-06-10
today: 2022-03-05
days: 97

Tip: Learn about the ISO 8601 standard for exchanging date-time values as text.

忆悲凉 2025-01-18 06:31:40
    Calendar calendar = Calendar.getInstance();   
    Calendar collegeDate = Calendar.getInstance();
    collegeDate.set(Calendar.DATE,10);
    collegeDate.set(Calendar.MONTH, 5);
    collegeDate.set(Calendar.YEAR, 2022);
    System.out.println(Duration.between(calendar.toInstant(), collegeDate.toInstant()).toDays());
    Calendar calendar = Calendar.getInstance();   
    Calendar collegeDate = Calendar.getInstance();
    collegeDate.set(Calendar.DATE,10);
    collegeDate.set(Calendar.MONTH, 5);
    collegeDate.set(Calendar.YEAR, 2022);
    System.out.println(Duration.between(calendar.toInstant(), collegeDate.toInstant()).toDays());
情丝乱 2025-01-18 06:31:40

你可以试试这个

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Calendar calendar = Calendar.getInstance();
        Date start = sdf.parse("10/06/2022");

        long dif = Math.abs(calendar.getTimeInMillis() - start.getTime());
        long result = TimeUnit.DAYS.convert(dif, TimeUnit.MILLISECONDS);

        System.out.println(result);

You can try this

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Calendar calendar = Calendar.getInstance();
        Date start = sdf.parse("10/06/2022");

        long dif = Math.abs(calendar.getTimeInMillis() - start.getTime());
        long result = TimeUnit.DAYS.convert(dif, TimeUnit.MILLISECONDS);

        System.out.println(result);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文