Java Date and Time: How do I make plus() and until() to be each others inverse

安稳与你 提交于 2020-01-14 12:39:52

问题


Most of the time, these 2 methods are each others inverse:

  • Temporal Temporal.plus(long, TemporalUnit)
  • long Temporal.until(Temporal, TemporalUnit)

For example starting from 1-JAN:

System.out.println("1-JAN plus 1 month: " +
    LocalDate.of(2017, 1, 1).plus(1, ChronoUnit.MONTHS));
System.out.println("1-JAN until 1-FEB in months: " +
    LocalDate.of(2017, 1, 1).until(LocalDate.of(2017, 2, 1), ChronoUnit.MONTHS));

They are each others inverse:

1-JAN plus 1 month: 2017-02-01
1-JAN until 1-FEB in months: 1 // GOOD


However, in this example starting from 31-JAN:

System.out.println("31-JAN plus 1 month: " +
    LocalDate.of(2017, 1, 31).plus(1, ChronoUnit.MONTHS));
System.out.println("31-JAN until 28-FEB in months: " +
    LocalDate.of(2017, 1, 31).until(LocalDate.of(2017, 2, 28), ChronoUnit.MONTHS));

They are not each others inverse:

31-JAN plus 1 month: 2017-02-28 // Bad? Should ceil to 1 March?
31-JAN until 28-FEB in months: 0 // Or is this bad? Should be 1?


How can I make them to be each others inverse?


回答1:


There is only 28 days between those dates so the number of whole months is 0. However you could use a bit of rounding if you want less than 1 month be be considered a month. e.g.

// compare the months after rounding to the next month
LocalDate a = LocalDate.of(2017, 1, 31);
LocalDate b = LocalDate.of(2017, 2, 28);
long months = a.plus(1, ChronoUnit.MONTHS)
        .until(b.plus(1, ChronoUnit.MONTHS), ChronoUnit.MONTHS);
System.out.println(a + " until " + b + " in months: " + months);

prints

2017-01-31 until 2017-02-28 in months: 1

and

// compare the months after rounding to the next month
LocalDate a = LocalDate.of(2017, 1, 31);
LocalDate b = LocalDate.of(2017, 3, 30);
long months = a.plus(1, ChronoUnit.MONTHS)
        .until(b.plus(1, ChronoUnit.MONTHS), ChronoUnit.MONTHS);
System.out.println(a + " until " + b + " in months: " + months);

prints

2017-01-31 until 2017-03-30 in months: 2

I probably want the plus to ceil instead of to floor

You might want to treat all months as notionally 30 days depending on your requirements.



来源:https://stackoverflow.com/questions/36593101/java-date-and-time-how-do-i-make-plus-and-until-to-be-each-others-inverse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!