2

Is there any libraries that is able to take a string such as 5d 1h 2m 15s and add it to a java date / java Calendar?

ie a system property will be set as 5d 1h 2m 15s

We will read in the system property and add this amount of time to the current date.

Otherwise I will have to implement this as a long in milliseconds.

Coder
  • 2,153
  • 1
  • 16
  • 21
IanWatson
  • 1,649
  • 2
  • 27
  • 49
  • 2
    Hey take a look here: http://stackoverflow.com/questions/6403851/parsing-time-strings-like-1h-30min – Emax Oct 13 '16 at 15:08
  • Also see "Parsing Strings into Dates" at https://www.tutorialspoint.com/java/java_date_time.htm – Ben I. Oct 13 '16 at 15:11
  • **Search Stack Overflow** for *ISO 8601, duration, Period, and PnYnMnDTnHnMnS*. This topic has been addressed many times. The java.time classes have support for both parsing and generating such strings. – Basil Bourque Oct 13 '16 at 16:38

1 Answers1

0

you'll need a bit of parsing to extract the component element but Joda-Time's duration should help you out

http://joda-time.sourceforge.net/apidocs/org/joda/time/Duration.html

If you are using Java8 joda-time's concept were integrated so no need to external dependencies

https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html

once you have a duration adding it to the current date time should be trivial

Java8 and jodaTime

Instant ajustedTime = Instant.now().plus(yourDurationInstance);

you can convert to and from Java's date pretty easily, others have already answered here

Community
  • 1
  • 1
Newtopian
  • 7,543
  • 4
  • 48
  • 71