Sum two dates in Java

前端 未结 9 666
夕颜
夕颜 2020-11-27 07:47

How can I add two dates in Java?

Example: The sum of \"2010-01-14 19:16:17\" \"0000-10-03 01:10:05\"
would result in \"2010-11-17 20:26:22\".

I know how

9条回答
  •  一向
    一向 (楼主)
    2020-11-27 08:26

    You need to define your EPOCH. The Java epoch (like Unix) is 1 Jan 1970 GMT/UTC. I assume you think you're adding ten months, 3 days and some odd hours from 1 Jan 0000 but you have a epoch offset until 1970. The maths may not necessarily work.

    Use Calendar or Joda (as mentioned). If you just simply want to add a number of seconds and days (&c) then feel free to add said # of milliseconds to your first date object.

提交回复
热议问题