Java: Difference between two dates spanning over months

前端 未结 4 1751
没有蜡笔的小新
没有蜡笔的小新 2021-01-06 06:31

I have the following code that successfully gets me the difference between two days (in days, hours, minutes, seconds):

SimpleDateFormat format = new SimpleD         


        
4条回答
  •  不思量自难忘°
    2021-01-06 07:25

    While you should use Joda Time simply as a much better date/time API, I suspect the problem is actually just that you're parsing the values incorrectly. Use a format string of:

    yyyy-MM-dd HH:mm:ss
    

    ... with dd instead of DD. The DD value is "day in year" which I suspect is confusing things, basically overriding the month part entirely. See the SimpleDateFormat documentation for more details.

    You can validate what's wrong by printing out d1 and d2 after parsing...

提交回复
热议问题