Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a duration format which is like 0DT3H10M. So need to know how to parse this kind of data.
I want 0 Days 3 Hours and 10 Minutes from 0DT3H10M
in a specific format.
We can manually parse it by a character which is working fine but is there any other way or library available for this in android/java?
Duration
I will guess that string represents a duration of three hours and ten minutes.
Unfortunately that string fails to comply with the ISO 8601 standard used by default in the java.time classes Duration and Period. The standard starts all such strings with a P. And the standard separates any years-months-days from any hours-minutes-seconds with a T. So your input of three hours and ten minutes would be PT3H10M.
You will need to parse the string with your own code. Then use the extracted values to set the value of a java.time.Duration object.
You may be able to get away with simply prepending a P to comply with the standard. I hesitate to recommend this only because you would need to see the range of possible values you might receive to verify this approach would work.
Duration.parse( "P" + "0DT3H10M" )
Tip: Educate the publisher of your input data about ISO 8601.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am doing a work for the university and I have to read many lines from file a with that format:
3ld4R7 4:27 3475
Everything is correct, each line represents a song, the first string is the name, the second the duration and the third the popularity. However, I don't know exactly what type I can choose for the time. Then, I have to do many operations with the time (minutes, seconds, hours). I don't know if there is a class in Java libraries for that such as Time or something like that. Any help is thanked!!!
java.time.Duration
The Duration class of java.time, the modern Java date and time API, is the class for — well, the name says it already. Unfortunately parsing a string like 4:27 into a Duration is not built-in. My preferred trick is:
String durationString = "4:27";
String isoString = durationString.replaceFirst("^(\\d+):(\\d+)$", "PT$1M$2S");
Duration dur = Duration.parse(isoString);
System.out.println(dur);
Output:
PT4M27S
Read as a period of time of 4 minutes 27 seconds. The Duration.parse method requires a format known as ISO 8601, an international standard. And Duration.toString(), implicitly called when we print the Duration, produces ISO 8601 back. It goes like what you saw, PT4M27S. So in my code, the first thing I do is convert your input from the file to ISO 8601 format, which I then parse.
If you want to format the duration for display, for example back in the same format as in the file:
System.out.format("%d:%02d%n", dur.toMinutes(), dur.toSecondsPart());
4:27
Links
Oracle tutorial: Date Time explaining how to use java.time.
Wikipedia article: ISO 8601
There is such a library in Java and more than one.
Try java.util.Date library and SimeplDateFormatter class to parse the date-time objects in a specific way according to the strings.
For example:
DateFormat formatter = new SimpleDateFormat("MM/dd/yy h:mm a");
Date date = (Date)formatter.parse(date)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I've noticed that the various Java time parse methods (such as ZonedDateTime.parse(...)) consistently use the relevant portions of 2007-12-03T10:15:30+01:00[Europe/Paris] as the example in their Javadocs (with the exception of Instant which uses UTC as the time zone).
Obtains an instance of ZonedDateTime from a text string such as 2007-12-03T10:15:30+01:00[Europe/Paris].
Class
Example
Instant
2007-12-03T10:15:30.00Z
LocalDate
2007-12-03
LocalDateTime
2007-12-03T10:15:30
LocalTime
10:15
MonthDay
--12-03
OffsetDateTime
2007-12-03T10:15:30+01:00
OffsetTime
10:15:30+01:00
Year
2007
YearMonth
2007-12
ZonedDateTime
2007-12-03T10:15:30+01:00[Europe/Paris]
I realize this might just be an arbitrary date, but I've found in the past that oftentime the example values have additional meaning that help my understanding of the overall domain, beyond being just an example.
Is there any particular significance to this datetime, and why was it chosen as the example parse value for the Java time API?
I'm looking specifically for something that can be backed up with something concrete (e.g. official implementation discussions or statements by those involved in the library creation).
No special meaning
No, there is no special meaning to that example date-time value. Date-time handling is tricky enough, do not distract yourself with such trivial detail.
Technical writers commonly work with the same example data across scenarios for consistency, to most easily make apparent the similarities and contrasts.
The value may have personal significance to the original author. But as Arvind Kumar Avinash commented, what matters here is the formats rather than the value.
2007-12-03T10:15:30.00Z is not really an ideal example. I would have chosen a day-of-month larger than 12 to distinguish from the month number. And I would have chosen an hour larger than 12 to make obvious the 24-hour clock (0-23).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm writting a own application to help RuneScape 3 streamers educate their viewers with a DPS Rotation showing, and i'm wanting to implement a feature A.K.A APM (Which means Actions per minute) which is in other words, number of Keys (hotkeys) pressed within a minute, what i'm doing atm is, having a LocalTime when the thread starts, and every 5 secs it should show APM, but I guess my formula isn't correct.
Current formula is:
LocalTime apm = Main.keysPressed.size() / (LocalTime.now().minus(Main.apmLocalTime);
Which is:
counter / (current_time - start_time)
Problem is, the .minus() asks for a TemporalUnit as parameter, and i'm quite lost.
Can someone plz help me getting the formula.
tl;dr
actionsCount / Duration.between( start , Instant.now() ).toMinutes()
java.time.Instant
Use Instant to track a moment, not LocalTime.
Instant represents a moment, a point on the timeline, as seen in UTC. The class resolves to nanoseconds, but current conventional hardware clocks limit capturing the current moment to microseconds or milliseconds.
The LocalTime class represents merely a time-of-day without the context of a date and time zone or offset-from-UTC. So this class cannot represent a moment.
Use Duration class for ease, and to make your code more self-documenting.
Instant start = Instant.now() ;
…
Duration elapsed = Duration.between( start , Instant.now() ) ;
long minutesElapsed = elapsed.toMinutes() ; // Get a count of whole minutes in total span of time.
long actionsPerMinute = ( actionsCount / minutesElapsed ) ;
Tip: While generally in Java we want to use the more general interfaces and superclasses rather than the more specific concrete classes, in java.time the opposite is true. In java.time we want to use the specific concrete classes, because the framework programmers told us so in the documentation. So if your IDE or compiler suggests a Temporal, for example, follow the Javadoc to see the list of implementing classes, such as Instant.
This question already has answers here:
SimpleDateFormat with TimeZone
(6 answers)
Closed 4 years ago.
Currently we are using
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")
To format the time range to an external vendor for a range of data, for India, that formatter will give time like this:
2018-04-26T00:00:00.000+0530
However, my vendor say they cannot accept this format and it have to look like
2018-04-26T00:00:00.000+05:30
However, look like in DateTimeFormatter, whatever I choose Z/z/X/x, I don't get that format of offset. Just wonder is that a way to customize the offset to be HH:mm?
Or, I need to get the offset in second and work that our myself?
It is three x. Just tried with JavaRepl:
java.time.format.DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSxxx")
.withZone(java.time.ZoneId.systemDefault())
.format(java.time.Instant.now())
Results in
java.lang.String res10 = "2018-04-27T11:06:50.648+00:00"
After some trial and error, I saw that this is also documented in the API documentation of DateTimeFormatter but it is not easy to find (buried in a lot of other text):
Three letters outputs the hour and minute, with a colon, such as '+01:30'
DateTimeFormatter API Documentation
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
How can I convert the current time in milliseconds, which is a Long, to a date in specific format?
The format that I need is yyyy-MM-dd HH:mm. This should be of type Date, not String.
You are confused. The type Date is a number of milliseconds since January 1 1970 midnight UTC. It has no inherent format. There is a default system format for a Date, but you cannot alter it. You will need to format your Date as a String if you need that particular String format.