List of usage examples for org.joda.time PeriodType standard
public static PeriodType standard()
From source file:com.cloudhopper.commons.util.demo.UptimeMain.java
License:Apache License
public static void main(String[] args) { //Period period = new Period(uptime, PeriodType.standard().withYearsRemoved().withWeeksRemoved().withMonthsRemoved().withMillisRemoved()); //MutablePeriod period = new Duration(uptime).toPeriod().toMutablePeriod(); long uptime = UPTIME_56_SECS; // ah, ha -- this is super important -- need to normalize the period! PeriodType periodType = PeriodType.standard().withYearsRemoved().withMonthsRemoved().withWeeksRemoved() .withMillisRemoved();/*from ww w. j av a 2 s . c om*/ Period period = new Period(uptime).normalizedStandard(periodType); System.out.println("Uptime: " + uptime + " ms"); System.out.println("Weeks: " + period.getWeeks()); System.out.println("Days: " + period.getDays()); System.out.println("Millis: " + period.getMillis() + " ms"); // print out the uptime String uptimeStyleString = PeriodFormatterUtil.getStandardUptimeStyle().print(period); String linuxStyleString = PeriodFormatterUtil.getLinuxUptimeStyle().print(period); System.out.println(uptimeStyleString); System.out.println(linuxStyleString); PeriodFormatter fmt = new PeriodFormatterBuilder().printZeroNever().appendDays() .appendSuffix(" day ", " days ").appendHours().appendSuffix(" hours ").appendMinutes() .appendSuffix(" mins ").printZeroAlways().appendSeconds().appendSuffix(" secs ").toFormatter(); String str0 = fmt.print(period); System.out.println(str0); String str1 = PeriodFormat.getDefault().print(period); System.out.println(str1); }
From source file:influent.server.utilities.DateTimeParser.java
License:MIT License
/** * @see http://joda-time.sourceforge.net/apidocs/org/joda/time/Period.html#normalizedStandard() *//*from w w w . j a v a2 s.c om*/ public static Period normalize(Period period) { long millis = period.getMillis(); millis += period.getSeconds() * DateTimeConstants.MILLIS_PER_SECOND; millis += period.getMinutes() * DateTimeConstants.MILLIS_PER_MINUTE; millis += period.getHours() * DateTimeConstants.MILLIS_PER_HOUR; millis += period.getDays() * DateTimeConstants.MILLIS_PER_DAY; millis += period.getWeeks() * DateTimeConstants.MILLIS_PER_WEEK; Period result = new Period(millis, DateTimeUtils.getPeriodType(PeriodType.standard()), ISOChronology.getInstanceUTC()); int years = period.getYears(); int months = period.getMonths(); if (years != 0 || months != 0) { years = FieldUtils.safeAdd(years, months / 12); months = months % 12; if (years != 0) { result = result.withYears(years); } if (months != 0) { result = result.withMonths(months); } } return result; }
From source file:net.sourceforge.fenixedu.presentationTier.renderers.DurationRenderer.java
License:Open Source License
private DurationFieldType[] getDurationFieldTypes() { List<DurationFieldType> result = new ArrayList<DurationFieldType>(); if (getIncludedFields() == null) { result.add(DurationFieldType.hours()); result.add(DurationFieldType.minutes()); } else {/*from w w w.ja v a2s. c o m*/ PeriodType standard = PeriodType.standard(); for (int index = 0; index < standard.size(); index++) { if (getIncludedFields().contains(standard.getFieldType(index).getName())) { result.add(standard.getFieldType(index)); } } } return result.toArray(new DurationFieldType[] {}); }
From source file:org.jadira.usertype.dateandtime.joda.columnmapper.StringColumnPeriodMapper.java
License:Apache License
private PeriodType determinePeriodType(String s) { PeriodType periodType = PeriodType.standard(); String current = s;/*from w w w . j av a 2 s. c o m*/ if (current.startsWith(PeriodType.standard().getName())) { periodType = PeriodType.standard(); current = s.substring(PeriodType.standard().getName().length()); } else if (current.startsWith(PeriodType.yearMonthDayTime().getName())) { periodType = PeriodType.yearMonthDayTime(); current = s.substring(PeriodType.yearMonthDayTime().getName().length()); } else if (current.startsWith(PeriodType.yearMonthDay().getName())) { periodType = PeriodType.yearMonthDay(); current = s.substring(PeriodType.yearMonthDay().getName().length()); } else if (current.startsWith(PeriodType.yearWeekDayTime().getName())) { periodType = PeriodType.yearWeekDayTime(); current = s.substring(PeriodType.yearWeekDayTime().getName().length()); } else if (current.startsWith(PeriodType.yearWeekDay().getName())) { periodType = PeriodType.yearWeekDay(); current = s.substring(PeriodType.yearWeekDay().getName().length()); } else if (current.startsWith(PeriodType.yearDayTime().getName())) { periodType = PeriodType.yearDayTime(); current = s.substring(PeriodType.yearDayTime().getName().length()); } else if (current.startsWith(PeriodType.yearDay().getName())) { periodType = PeriodType.yearDay(); current = s.substring(PeriodType.yearDay().getName().length()); } else if (current.startsWith(PeriodType.dayTime().getName())) { periodType = PeriodType.dayTime(); current = s.substring(PeriodType.dayTime().getName().length()); } else if (current.startsWith(PeriodType.time().getName())) { periodType = PeriodType.time(); current = s.substring(PeriodType.time().getName().length()); } else if (current.startsWith(PeriodType.years().getName())) { periodType = PeriodType.years(); current = s.substring(PeriodType.years().getName().length()); } else if (current.startsWith(PeriodType.months().getName())) { periodType = PeriodType.months(); current = s.substring(PeriodType.months().getName().length()); } else if (current.startsWith(PeriodType.weeks().getName())) { periodType = PeriodType.weeks(); current = s.substring(PeriodType.weeks().getName().length()); } else if (current.startsWith(PeriodType.days().getName())) { periodType = PeriodType.days(); current = s.substring(PeriodType.days().getName().length()); } else if (current.startsWith(PeriodType.hours().getName())) { periodType = PeriodType.hours(); current = s.substring(PeriodType.hours().getName().length()); } else if (current.startsWith(PeriodType.minutes().getName())) { periodType = PeriodType.minutes(); current = s.substring(PeriodType.minutes().getName().length()); } else if (current.startsWith(PeriodType.seconds().getName())) { periodType = PeriodType.seconds(); current = s.substring(PeriodType.seconds().getName().length()); } else if (current.startsWith(PeriodType.millis().getName())) { periodType = PeriodType.millis(); current = s.substring(PeriodType.millis().getName().length()); } while (current.length() > 0) { if (current.startsWith("NoYears")) { periodType = periodType.withYearsRemoved(); current = s.substring("NoYears".length()); } else if (current.startsWith("NoMonths")) { periodType = periodType.withMonthsRemoved(); current = s.substring("NoMonths".length()); } else if (current.startsWith("NoWeeks")) { periodType = periodType.withWeeksRemoved(); current = s.substring("NoWeeks".length()); } else if (current.startsWith("NoDays")) { periodType = periodType.withDaysRemoved(); current = s.substring("NoDays".length()); } else if (current.startsWith("NoHours")) { periodType = periodType.withHoursRemoved(); current = s.substring("NoHours".length()); } else if (current.startsWith("NoMinutes")) { periodType = periodType.withMinutesRemoved(); current = s.substring("NoMinutes".length()); } else if (current.startsWith("NoSeconds")) { periodType = periodType.withSecondsRemoved(); current = s.substring("NoSeconds".length()); } else if (current.startsWith("NoMillis")) { periodType = periodType.withMillisRemoved(); current = s.substring("NoMillis".length()); } else { throw new IllegalArgumentException("Unrecognised PeriodType: " + s + "{" + current + "}"); } } return periodType; }
From source file:ru.anr.base.BaseParent.java
License:Apache License
/** * @param startDate//from w w w . j av a 2 s . c o m * Start date * @param endDate * End date * @param locale * Locale * @return formatted period without seconds */ public static String formatPeriodWithoutSeconds(Calendar startDate, Calendar endDate, String locale) { Period period = new Period(startDate.getTimeInMillis(), endDate.getTimeInMillis(), PeriodType.standard().withSecondsRemoved().withMillisRemoved()); String l = locale == null ? Locale.getDefault().toString() : locale; return PeriodFormat.wordBased(Locale.forLanguageTag(l.replaceAll("_", "-"))).print(period); }