ServerSideCat
ServerSideCat

Reputation: 2062

Parse ISO date by Period in Java 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?

Upvotes: 8

Views: 1773

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 338785

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.

Upvotes: 0

assylias
assylias

Reputation: 328639

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();
  }
}

Upvotes: 6

Stern
Stern

Reputation: 130

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.

Upvotes: -4

Related Questions