List of usage examples for org.joda.time.format PeriodFormatter parsePeriod
public Period parsePeriod(String text)
From source file:ca.phon.session.AgeFormatter.java
License:Open Source License
/** * Return an age string as a {@link Period} object * /*from ww w .j a v a 2s . c om*/ * @param text * @return age * * @throws IllegalArgumentException if the string is not formatted * correctly */ public static Period stringToAge(String text) { final PeriodFormatter formatter = createFormatter(); return formatter.parsePeriod(text); }
From source file:colossal.pipe.BaseOptions.java
License:Apache License
private Period parseDuration() { PeriodFormatter[] toTry = { PeriodFormat.getDefault(), ISOPeriodFormat.standard(), ISOPeriodFormat.alternate(), ISOPeriodFormat.alternateExtended(), ISOPeriodFormat.alternateExtendedWithWeeks(), ISOPeriodFormat.alternateWithWeeks() }; for (PeriodFormatter f : toTry) { try {//from ww w .j a v a 2 s . c o m return f.parsePeriod(duration); } catch (IllegalArgumentException iae) { // skip to next } } throw new IllegalArgumentException("Can't parse: " + duration); }
From source file:com.anrisoftware.propertiesutils.DateContextProperties.java
License:Open Source License
/** * Returns a time period property using the format defined in * {@link ISOPeriodFormat#standard()}./*from w ww .ja va 2 s.c o m*/ * * @param key * the property key. * * @param formatter * the {@link PeriodFormatter} that parses the period property. * * @return the {@link Period}. */ public Period getPeriodProperty(String key, PeriodFormatter formatter) { String property = getProperty(key); return formatter.parsePeriod(property); }
From source file:com.anrisoftware.propertiesutils.DateContextProperties.java
License:Open Source License
/** * Returns a time duration property using the format defined in * {@link ISODurationFormat#standard()}. * //from w ww.j a va2 s.c o m * @param key * the property key. * * @param formatter * the {@link PeriodFormatter} that parses the duration property. * * @return the {@link Duration}. */ public Duration getDurationProperty(String key, PeriodFormatter formatter) { String property = getProperty(key); return formatter.parsePeriod(property).toStandardDuration(); }
From source file:com.enonic.cms.business.portal.datasource.expressionfunctions.ExpressionFunctions.java
License:Open Source License
public String currentDatePlusOffset(String format, String periodStr) { DateTime nowDateTime = timeService.getNowAsDateTime(); PeriodFormatter periodFormatter = ISOPeriodFormat.standard(); Period period = periodFormatter.parsePeriod(periodStr); DateTime offsetDateTime = nowDateTime.plus(period); SimpleDateFormat fmt = new SimpleDateFormat(format); return fmt.format(offsetDateTime.toDate()); }
From source file:com.enonic.cms.business.portal.datasource.expressionfunctions.ExpressionFunctions.java
License:Open Source License
public String currentDateMinusOffset(String format, String periodStr) { DateTime nowDateTime = timeService.getNowAsDateTime(); PeriodFormatter periodFormatter = ISOPeriodFormat.standard(); Period period = periodFormatter.parsePeriod(periodStr); DateTime offsetDateTime = nowDateTime.minus(period); SimpleDateFormat fmt = new SimpleDateFormat(format); return fmt.format(offsetDateTime.toDate()); }
From source file:com.facebook.presto.util.DateTimeUtils.java
License:Apache License
private static Period parsePeriod(PeriodFormatter periodFormatter, String value, IntervalField startField, IntervalField endField) {//from w ww . ja v a 2 s. c o m try { return periodFormatter.parsePeriod(value); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( "Invalid INTERVAL " + startField + " to " + endField + " value: " + value, e); } }
From source file:com.fatboyindustrial.gsonjodatime.PeriodConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the * specified type. <p>/* www. j a v a 2 s . co m*/ * * In the implementation of this call-back method, you should consider invoking * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects * for any non-trivial field of the returned object. However, you should never invoke it on the * the same type passing {@code json} since that will cause an infinite loop (Gson will call your * call-back method again). * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public Period deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { // Do not try to deserialize null or empty values if (json.getAsString() == null || json.getAsString().isEmpty()) { return null; } final PeriodFormatter fmt = ISOPeriodFormat.standard(); return fmt.parsePeriod(json.getAsString()); }
From source file:com.funambol.common.pim.utility.TimeUtils.java
License:Open Source License
/** * Returns the given Iso 8601 duration in minutes * @param iso8601Duration String//from w w w. jav a 2s . c o m * @return int */ public static int getMinutes(String iso8601Duration) { if (iso8601Duration == null || iso8601Duration.equals("") || iso8601Duration.equalsIgnoreCase("null")) { return -1; } PeriodFormatter formatter = ISOPeriodFormat.standard(); Period p = formatter.parsePeriod(iso8601Duration); Duration d = p.toDurationFrom(new Instant(0)); long mills = d.getMillis(); int minutes = (int) (mills / 60L / 1000L); return minutes; }
From source file:com.gmt2001.YouTubeAPIv3.java
License:Open Source License
public int[] GetVideoLength(String id) { com.gmt2001.Console.debug.println("YouTubeAPIv3.GetVideoLength Start id=" + id); JSONObject j = GetData(request_type.GET, "https://www.googleapis.com/youtube/v3/videos?id=" + id + "&key=" + apikey + "&part=contentDetails"); if (j.getBoolean("_success")) { if (j.getInt("_http") == 200) { JSONArray a = j.getJSONArray("items"); if (a.length() > 0) { JSONObject i = a.getJSONObject(0); JSONObject cd = i.getJSONObject("contentDetails"); PeriodFormatter formatter = ISOPeriodFormat.standard(); Period d = formatter.parsePeriod(cd.getString("duration")); if (cd.getString("duration").equalsIgnoreCase("PT0S")) { com.gmt2001.Console.debug.println("YouTubeAPIv3.GetVideoLength Fail (Live Stream)"); return new int[] { 123, 456, 7899 }; }/*from ww w . j av a 2 s.c o m*/ //String d = cd.getString("duration").substring(2); int h, m, s; String hours = d.toStandardHours().toString().substring(2); h = Integer.parseInt(hours.substring(0, hours.indexOf("H"))); String minutes = d.toStandardMinutes().toString().substring(2); m = Integer.parseInt(minutes.substring(0, minutes.indexOf("M"))); String seconds = d.toStandardSeconds().toString().substring(2); s = Integer.parseInt(seconds.substring(0, seconds.indexOf("S"))); /* * if (d.contains("H")) { h = * Integer.parseInt(d.substring(0, d.indexOf("H"))); * * d = d.substring(0, d.indexOf("H")); } * * if (d.contains("M")) { m = * Integer.parseInt(d.substring(0, d.indexOf("M"))); * * d = d.substring(0, d.indexOf("M")); } * * s = Integer.parseInt(d.substring(0, d.indexOf("S"))); */ com.gmt2001.Console.debug.println("YouTubeAPIv3.GetVideoLength Success"); return new int[] { h, m, s }; } else { com.gmt2001.Console.debug.println("YouTubeAPIv3.GetVideoLength Fail"); return new int[] { 0, 0, 0 }; } } else { com.gmt2001.Console.debug.println("YouTubeAPIv3.GetVideoLength Fail2"); return new int[] { 0, 0, 0 }; } } com.gmt2001.Console.debug.println("YouTubeAPIv3.GetVideoLength Fail3"); return new int[] { 0, 0, 0 }; }