Sum two dates in Java

前端 未结 9 640
夕颜
夕颜 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:25

    If you are using the Date object, you can just do:

    Date d1 = ...
    Date d2 = ...
    
    long sum = d1.getTime() + d2.getTime();
    
    Date sumDate = new Date(sum);
    

    The code uses the .getTime() method that returns the number of milliseconds since the epoch. Needless to say the Date class has a lot of problems and should be avoided when possible.

    Do you want to sum other types instead?

    Update: for Calendar, I would do the following (based on javadocs):

    Calendar c1 = ...
    Calendar c2 = ...
    long sum = c1.getTimeInMillis() + c2.getTimeInMillis();
    Calendar sumCalendar = (Calendar)c1.clone();
    sumCalendar.setTimeInMillis(sum);
    

    UPDATED: As Steve stated, this works if the Date you presented here assumes that the second date is with respect to the Java epoch. If you do want to start with year "0", then you need to account for that (by subtracting your epoch time).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 08:30

    tl;dr

    LocalDateTime later = 
        LocalDateTime
        .parse ( 
            "2010-01-14 19:16:17"
            .replace ( " " , "T" ) 
        )
        .plus( Period.parse ( "P10M3D" ) )
        .plus( Duration.parse ( "PT1H10M5S" ) ) 
    ;
    

    ISO 8601

    The representation of a span-of-time using the same format as a moment is creating confusion. A span is not at all the same as a moment.

    Instead of using YYYY-MM-DD HH-MM-SS format for a span of time, I suggest using the standard ISO 8601 format of PnYnMnDTnHnMnS. In this format, the P marks the beginning (for "Period" presumably) and the T separates the years-month-days portion from the hours-minutes-seconds portion.

    Example values:

    • PT1H30M → One and a half hours.
    • P3Y6M4DT12H30M5S → Three years, six months, four days, twelve hours, thirty minutes, and five seconds.
    • P10M3DT1H10M5S → Your Question’s duration of 0000-10-03 01:10:05.

    java.time

    The Question and the other Answers use troublesome old date-time classes now outmoded by the java.time framework built into Java 8 and later. See Oracle Tutorial. Much of the java.time functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

    The java.time classes use ISO 8601 formats by default when parsing and generating Strings that represent date-time values.

    The Question does not provide any time zone info, so here we use the LocalDateTime class. If we know an offset-from-UTC we would use the OffsetDateTime class, and if even better we knew a time zone, we would use the ZonedDateTime class.

    Spans of time in java.time are divided amongst a pair of classes. Years-months-days are represented by the Period class, and hours-minutes-seconds are handled by the Duration class.

    Combining these times, we can indeed perform date-time math. Here we add a span of time to an starting date-time to get a resulting date-time. And we do so in very few lines of code. The result is indeed that expected by the Question.

    We convert the input strings to canonical ISO 8601 format by replacing the SPACE in the middle with a T.

    LocalDateTime ldt = LocalDateTime.parse ( "2010-01-14 19:16:17".replace ( " " , "T" ) );
    //"0000-10-03 01:10:05"
    Period period = Period.parse ( "P10M3D" );
    Duration duration = Duration.parse ( "PT1H10M5S" );
    LocalDateTime result = ldt.plus ( period ).plus ( duration );
    

    Compare to the result expected in the Question.

    LocalDateTime expectation = LocalDateTime.parse ( "2010-11-17 20:26:22".replace ( " " , "T" ) );
    Boolean isSame = result.equals ( expectation );
    

    Dump to console.

    System.out.println ( "ldt: " + ldt + " + period: " + period + " + duration: " + duration + " is result: " + result + " compared to expectation: " + expectation + " is the same: " + isSame );
    

    ldt: 2010-01-14T19:16:17 + period: P10M3D + duration: PT1H10M5S is result: 2010-11-17T20:26:22 compared to expectation: 2010-11-17T20:26:22 is the same: true

    0 讨论(0)
提交回复
热议问题