List of usage examples for javax.xml.datatype DatatypeFactory newDuration
public abstract Duration newDuration(final long durationInMilliSeconds);
From source file:org.miloss.fgsms.presentation.Helper.java
/** * returns a duration parsed from xyr xmo xd xhr xmin xs * //from w ww. j a va 2s .c om * first it will parse it as a xml representation then as a Long then the * specific format */ public static Duration StringToDuration(String d) throws DatatypeConfigurationException { if (Utility.stringIsNullOrEmpty(d)) { return null; } String[] s = d.split(" "); if (s == null || s.length == 0) { return null; } DatatypeFactory fac = DatatypeFactory.newInstance(); try { Duration dur = fac.newDuration(d); return dur; } catch (Exception ex) { } try { Duration dur = fac.newDuration(Long.parseLong(d)); return dur; } catch (Exception ex) { } long durationInMilliSeconds = 0; String temp; long x = 0; for (int i = 0; i < s.length; i++) { if (s[i].contains("yr")) { temp = s[i].replace("yr", "").trim(); x = Long.parseLong(temp); durationInMilliSeconds += (x * 365 * 24 * 60 * 60 * 1000); } else if (s[i].contains("mo")) { temp = s[i].replace("mo", "").trim(); x = Long.parseLong(temp); durationInMilliSeconds += (x * 30 * 24 * 60 * 60 * 1000); } else if (s[i].contains("d")) { temp = s[i].replace("d", "").trim(); x = Long.parseLong(temp); durationInMilliSeconds += (x * 24 * 60 * 60 * 1000); } else if (s[i].contains("hr")) { temp = s[i].replace("hr", "").trim(); x = Long.parseLong(temp); durationInMilliSeconds += (x * 60 * 60 * 1000); } else if (s[i].contains("min")) { temp = s[i].replace("min", "").trim(); x = Long.parseLong(temp); durationInMilliSeconds += (x * 60 * 1000); } else if (s[i].contains("s")) { temp = s[i].replace("s", "").trim(); x = Long.parseLong(temp); durationInMilliSeconds += (x * 1000); } } Duration dur = fac.newDuration(durationInMilliSeconds); return dur; }