8

i want to replace JodaTime by Java 8 DateTime API.

I've got ISO-8601 period described = P2W5DT11H8M

In JodaTime i parse it very simply by executing the following code:

Period.parse("P2W5DT11H8M") and i get the successful Period object.

Can i do the same in Java 8?

assylias
  • 321,522
  • 82
  • 660
  • 783
ServerSideCat
  • 1,992
  • 3
  • 18
  • 24
  • And what is your intention regarding the result? Do you plan to add the period to a `LocalDateTime`? If so then the addition algorithm implicitly suggested by the answer of Assylias (separate additions in two steps) is not exactly what Joda-Time defines and might yield different results in some edge cases. Furthermore, there are no formatting capabilities in Java-8 for periods/durations so you might think again about your decision. – Meno Hochschild Jul 16 '15 at 10:39
  • 1
    @MenoHochschild i want to have the same Period as it was in JodaTime. Currently, i see that i'm losing the data, for example i'm not able to operate minutes, instead of it i should use extra implementation from Java 8 - the Duration class. – ServerSideCat Jul 16 '15 at 11:58

3 Answers3

5

A Period in Java 8 only has year/month/day components. A Duration has hour/minute/second components. It seems that you will need to parse the string manually. One option could look like the code below (you need to add input validation etc.) - there may be better alternatives.

public static void main(String[] args) {
  System.out.println(PeriodAndDuration.parse("P2W5DT11H8M"));
}

public static class PeriodAndDuration {
  private final Period p;
  private final Duration d;

  public PeriodAndDuration(Period p, Duration d) {
    this.p = p;
    this.d = d;
  }

  public Period getPeriod() {
    return p;
  }

  public Duration getDuration() {
    return d;
  }

  public static PeriodAndDuration parse(String input) {
    int periodStart = input.indexOf("P");
    int timeStart = input.indexOf("T");
    Period p = Period.parse(input.substring(periodStart, timeStart));
    Duration d = Duration.parse("P" + input.substring(timeStart, input.length()));
    return new PeriodAndDuration(p, d);
  }

  @Override
  public String toString() {
    return p.toString() + d.toString();
  }
}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • Thanks for the answer. So, as far as i understand, parsing such full string i should be responsible and think that i can parse only year/month/day from it. – ServerSideCat Jul 15 '15 at 11:00
  • 1
    As far as I know yes - somebody else may come up with a better answer. – assylias Jul 15 '15 at 11:05
  • This kind of combination has already been written in [`org.threeten.extra.PeriodDuration`](http://www.threeten.org/threeten-extra/apidocs/org/threeten/extra/PeriodDuration.html). See [my Answer](https://stackoverflow.com/a/50809751/642706) for details. – Basil Bourque Jun 12 '18 at 05:24
0

PeriodDuration

Period.parse("P2W5DT11H8M")

Can i do the same in Java 8?

Yes.

org.threeten.extra.PeriodDuration.parse( "P2W5DT11H8M" )

ThreeTen-Extra project

The java.time classes built into Java 8 and later are supplemented by the ThreeTen-Extra project. This project is run by the same man as who ran Joda-Time and JSR 310 (the spec for java.time), Stephen Colebourne.

That project offers the PeriodDuration that combines the ideas of the Period and Duration classes in java.time.

Documentation for PeriodDuration.parse says:

Obtains an instance from a text string such as PnYnMnDTnHnMnS.

PeriodDuration pd = PeriodDuration.parse( "P2W5DT11H8M" ) ;

Caveat

Think twice before using PeriodDuration.

The two concepts of years-months-days and hours-minutes-seconds were separated in java.time for a reason. If you give it a ponder, you may find that it may not make sense to combine the two in practice.

If you do proceed with this class, be sure to study the documentation and its behavior. Practice with some scenarios to see if it meets your expectations and needs.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-4

Short answer:

Period period = Period.parse("P2W5DT11H8M");

Long answer: There is a good description by Oracle on when to use Period or Duration. It comes with some nice code-samples, and should answer any follow-up questions you might have.

Stern
  • 130
  • 1
  • 8