0

I know how to express an amount of days in Java by using the java.time.Period class. But how can I express an specific time period in history, like "the seven days between March 23, 2012 and March 30 2012", and not just "seven days"?

To illustrate, in the code bellow p1 and p2 are created as distinct "time periods in history", both spanning exactly seven days but with the former in March 2012 and the later in April 2014.

    java.time.Period p1 = Period.between(
        LocalDate.of(2012, Month.MARCH, 23),
        LocalDate.of(2012, Month.MARCH, 30));

    java.time.Period p2 = Period.between(
        LocalDate.of(2014, Month.APRIL, 1),
        LocalDate.of(2014, Month.APRIL, 8));

As instances of java.util.Period, they are nevertheless equals to each other

    //returns true!
    boolean isTheSamePeriod = p1.equals(p2);

What would be the class to represent a certain amount of days from an specific starting date?

bruno
  • 2,213
  • 1
  • 19
  • 31
  • 1
    sounds easy enough to write yourself. – Scary Wombat Apr 06 '16 at 02:49
  • JodaTime has `Interval`, but that does not seem to have made it into java.time. – Thilo Apr 06 '16 at 02:56
  • 1
    @ScaryWombat: When it comes to anything date/time-related, that's probably not the case. Too many crazy edge-cases :-) – Thilo Apr 06 '16 at 02:57
  • Case in point re "sounds easy": "JDK 8 JSR-310 does not have an Interval class. The concept of intervals was descoped to ensure that the rest of the library could be completed." – Thilo Apr 06 '16 at 03:00
  • What you are looking for is not even content of Joda-Time because Joda-interval relates to instants not to calendar dates. And of course, Java-8 does not contain a date interval class, too. But maybe my library Time4J containing the class [DateInterval](http://time4j.net/javadoc-en/net/time4j/range/DateInterval.html) is interesting for you. By the way, `Period` is not an interval because it is NOT anchored on the date- or timeline as you already noticed. – Meno Hochschild Apr 06 '16 at 06:58
  • Isn’t the most straight-forward way to express “'time periods in history” just keeping the start and end date? Of course, if you want to sort time periods, you have to decide whether you want to sort by start, end or duration… – Holger Apr 06 '16 at 10:52
  • @Holger Yes, it is. Do we have a specific class for that in Java? (spoiler: no. According to answers to previous question.) – bruno Apr 09 '16 at 06:07

0 Answers0