I have monitored my java program using jconsole recently. I saved CPU usage data as a csv file. This is what I've got:
Time,CPU Usage
43690.008014,1,8
43690.008060,0,1
43690.008106,0,1
43690.008153,0,1
43690.008199,0,1
43690.008245,0,1
The CPU Usage column is clear, but I cannot say the same thing about the Time column. What is 43690.008014? How can I parse it into Date? I haven't seen anything like this in my life.
The duration recorded in the CSV file is the number of days since 1899-12-311. In order to get the current date, you can add this duration to LocalDate.of(1899, 12, 30).atStartOfDay().atOffset(ZoneOffset.UTC). I recommend you use java.time.Duration which is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation. The function, Duration#ofNanos gives you a Duration representing the specified number of nanoseconds. The reason why I am recommending you to use this function despite the fact that there is already a function, Duration#ofDays is that these functions take a long value as the argument and if you cast the duration in your log file (e.g. 43690.008014) to long, its fractional part will be lost giving you an incorrect result.
Therefore, convert these days to nanoseconds, get Duration from the resulting nanoseconds and add the same to LocalDate.of(1899, 12, 30).atStartOfDay().atOffset(ZoneOffset.UTC) to get the current date and time in UTC.
Demo:
import java.time.Duration;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
OffsetDateTime startDateTime = LocalDate.of(1899, 12, 30).atStartOfDay().atOffset(ZoneOffset.UTC);
OffsetDateTime current = startDateTime
.plus(Duration.ofNanos((long) (43690.008014 * 24 * 60 * 60 * 1000_000_000)));
System.out.println(current);
}
}
Output:
2019-08-13T00:11:32.409600512Z
Learn about the modern date-time API from Trail: Date Time.
For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7.
If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
1 Why is 1899-12-30 the zero date in Access / SQL Server instead of 12/31?
The time format is daysSinceEpoch.fractionOfCurrentDay
The daysSinceEpoch starting at 1899-12-30 instead of 1900-01-01
The fractionOfCurrentDay is a millionth of the current day, ranging from zero to 999999 (23:59:59)
You can use the following function to convert to LocalDateTime:
public static LocalDateTime convert( final String jconsoleDateFormat )
{
String[] split = jconsoleDateFormat.split( "\\." );
long daysSinceEpoch = Long.parseLong( split[0] );
long dayFraction = Long.parseLong( split[1] );
LocalDateTime epochDate = LocalDateTime.of( 1899,12,30, 0,0 );
LocalDateTime currentDate = epochDate.plusDays( daysSinceEpoch );
long secondsADay = 24 * 60 * 60L; // 86_400
long secondsSinceDayStarted = secondsADay * dayFraction / 1_000_000;
return currentDate.plusSeconds( secondsSinceDayStarted );
}
public static void main( String[] args )
{
System.out.println( convert( "43690.008014" ) ); // 2019-08-13T00:11:32
}
Related
I have a 5 minutes timer. In case i finish 30 seconds its shown 4:30 but i want set 30 seconds .
code to decrease time
String timeReminder= String.format(Locale.ENGLISH , "%02d:%02d" , TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) , TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)) );
timerText.setText(timeReminder);
i want only reminder time.
java.time
You can use java.time.Duration which is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation. With Java-9 some more convenience methods were introduced.
import java.time.Duration;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
Duration total = Duration.ofMinutes(5);
Duration elapsed = Duration.ofSeconds(30);
Duration remaining = total.minus(elapsed);
// ###############Java 8###########################
String timeReminder = String.format(Locale.ENGLISH, "%02d:%02d", remaining.toMinutes(),
remaining.toSeconds() % 60);
System.out.println(timeReminder);
// ################################################
// ###############Java 9###########################
timeReminder = String.format(Locale.ENGLISH, "%02d:%02d", remaining.toMinutesPart(), remaining.toSecondsPart());
System.out.println(timeReminder);
// ################################################
}
}
Output:
04:30
04:30
Learn more about the modern date-time API from Trail: Date Time.
For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7.
If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Ok, I guess the word you are looking for is elapsed time, however, your logic doesn't seem correct.
So here is the example,
Long startTime = System.currentTimeMillis();
Long estimatedTime = TimeUnit.MINUTES.toMillis(10); // For 10 minutes
To calculate elapsed time :
Long elapsedTime = System.currentTimeMillis() - startTime;
To calculate Remaining time :
Long remainingTime = estimatedTime - System.currentTimeMillis();
Now you have both times in Millis, You can easily convert and format in Minutes:Second format.
System.out.println(json.toString());
System.out.println(json.get("date"));
returns time in epoch time such as: 1609642292
> Task :Program:DateUtils.main()
{"date":1609642292}
1609642292
This is what I'm using to pull the date from the API
import java.io.InputStreamReader;
import java.net.URL;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.Date;
import org.json.JSONException;
import org.json.JSONObject;
public class DateUtils
{
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
// InputStream is = new URL(url).openStream();
try (var is = new URL(url).openStream()) {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
}
}
public static void main(String[] args) throws IOException, JSONException {
JSONObject json = readJsonFromUrl("https://Time.xyz/api/date"); //Don't want to post real API
System.out.println(json.toString());
System.out.println(json.get("date"));
}
}
In my other java file I'm trying to do something like
Calendar expiry = Calendar.getInstance();
expiry.set(2021,1,31,0,0) //When my program expires:year, month, date, hour, min
Calendar now = DateUtils.getAtomicTime();
//where DateUtils.getAtomicTime comes from this class that pulls current time from the National Institute of Standards and Technology
//https://www.rgagnon.com/javadetails/java-0589.html
if (now.after(expiry)) {
shutdown()
}else{
startProgram()
}
}
How can I change
Calendar now - DateUtils.getatomicTime() to this new API
My problems:
I don't know how to use what I have to check for the time and refer to it.
Like it prints the time properly, but now how do I use that println jsontostring and then use that to add it to that code slightly above to compare my Set Expiration Date with API date.
Please give me some advice. Thank you.
It appears that the goal is to use the epoch time in the date element of the server response and call shutdown if that is earlier than the current time.
Instead of creating Calendar instances, I would compare the current epoch time to the value in the HTTP response.
if (DateUtils.readJsonFromUrl("https://Time.xyz/api/date").get("date") * 1000 < System.currentTimeMillis()) {
shutdown();
} else {
startProgram();
}
tl;dr
Your Question is not clear. But it seems you want to compare some moment represented as a textual number of whole seconds since 1970-01-01T00:00Z to some number of calendar days past the current moment as captured from a remote time server using some library you’ve not explained.
boolean isFurtherOutIntoTheFuture =
Instant // Represent a moment, a point on the timeline, resolving to nanoseconds, as seen in UTC.
.ofEpochSecond( // Interpret a number as a count of whole seconds since the epoch reference point of 1970-01-01T00:00Z.
Long.parseLong( "1609642292" ) // Parse text as a number, a 64-bit `long`.
) // Returns a `Instant`.
.isAfter( // Compare one `Instant` object to another.
DateUtils // Some mysterious library that fetches current moment from a remote time server.
.getAtomicTime() // Returns a `java.until.Date` object (apparently – not explained in Question).
.toInstant() // Convert from legacy class to its modern replacement.
.atZone( // Adjust from UTC to some time zone. Same moment, different wall-clock time.
ZoneId.of( "Africa/Tunis" ) // Whatever time zone by which you want to add some number of calendar days.
) // Returns a `ZonedDateTime` object.
.plusDays( x ) // Add some number of calendar days (*not* necessarily 24-hours long). Returns a new `ZonedDateTime` object with values based on the original.
.toInstant() // Adjust from some time zone to UTC (an offset-from-UTC of zero hours, minutes, and seconds).
) // Returns a `boolean`.
;
Details
Never use Calendar. That terrible class was supplanted years ago by the modern java.time classes.
Convert your epoch seconds to Instant by calling Instant.ofEpochSecond. Pass a long parsed from your textual input.
Apparently a call to the DateUtils.getAtomicTime, of some library you neglected to mention, results in Java.until.Date. Convert that terrible legacy class to its modern replacement, java.time.Instant. Notice the new to… and from… conversion methods added to the old legacy classes.
Instant now = DateUtils.getAtomicTime().toInstant() ;
Compare to current moment.
boolean isInTheFuture = someInstant.isAfter( now ) ;
You commented about “x amount of days”. Did you mean calendars days or generic chunks of 24-hours? If the latter:
Instant later = myInstant.plus( Duration.ofDays( x ) ) ;
If you meant calendar days, apply a time zone.
ZoneId z = ZoneId.of( "America/Edmonton" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
ZonedDateTime later = zdt.plusDays( x ) ;
Instant laterInUtc = later.toInstant() ;
All of this has been covered many many many times already on Stack Overflow. Search to learn more.
The answer by Basil Bourque guides you in the right direction. This answer is focused on what code you should write.
The class, Instant serves as the bridge between the legacy date-time API and the modern date-time API. Convert the java.util.Calendar object (which you are getting from json.get("date")) to Instant using Calendar#toInstant.
For expiry date, you can create an Instant object using the OffsetDateTime object set with ZoneOffset.UTC.
Finally, you can compare these two objects of Instant using Instant#isAfter.
Based on the explanation given above, you need to write the following code:
JSONObject json = readJsonFromUrl("https://Time.xyz/api/date");
Calendar now = json.get("date");
Instant instantNow = now.toInstant();
Instant expiry = OffsetDateTime.of(LocalDateTime.of(2021, 1, 31, 0, 0), ZoneOffset.UTC).toInstant();
if (instantNow.isAfter(expiry)) {
shutdown();
} else {
startProgram();
}
Learn about the modern date-time API from Trail: Date Time.
Note that the date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API.
For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7.
If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
since Java 8 I can use instant and LocalDateTime
To get the Unix timestamp:
long unixTimestamp = Instant.now().getEpochSecond();
But if I want to get a time from the past, lets say 2 weeks I use this:
int unixtime2weeksAgo = (int) ((System.currentTimeMillis() - 24 * 60 * 60 * 1000 * 14) / 1000L)
With the docu I couldn't build a solution based on the new java 8 features is here someone who could provide a good solution by using instant or LocalDateTime?
"Two weeks ago" is dependent on your time zone (there may have been some DST changes etc.). So using Instant or LocalDateTime may create issues because they don't include any time zone information.
Assuming you want to do it in UTC, you can use:
ZonedDateTime twoWeeksAgo = ZonedDateTime.now(ZoneOffset.UTC).minusWeeks(2);
long unixTs = twoWeeksAgo.toEpochSecond();
You can specify a different time zone in place of ZoneOffset.UTC seen above. For example, ZoneId.of( "Asia/Kolkata" ).
see below:
import java.time.Instant;
import java.time.temporal.ChronoUnit;
public class TimestampExample {
public static void main(String[] args) {
long currentUnixTimestamp = Instant.now().getEpochSecond();
System.out.println(currentUnixTimestamp);
// two weeks ago
long pastTimestamp = Instant.now().minus(14, ChronoUnit.DAYS).getEpochSecond();
System.out.println(pastTimestamp);
}
}
I would like to save some user timezone in a Daylight saving proof format.
My goal is to get the correct GMT offset whenever the code gets executed.
In order to figure out my best option, I wrote the following:
ArrayList<String> list = new ArrayList<String>();
list.add( "EST");
list.add( "EDT");
list.add( "America/New_York");
long now = System.currentTimeMillis();
for( String tzID: list) {
TimeZone tz = TimeZone.getTimeZone( tzID);
System.out.println( tzID + " now=" + tz.getOffset( now) / 3600000 + " / +182=" + tz.getOffset( now + ( 182 * 86400000)) / 3600000);
}
For short, give me the offset now and in 182 days
Executed September 3rd, the output is
EST now=-5 / +182=-5
EDT now=0 / +182=0
America/New_York now=-4 / +182=-4
This is unexpected for several reasons
1) Why is America/New_York not giving -4/-5 ?, Isn't it supposed to be date sensitive?
2) Why does EDT == UTC?
java.time
The question and the accepted answer use the java.util date-time API which was the right thing to do in 2012. In March 2014, the modern Date-Time API was released as part of the Java 8 standard library which supplanted the legacy date-time API and since then it is strongly recommended to switch to java.time, the modern date-time API.
Solution using java.time
You can use ZonedDateTime which automatically adjusts the time zone offset for a given ZoneId.
Demo:
import java.time.ZoneId;
import java.time.ZonedDateTime;
class Main {
public static void main(String[] args) {
ZoneId zone = ZoneId.of("America/New_York");
ZonedDateTime now = ZonedDateTime.now(zone);
ZonedDateTime after182Days = now.plusDays(182);
System.out.println(zone + " now=" + now.getOffset() + " / +182=" + after182Days.getOffset());
}
}
Output as of now:
America/New_York now=-05:00 / +182=-04:00
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
Do not use three-letter timezone ID: Note from the Java 7 Timezone documentation:
Three-letter time zone IDs
For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are
also supported. However, their use is deprecated because the same
abbreviation is often used for multiple time zones (for example, "CST"
could be U.S. "Central Standard Time" and "China Standard Time"), and
the Java platform can then only recognize one of them.
One problem you have is that 182 * 86400000 overflows. If you use
long now = System.currentTimeMillis();
for( String tzID: "EST,EDT,America/New_York".split(",")) {
TimeZone tz = TimeZone.getTimeZone( tzID);
System.out.println( tz.getDisplayName() + " now=" + tz.getOffset( now) / 36e5
+ " / +182=" + tz.getOffset( now + 182 * 86400000L) / 36e5);
}
prints
Eastern Standard Time now=-5.0 / +182=-5.0
Greenwich Mean Time now=0.0 / +182=0.0
Eastern Standard Time now=-4.0 / +182=-5.0
If you look at the javadoc and source for getTimeZone you can see
* #return the specified <code>TimeZone</code>, or the GMT zone if the given ID
* cannot be understood.
public static synchronized TimeZone getTimeZone(String ID) {
return getTimeZone(ID, true);
}
private static TimeZone getTimeZone(String ID, boolean fallback) {
TimeZone tz = ZoneInfo.getTimeZone(ID);
if (tz == null) {
tz = parseCustomTimeZone(ID);
if (tz == null && fallback) {
tz = new ZoneInfo(GMT_ID, 0);
}
}
return tz;
}
In short, EDT is not recognised so it becomes GMT.
I suspect this is the problem:
now + ( 182 * 86400000)
The parenthesized arithmetic expression overflows 32 bits. You probably want:
now + ( 182 * 86400000L)
However, that still assumes that any daylight saving time will be applied for roughly six months, which is certainly not the case in the real world. For example, looking at the Sao Paolo time zone, it switches in October and February - so if you ran your code in September, you'd end up seeing -3 / -3. Even for time zones where DST switches on/off roughly every six months, you're very likely to find 182 consecutive days each year without a switchover (almost by definition, given that that's slightly less than half a year).
It's not clear exactly what you're trying to do, but I suspect you should really just be saving the time zone ID, e.g. "America/New_York". Almost anything else is asking for trouble.
i want to convert a string with a format of HH:MM:SS or MM:SS or SS into a datatype of Duration.
solution:
private ArrayList<Duration> myCdDuration = new ArrayList<Duration>();
private void convert(String aDuration) {
chooseNewDuration(stringToInt(splitDuration(aDuration))); //stringToInt() returns an int[] and splitDuration() returns a String[]
}
private void chooseNewDuration(int[] array) {
int elements = array.length;
switch (elements) {
case 1:
myCdDuration.add(newDuration(true, 0, 0, 0, 0, 0, array[0]));
break;
case 2:
myCdDuration.add(newDuration(true, 0, 0, 0, 0, array[0], array[1]));
break;
case 3:
myCdDuration.add(newDuration(true, 0, 0, 0, array[0], array[1],
array[2]));
break;
}
}
thanks for help ... any easier way to do that ? -> create your own Duration class:
public class Duration {
private int intSongDuration;
private String printSongDuration;
public String getPrintSongDuration() {
return printSongDuration;
}
public void setPrintSongDuration(int songDuration) {
printSongDuration = intToStringDuration(songDuration);
}
public int getIntSongDuration() {
return intSongDuration;
}
public void setIntSongDuration(int songDuration) {
intSongDuration = songDuration;
}
public Duration(int songDuration) {
setIntSongDuration(songDuration);
}
Converts the int value into a String for output/print:
private String intToStringDuration(int aDuration) {
String result = "";
int hours = 0, minutes = 0, seconds = 0;
hours = aDuration / 3600;
minutes = (aDuration - hours * 3600) / 60;
seconds = (aDuration - (hours * 3600 + minutes * 60));
result = String.format("%02d:%02d:%02d", hours, minutes, seconds);
return result;
}
tl;dr
No need to define your own Duration class, as Java provides one.
Duration.between ( // Represent a span of time of hours, minutes, seconds.
LocalTime.MIN , // 00:00:00
LocalTime.parse ( "08:30:00" ) // Parse text as a time-of-day.
) // Returns a `Duration` object, a span-of-time.
.toString() // Generate a `String` with text in standard ISO 8601 format.
PT8H30M
And parse standard ISO 8601 formatted text.
Duration.parse( "PT8H30M" ) // Parse standard ISO 8601 text yo get a `Duration` object.
Avoid HH:MM:SS format
If by the string 08:30:00 you mean "eight and a half hours" span of time rather than a time-of-day “half-past eight in the morning”, then avoid that format of HH:MM:SS. That format ambiguous, appearing to be a time-of-day. Instead use the standard ISO 8601 format discussed below.
Duration and time-of-day are two very different concepts. You must be clear on them, each should be distinct in your mind. Using the ambiguous format of HH:MM:SS makes that distinction all the more difficult (so avoid that format!).
java.time
The modern way is with the java.time classes.
LocalTime
First parse your string as a LocalTime. This class represents a time-of-day without a date and without a time zone. Having no time zone means these objects are based on a generic 24-hour clock without regard for anomalies such as Daylight Saving Time (DST).
We do not really want a LocalTime as your input string represents a span of time rather than a time-of-day. But this is just the first step.
LocalTime lt = LocalTime.parse ( "08:30:00" );
Duration
To represent the desired span-of-time, we want the Duration class. This class is for spans of time not attached to the timeline. We can create one by converting that LocalTime via getting the amount of time from the beginning of the time-of-day clock, 00:00:00.0 or LocalTime.MIN, and the lt we just instantiated.
Duration d = Duration.between ( LocalTime.MIN , lt );
Editing the input string
The approach above using LocalTime only works if your input strings represent a duration of less than 24 hours. If over 24 hours, you will parse the input string yourself.
Something like the following code. Of course the actual parsing depends on resolving the ambiguity of your particular input string. Is 50:00 meant to be fifty hours or fifty minutes? (This ambiguity is a strong reason to avoid this confusing format whenever possible, and stick with ISO 8601 formats.)
String input = "50:00"; // Or "50:00:00" (fifty hours, either way)
String[] parts = input.split ( ":" );
Duration d = Duration.ZERO;
if ( parts.length == 3 ) {
int hours = Integer.parseInt ( parts[ 0 ] );
int minutes = Integer.parseInt ( parts[ 1 ] );
int seconds = Integer.parseInt ( parts[ 2 ] );
d = d.plusHours ( hours ).plusMinutes ( minutes ).plusSeconds ( seconds );
} else if ( parts.length == 2 ) {
int hours = Integer.parseInt ( parts[ 0 ] );
int minutes = Integer.parseInt ( parts[ 1 ] );
d = d.plusHours ( hours ).plusMinutes ( minutes );
} else {
System.out.println ( "ERROR - Unexpected input." );
}
ISO 8601
We can see the result by generating a String in standard ISO 8601 format for durations by simply calling Duration::toString. The java.time classes use ISO 8601 by default when parsing/generating strings. For durations, the standard format is PnYnMnDTnHnMnS where the P marks the beginning and the T separates the years-months-days portion from the hours-minutes-seconds portion. So, our eight-and-a-half hours will appear as PT8H30M.
System.out.println ( "d.toString(): " + d );
d.toString(): PT8H30M
Collecting Duration objects
You can make a List holding elements of the type Duration.
List<Duration> durations = new ArrayList<>( 3 ); // Initial capacity of 3 elements.
durations.add( d ) ;
durations.add( Duration.between ( LocalTime.MIN , LocalTime.parse ( "03:00:00" ) ) ) ;
durations.add( Duration.between ( LocalTime.MIN , LocalTime.parse ( "01:15:00" ) ) ) ;
durations.toString(): [PT8H30M, PT3H, PT1H15M]
Remember that the strings you see in that output like PT8H30M are just that: output of generated strings. The Duration type is not a simple string but rather generates a String object by its toString method.
If you stick to the ISO 8601 formats, you can easily parse as well as generate such strings. No need to go through the LocalTime conversion rigamarole we performed at the top of this Answer.
Duration d = Duration.parse( "PT8H30M" );
See this example code live in IdeOne.com.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
Java SE 8 and SE 9 and later
Built-in.
Part of the standard Java API with a bundled implementation.
Java 9 adds some minor features and fixes.
Java SE 6 and SE 7
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
I assume what you're ultimately trying to achieve is to compute the duration of the CD in seconds.
There are several ways to do this, but I think the most straightforward is to just split on : to get the hours, minutes, and seconds fields, then to compute the duration manually:
String timestampStr = "14:35:06";
String[] tokens = timestampStr.split(":");
int hours = Integer.parseInt(tokens[0]);
int minutes = Integer.parseInt(tokens[1]);
int seconds = Integer.parseInt(tokens[2]);
int duration = 3600 * hours + 60 * minutes + seconds;
java.time.Duration
A couple of other answers have already mentioned that the Duration class from java.time, the modern Java date and time API, is the class to use for a duration. There are some ways to parse your strings into a Duration, and it’s honestly not clear which one is the best. I’d like to present my way.
Basic
I start simple with just one possible format, hh:mm:ss, for example 01:30:41 for 1 hour 30 minutes 41 seconds.
String durationString = "01:30:41";
String iso = durationString.replaceFirst(
"^(\\d{2}):(\\d{2}):(\\d{2})$", "PT$1H$2M$3S");
Duration dur = Duration.parse(iso);
System.out.format("%-10s Total %2d minutes or %4d seconds%n",
dur, dur.toMinutes(), dur.toSeconds());
Output so far is:
PT1H30M41S Total 90 minutes or 5441 seconds
The Duration.parse method requires ISO 8601 format. It goes like PT1H30M41S for 1 hour 30 minutes 41 seconds. So what I do is I convert your string into this format through a regular expression. The $1, etc., in my replacement string will be substituted by what was matched by the groups in round brackets in the regular expression. So durationString.replaceFirst() converts your string to PT01H30M41S, which Duration can parse.
Three formats
You asked for conversion of HH:MM:SS or MM:SS or SS. The modification to the above is actually quite simple: we just need three calls to replaceFirst() instead of one. Exactly one of them will succeed in replacing anything. The other two will just return the string unchanged.
String[] durationStrings = { "01:32:43", "26:31", "14" };
for (String durationString : durationStrings) {
String iso = durationString.replaceFirst("^(\\d{2}):(\\d{2}):(\\d{2})$", "PT$1H$2M$3S")
.replaceFirst("^(\\d{2}):(\\d{2})$", "PT$1M$2S")
.replaceFirst("^(\\d{2})$", "PT$1S");
Duration dur = Duration.parse(iso);
System.out.format("%-10s Total %2d minutes or %4d seconds%n",
dur, dur.toMinutes(), dur.toSeconds());
}
PT1H32M43S Total 92 minutes or 5563 seconds
PT26M31S Total 26 minutes or 1591 seconds
PT14S Total 0 minutes or 14 seconds
Time4J and net.time4j.Duration
In case you’re fine with an external dependency, the Time4J library offers a much more elegant way of parsing your strings to duration objects. We first declare a formatter:
private static final Duration.Formatter<ClockUnit> FORMATTER
= Duration.formatter(ClockUnit.class, "[[hh:]mm:]ss");
The square brackets in the format pattern string surround optional parts, so this formatter accepts all of hh:mm:ss, mm:ss and just ss.
for (String durationString : durationStrings) {
Duration<ClockUnit> dur = FORMATTER.parse(durationString);
long minutes = dur.with(ClockUnit.MINUTES.only())
.getPartialAmount(ClockUnit.MINUTES);
long seconds = dur.with(ClockUnit.SECONDS.only())
.getPartialAmount(ClockUnit.SECONDS);
System.out.format("%-10s Total %2d minutes or %4d seconds%n",
dur, minutes, seconds);
}
Output is the same as before:
PT1H32M43S Total 92 minutes or 5563 seconds
PT26M31S Total 26 minutes or 1591 seconds
PT14S Total 0 minutes or 14 seconds
Links
Oracle tutorial: Date Time explaining how to use java.time.
Wikipedia article: ISO 8601
Time4J - API, Tutorials and Examples
Your myCdDuration is confusing. Do you want one Duration object equivalent to whatever was specified in the string, or a list of Duration objects where the first contains the hours, the second minutes etc?
You can't just cast a String into some other object. You should parse the value into an numeric type and use DataTypeFactory to construct the Duration object.
I would suggest not using javax.xml.datatype.Duration, as its related to the XML Java API and it's confusing to use it if you are not dealing with XML. Moreover, it is an abstract class, and there's no non-abstract documented implementation of it in Java SE, so you'd have to either create your own non-abstract implementation or obtain an instance somehow (probably, playing with the XML API).
You manage time and dates in Java using the Date and Calendar classes. To convert Strings to Date/Calendar you use DateFormat or SimpleDateFormat. That will let you perform your duration arithmetic, although that's not 100% pretty.
Mansoor provides a way to do stuff manually using String manipulation and handling durations as numeric values- if you only do simple stuff, it might be more straightforward to do that.
If you have to perform more complex stuff, you might want to look into http://joda-time.sourceforge.net/
I have written this method in my utils class to parse various kind of duration strings. It is quite flexible :
public static int getSecondsFromFormattedDuration(String duration){
if(duration==null)
return 0;
try{
Pattern patternDuration = Pattern.compile("\\d+(?::\\d+){0,2}");
int hours = 0;
int minutes = 0;
int seconds = 0;
if(patternDuration.matcher(duration).matches()){
String[] tokens = duration.split(":");
if(tokens.length==1){
seconds = Integer.parseInt(tokens[0]);
}else if(tokens.length == 2){
minutes = Integer.parseInt(tokens[0]);
seconds = Integer.parseInt(tokens[1]);
}else{
hours = Integer.parseInt(tokens[0]);
minutes = Integer.parseInt(tokens[1]);
seconds = Integer.parseInt(tokens[2]);
}
return 3600 * hours + 60 * minutes + seconds;
}else
return 0;
}catch (NumberFormatException ignored){
return 0;
}
}
This is how it parsed these durations :
"1" --> 1
"10" --> 10
"10:" --> 0 (not a valid duration)
"10:07" --> 607
"06:08" --> 368
"7:22" --> 442
":22" --> 0 (not a valid duration)
"10:32:33" --> 37953
"2:33:22" --> 9202
"2:2:02" --> 7322
"2:33:43:32" --> 0 (not a valid duration)
"33ff" --> 0 (not a valid duration)
"2d:33" --> 0 (not a valid duration)
With Java 8 and Java.time.Duration you can do this given that the string is of the format HH:MM:SS or MM:SS or SS
Duration.ofSeconds(Arrays.stream(runtime.split(":"))
.mapToInt(n -> Integer.parseInt(n))
.reduce(0, (n, m) -> n * 60 + m));
Sample, Convert Current DateTime to Duration in Java 7.
DatatypeFactory.newInstance().newDuration(Calendar.getInstance().getTimeInMillis())
Output -
P48Y5M13DT19H59M24.658S