17

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

Jonnyr612
  • 187
  • 1
  • 1
  • 8
  • 1
    Answered already here http://stackoverflow.com/questions/25747499/java-8-calculate-difference-between-two-localdatetime – JynXXedRabbitFoot Nov 08 '16 at 15:18
  • If you meant this Question to refer to Joda-Time classes, then you should say so explicitly. Also, the Joda-Time project is now in maintenance mode and advised migration to the java.time classes. – Basil Bourque Nov 09 '16 at 06:39

2 Answers2

46

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());
Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
  • The OP obviously uses Joda-Time, not Java-8-time-api, see the usage of Period-constructor.. – Meno Hochschild Nov 08 '16 at 18:07
  • Thanks, however it keeps returning POD, is this caused by the same date, as this is only toLocalDate, as opposed to the time included? – Jonnyr612 Nov 08 '16 at 18:08
  • @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. – Meno Hochschild Nov 08 '16 at 18:13
  • @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. – Olivier Grégoire Nov 08 '16 at 18:50
8

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.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126