I am trying to replace org.joda.time.Period with java.time.
We have the following values stored in DB.
P1M, P1Y, P1D, PT1H, PT1M
Just to parse this value:
Period monthly = ISOPeriodFormat.standard().parsePeriod(<One of the above value from DB>);
This is simple and getting Period as expected. But, now replacing with java.time is troublesome.
Because, P1D, P1M, P1Y
should be parsed using the below code:
java.time.Period period = java.time.Period.parse("P1M");
And, P1H and P1D
should be parsed using the below code.
Duration dailyD = Duration.parse("P1D");
So, I might need some string check also like:
if(value.startsWith("PT")) {
// Use java.time.Duration
} else {
// Use java.time.Period
}
Is there any better code for this logic?
Also, Finally, I will have to find the number of occurances from some startTime to till date based on the above java.time.Period or java.time.Duration.
Like, if startDateTime is 01/01/2015 08:30
:
LocalDateTime startDateTime = // the above startDateTime ..
if(value.startsWith("PT")) {
// Use java.time.Duration
ChronoUnit.SECONDS.between(startDateTime,curentDate)/(duration.toMillis()/1000)
} else {
if(value.endsWith("Y")) {
// Use java.time.Period
ChronoUnit.YEARS.between(startDateTime,curentDate)/(period.getYears()/1000)
} else if(value.endsWith("M")){
ChronoUnit.MONTHS.between(startDateTime,curentDate)/(period.getMonths()/1000)
}
Is there any other better way without this value parsing?
My input could have P2M, P10M, P2Y, PT15M, PT10M. It won't have the combination of both Period and time like P1MT10M. But any number of months, years or days possible.