As can be found on the English Wikipedia’s article about ISO 8601 (Date and time — Representations for information interchange):
- P is the duration designator (for period) placed at the start of the duration representation.
- Y is the year designator that follows the value for the number of years.
- M is the month designator that follows the value for the number of months.
- W is the week designator that follows the value for the number of weeks.
- D is the day designator that follows the value for the number of days.
- T is the time designator that precedes the time components of the representation.
So P means 'Period', and because there are no 'date'-components. It only has a 'time'.
You could interpret this as 'Period of Time'.
The 'why' this was chosen, you have to ask the ISO members that wrote the standard, but my guess is that it is easier to parse (short and unambiguous).
The details for the time component are:
- H is the hour designator that follows the value for the number of hours.
- M is the minute designator that follows the value for the number of minutes.
- S is the second designator that follows the value for the number of seconds.
The value of PT20S then parses to:
- Period
- Time
- 20
- Seconds.
So, a duration of 20 seconds.
More examples can be found in the Java 25 documentation.
Addendum: Wikipedia gives a reason for T:
To resolve ambiguity, "P1M" is a one-month duration and "PT1M" is a one-minute duration (note the time designator, T, that precedes the time value).
Answer from Rob Audenaerde on Stack OverflowAs can be found on the English Wikipedia’s article about ISO 8601 (Date and time — Representations for information interchange):
- P is the duration designator (for period) placed at the start of the duration representation.
- Y is the year designator that follows the value for the number of years.
- M is the month designator that follows the value for the number of months.
- W is the week designator that follows the value for the number of weeks.
- D is the day designator that follows the value for the number of days.
- T is the time designator that precedes the time components of the representation.
So P means 'Period', and because there are no 'date'-components. It only has a 'time'.
You could interpret this as 'Period of Time'.
The 'why' this was chosen, you have to ask the ISO members that wrote the standard, but my guess is that it is easier to parse (short and unambiguous).
The details for the time component are:
- H is the hour designator that follows the value for the number of hours.
- M is the minute designator that follows the value for the number of minutes.
- S is the second designator that follows the value for the number of seconds.
The value of PT20S then parses to:
- Period
- Time
- 20
- Seconds.
So, a duration of 20 seconds.
More examples can be found in the Java 25 documentation.
Addendum: Wikipedia gives a reason for T:
To resolve ambiguity, "P1M" is a one-month duration and "PT1M" is a one-minute duration (note the time designator, T, that precedes the time value).
Java has taken a subset of the ISO 8601 standard format for a duration. So the “why” is why the standard was written the way it is, and it’s a guessing game. My go is:
Pfor period was chosen so that you can distinguish a duration from a date and/or time. Especially since a period may also be written in the same format as a local date-time, for exampleP0003-06-04T12:30:05for 3 years 6 months 4 days 12 hours 30 minutes 5 seconds, thePcan be necessary to distinguish. ThePalso gives a little but quick and convenient bit of validation in case you happen to pass a completely different string in a place where a duration was expected. And yes,PT10Slooks weird, but once you get accustomed to it, you recognize it immediately as a duration, which can be practical.Tfor time between the date part and the time part was chosen for two reasons:- For consistency with date-time strings that have
Tin the same place, for example2018-07-04T15:00for July 4, 2018 at 15:00 hours. - To disambiguate the otherwise ambiguous
Mfor either months or minutes:P3Munambiguously means 3 months whilePT3Mmeans 3 minutes.
- For consistency with date-time strings that have
Add a Double variable representing minute into a Date in Java - Stack Overflow
Conversion of milliseconds duration to ISO 8601 precise string format for duration in Java - Stack Overflow
My Quest for 345s
345s
tl;dr
Instant.parse( "2016-12-23T01:23:45Z" )
.plusNanos( Math.round(
yourNumberOfMinutesAsDouble *
TimeUnit.MINUTES.toNanos( 1 ) )
)
Avoid old date-time classes.
Actually, you should using neither java.util.Date nor java.util.Calendar. These old legacy classes are notoriously troublesome, confusing, and flawed. Now supplanted by the java.time classes.
For the old classes, the Answer by Jireugi is correct. For java.time classes, read on.
Avoid using a fractional number for elapsed time
Not a good idea using a double or Double to represent elapsed time. Firstly, that type uses floating-point technology which trades away accuracy for speed of execution. So you will often have extraneous extra digits to the right of your decimal fraction.
Secondly, this is an awkward way to handle time, given that we have sixty seconds to a minute, and sixty minutes to an hour, and so on.
- In Java, use the
PeriodorDurationclasses. See Oracle Tutorial. - In text, use the standard ISO 8601 format
PnYnMnDTnHnMnSwherePmarks the beginning andTseparates the years-months-days from the hours-minutes-seconds. So two and a half minutes would bePT2M30S.
Nanoseconds
If we must work with the Double as a number of minutes for elapsed time, let’s convert from your fractional decimal number to a whole number (integer).
Note that the old legacy date-time classes are limited to a resolution of milliseconds while the java.time classes can go as fine as nanoseconds. So we want to convert your Double to a whole number of nanoseconds.
Rather than hard-code a magic number of the number of nanoseconds in a minute for this calculation, let's use the TimeUnit enum. That class can calculate the number of nanoseconds in a minute for us ( 60 * 1_000_000_000L ).
Finally, the Math.round function returns the closest long to the argument, with ties rounding to positive infinity.
long nanoseconds = Math.round(
yourNumberOfMinutesAsDouble *
TimeUnit.MINUTES.toNanos( 1 )
);
Instant
If working with date-time values in UTC, use the Instant class. Each Instant represents a moment on the timeline in UTC with a resolution of nanoseconds.
Instant Instant.parse( "2016-12-23T01:23:45Z" )
Instant future = Instant.plusNanos( nanoseconds );
Duration
Rather than passing around a Double as your elapsed time, I strongly suggest you pass around Duration objects.
Duration duration = Duration.ofNanos( Math.round( yourNumberOfMinutesAsDouble * TimeUnit.MINUTES.toNanos( 1 ) ) );
You can do math with a Duration such as plus and minus.
Instant instant = Instant.parse( "2016-12-23T01:23:45Z" )
Instant future = instant.plus( duration );
You can generate a String representation of the Duration in standard ISO 8601 format such as PT8H6M12.345S by merely calling toString. And Duration can parse such strings as well.
String output = duration.toString(); // PT8H6M12.345S
…or going the other direction…
Duration duration = Duration.parse( "PT8H6M12.345S" );
You could try this using Math.round:
Date clock;
SimpleDateFormat reqDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
clock = reqDF.parse(line[25]);
Double distance = distDB.find_distance(O, D, mode, facility_id);
long clockTimeMs = clock.getTime();
long distTimeInMs = Math.round(distance * 60000);
Date current_time = new Date(clockTimeMs + distTimeInMs);
Here the Date gets converted into a milliseconds timestamp. Since the distance is in minutes, you need to convert it also into milliseconds by multiplying it by 60 * 1000 (= 60000) before you can add it to the start time ("clock"). Finally a new Date gets created that represents the distance from the start time.
Please find details of the Date class here: https://docs.oracle.com/javase/6/docs/api/java/util/Date.html
Try:
String iso = Duration.ofMillis(millis).toString();
toString() returns the duration using the ISO-8601 seconds based representation
The Duration#toString() method returns
A string representation of this duration using ISO-8601 seconds based representation, such as
PT8H6M12.345S.
Use any of the various factory methods to create a Duration instance, then invoke its toString method to get a proper representation.
Rear shots
I got it free. its compact and fits my needs. plugged it in and got power but then started getting smoke and the burnt wire smell from by the pulse button. gonna take apart the head unit but anyone have any ideas?