Parse ISO date by Period in Java 8

雨燕双飞 提交于 2019-12-05 03:13:19

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!