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.