Java Subtracting LocalDateTime foo from LocalDateTime Bar

22,164

Solution 1

Why do you not simply use directly LocalDateTime as parameter type this way:

LocalDateTime start = new LocalDateTime(2016, 10, 4, 17, 45);
LocalDateTime end = LocalDateTime.now();
PeriodType ptype = PeriodType.yearMonthDayTime();
Period diff = new Period(start, end, ptype);

See also the API of Joda-Time. You don't need to cast to DateTime.

Solution 2

The question is about Joda-Time, not about Java 8. The question wasn't really clear about it. Anyways, this answer is about Java 8 and therefore doesn't directly answers the question. Though I think it's an appropriate answer if anyone wants to use Java 8 instead.

You should probably check the java.time.Duration class.

LocalDateTime from = ... ;
LocalDateTime to = ... ;
Duration duration = Duration.between(from, to);

If you really want to obtain a Period, it's still rather easy:

LocalDateTime from = ... ;
LocalDateTime to = ... ;
Period period = Period.between(from.toLocalDate(), to.toLocalDate());
Share:
22,164
Jonnyr612
Author by

Jonnyr612

Updated on November 09, 2020

Comments

  • Jonnyr612
    Jonnyr612 over 3 years

    I know this is probably a really stupid question, however I was wondering whether there is a way to minus a local date time from another, or if not, to cast localDateTime to just Date time and then subtract from there?

    I need to be able to work out the date time difference, between the start of record foo and the start of record bar (which is also the end of Foo)

    I am rather new to java, and for simplicity's sake, I want to be able to keep it in roughly the same layout, rather than having to convert from seconds etc.

    If adding and subtracting LocalDateTime is not an option, I know that once I am in dateTime I am able to subtract them using:

    Period diff = new Period(start, end);
    

    Due to the purpose of the data, I need to keep both the date and the time in order for this to work, and as mentioned before, I wish to keep it fairly simple so theres no need to convert between seconds back to the date.

    My issue is really just getting it in an acceptable format, as I say, I am rather new to java and object orientation as a whole, so please don't ridicule me for this, seemingly simple, question.

    Thanks,

    Jonny

  • Meno Hochschild
    Meno Hochschild over 7 years
    The OP obviously uses Joda-Time, not Java-8-time-api, see the usage of Period-constructor..
  • Jonnyr612
    Jonnyr612 over 7 years
    Thanks, however it keeps returning POD, is this caused by the same date, as this is only toLocalDate, as opposed to the time included?
  • Meno Hochschild
    Meno Hochschild over 7 years
    @Jonnyr612 In contrast to Joda-Time or other APIs, Java-8 (java.time-package) does NOT manage durations consisting of date and time units together.
  • Olivier Grégoire
    Olivier Grégoire over 7 years
    @MenoHochschild Obviously? No. Several class names are the same, no joda-time tag but after more scrutiny, you are right, the question is not about Java 8.