0

Okay, I have the following scenario: if I have a string, for example 5d6m9s, how would I make Java recognize that as 5 days, 6 minutes and 9 seconds, and then adding that onto the current milliseconds to get the milliseconds in 5 days, 6 minutes, and 9 seconds. Would substringing be the best method?

I suck at explaining so here it is simplified: 5d6m9s -> milliseconds + current milliseconds -> milliseconds in 5 days, 6 minutes and 9 months.

5d6m9s is just an example, the string could be anything like 2w9h4m or 3h7m.

Thanks in advance.

Arraying
  • 169
  • 2
  • 3
  • 10
  • 2
    And what have you tried doing? – UnholySheep Oct 02 '16 at 17:02
  • @UnholySheep I haven't tried anything, as I have no clue on how I would go about with this. – Arraying Oct 02 '16 at 17:03
  • I'm not sure thats a standard format or anything (or you could just use a library). You need to write some code that parses those strings into separate variables; int days, int minutes, int seconds, etc.. then you can multiple each one out (to get milliseconds), and add them together. – trooper Oct 02 '16 at 17:04
  • Then I would recommend you try doing something, you shouldn't just expect people to solve your problems for you. Personally I'd first try to extract the information from the string (using RegEx, but there might be better ways), convert the numbers to `int` and then do the math to turn them into milliseconds. – UnholySheep Oct 02 '16 at 17:05
  • @UnholySheep Do you know a neat site that explains how regex works, as I'm not exactly experienced in it, and would sub-stringing also work> – Arraying Oct 02 '16 at 17:08
  • 3
    [An open letter to students with homework problems](http://meta.programmers.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) – Joe C Oct 02 '16 at 17:09
  • @JoeC This isn't my homework, it's something I'd need to look into for my software. – Arraying Oct 02 '16 at 17:16
  • 1
    In that case, you should spend some time thinking about it, sleep on it if need be, and actually try something. If you run into a problem during the "try something" bit, then you can come back and ask for help. – Joe C Oct 02 '16 at 17:17
  • Regular Expressions are a rather comprehensive topic, so I can't give you anything with a simple explanation (but you can google [it](http://www.tutorialspoint.com/java/java_regular_expressions.htm)). I'm not sure how exactly you would go about solving this using substrings, as you have a lot of different possibilites of how the string may look like. I guess you could try iterating over every single character of the string and somehow extract the information that way – UnholySheep Oct 02 '16 at 17:19
  • FYI, these kinds of strings are formally defined by the [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) standard in the format `PnYnMnDTnHnMnS`, and used by default in the java.time classes such as `Period` and `Duration` ([Tutorial](http://docs.oracle.com/javase/tutorial/datetime/iso/period.html)) – Basil Bourque Oct 02 '16 at 20:36

1 Answers1

1

ISO 8601

If not homework, then use the standard ISO 8601 formats rather than devising your own.

The ISO 8601 standard includes the format PnYnMnDTnHnMnS for a span of time not attached to the timeline. The P marks the beginning and the T separates years-month-days from hours-minutes-seconds.

Examples:

  • P4Y is four years.
  • P1M15D is a month and half.
  • PT1H30M is an hour and a half.

java.time

The java.time classes supplant the troublesome old legacy date-time classes. The java.time classes use ISO 8601 formats by default when parsing and generating strings that represent date-time values.

Two classes represent a span of time unattached to the timeline. See Oracle Tutorial.

Search Stack Overflow to learn more, as your Question is really a duplicate of many others. Especially important to learn the limitations in java.time on this feature: Formatting a Duration in Java 8 / jsr310

You may want to track a span of time that is attached to the timeline. Use a pair of Instant objects for that. The ThreeTen-Extra project provides a java.time-savvy Interval class for this purpose.

You can add or subtract a Period or Duration to/from a moment.

Instant.now()
       .plus( Duration.parse( "PT5H" ) );  // Add 5 hours
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154