简体   繁体   English

如何在Java中移动日历?

[英]How to Shift Calendar in Java?

I am trying to write a Java method to shift a Calendar, using a time offset in milliseconds. 我试图编写一种Java方法来移动日历,使用以毫秒为单位的时间偏移量。 But when I try to shift for one month (30 days) it does not work: 但是,当我尝试移动一个月(30天)时,它不起作用:

Calendar c = new GregorianCalendar(2012, 5, 23, 13, 23, 10);
long original = c.getTimeInMillis();
System.out.println("Original Date "+c.getTime());

long oneMonth = 30*24*60*60*1000;   //one month in milliseconds
Calendar c_1Month = new GregorianCalendar();
c_1Month.setTimeInMillis(original+oneMonth);
System.out.println("After 1-month "+c_1Month.getTime());

The output is 输出是

Original Date Sat Jun 23 13:23:10 PDT 2012
After 1-month Sun Jun 03 20:20:22 PDT 2012

You can see that it does not shift it correctly to July 23rd. 您会看到它没有将其正确地转移到7月23日。 I understand there is a specific method add(field, amount) in Calendar and I can change month using that, but I wanna have one single method in my client to shift time, with providing shift amount in milliseconds (the amount of shift changes based on my tests, and I do not want to have several methods for that). 我了解日历中有一种特定的方法add(field,amount),我可以使用该方法更改月份,但我想在客户端中使用一种方法来更改时间,并提供以毫秒为单位的偏移量(偏移量基于在我的测试中,我不想为此使用多种方法)。

You think the variable oneMonth is a long, but you assign it an integer value. 您认为变量oneMonth是一个long,但是您为其分配了一个整数值。 But the result of 30*24*60*60*1000 doesn't fit into an integer and therefore overflows to a negative value. 但是30*24*60*60*1000不适合整数,因此会溢出为负值。

If you change your code to long oneMonth = 30*24*60*60*1000L; 如果将代码更改为long oneMonth = 30*24*60*60*1000L; it would work. 它会工作。

But despite of that "bug" in your code the comment of azurefrog is correct, this is not the recommended way to add one month, it would only be valid in case you like to add 30 days which is something different. 但是,尽管代码中存在“错误”,但azurefrog的注释是正确的,这不是建议增加一个月的方法,只有在您希望增加30天的情况下才有效,这是不同的。

Try to use something like this instead: 尝试改用如下形式:

c_1Month.add( Calendar.DAY_OF_MONTH, 30);

or 要么

c_1Month.add( Calendar.MONTH, 1);

The Calendar class is aware and able to handle all corner cases very good. Calendar类知道并能够很好地处理所有极端情况。 I would suggest you rely on this. 我建议你依靠这个。 It reduces possible bugs and makes code easier to read and understand. 它减少了可能的错误,并使代码更易于阅读和理解。

With Java 8 the code to use would be: 在Java 8中,要使用的代码为:

LocalDateTime.now().plusMonths(1);

or 要么

LocalDateTime.now().plusDays(30);

Add an "L" at the end of 30*24*60*60*1000 30*24*60*60*1000的末尾添加“ L”

The way you are calculating, you are transforming the whole thing into an INTEGER. 计算方式将整个过程转换为一个整数。

Don't believe me :), try printing the value before adding the L after the 1000; 不要相信我:),尝试在1000后面加上L之前打印值;

    System.out.println(oneMonth);

Your final code should be: 您的最终代码应为:

    Calendar c = new GregorianCalendar(2012, 5, 23, 13, 23, 10);
    long original = c.getTimeInMillis();
    System.out.println("Original Date "+c.getTime());

    long oneMonth = 30*24*60*60*1000L;   //one month in milliseconds

    Calendar c_1Month = new GregorianCalendar();
    c_1Month.setTimeInMillis(original+oneMonth);
    System.out.println("After 1-month "+c_1Month.getTime());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM