/** * 获得两个时间点之间的时间跨度 * * @param time1 * 开始的时间点 * @param time2 * 结束的时间点 * @param timeUnit * 跨度的时间单位 see {@link JodaTime} * (支持的时间单位有DAY,HOUR,MINUTE,SECOND,MILLI) */ public static long lengthBetween(DateTime time1, DateTime time2, DurationFieldType timeUnit) { Duration duration = Days.daysBetween(time1, time2).toStandardDuration(); if (timeUnit == JodaTime.DAY) { return duration.getStandardDays(); } else if (timeUnit == JodaTime.HOUR) { return duration.getStandardHours(); } else if (timeUnit == JodaTime.MINUTE) { return duration.getStandardMinutes(); } else if (timeUnit == JodaTime.SECOND) { return duration.getStandardSeconds(); } else if (timeUnit == JodaTime.MILLI) { return duration.getMillis(); } else { throw new RuntimeException( "TimeUnit not supported except DAY,HOUR,MINUTE,SECOND,MILLI"); } }
private static DurationFieldType getDurationFieldType(OrgInterval.Unit unit) { switch (unit) { case HOUR: return DurationFieldType.hours(); case DAY: return DurationFieldType.days(); case WEEK: return DurationFieldType.weeks(); case MONTH: return DurationFieldType.months(); case YEAR: return DurationFieldType.years(); default: throw new IllegalArgumentException("Unknown unit " + unit); } }
/** * Adds the fields from another period. * * @param values the array of values to update * @param period the period to add from, not null * @return the updated values * @throws IllegalArgumentException if an unsupported field's value is non-zero */ protected int[] addPeriodInto(int[] values, ReadablePeriod period) { for (int i = 0, isize = period.size(); i < isize; i++) { DurationFieldType type = period.getFieldType(i); int value = period.getValue(i); if (value != 0) { int index = indexOf(type); if (index == -1) { throw new IllegalArgumentException( "Period does not support field '" + type.getName() + "'"); } else { values[index] = FieldUtils.safeAdd(getValue(index), value); } } } return values; }
boolean isSupported(PeriodType type, int field) { switch (field) { default: return false; case YEARS: return type.isSupported(DurationFieldType.years()); case MONTHS: return type.isSupported(DurationFieldType.months()); case WEEKS: return type.isSupported(DurationFieldType.weeks()); case DAYS: return type.isSupported(DurationFieldType.days()); case HOURS: return type.isSupported(DurationFieldType.hours()); case MINUTES: return type.isSupported(DurationFieldType.minutes()); case SECONDS: return type.isSupported(DurationFieldType.seconds()); case MILLIS: return type.isSupported(DurationFieldType.millis()); case SECONDS_MILLIS: // drop through case SECONDS_OPTIONAL_MILLIS: return type.isSupported(DurationFieldType.seconds()) || type.isSupported(DurationFieldType.millis()); } }
public FDate add(final FTimeUnit field, final int amount) { final MutableDateTime delegate = newMutableDateTime(); final int usedAmount; final DurationFieldType usedField; switch (field) { case MILLENIA: usedField = FTimeUnit.YEARS.jodaTimeValue(); usedAmount = amount * FTimeUnit.YEARS_IN_MILLENIUM; break; case CENTURIES: usedField = FTimeUnit.YEARS.jodaTimeValue(); usedAmount = amount * FTimeUnit.YEARS_IN_CENTURY; break; case DECADES: usedField = FTimeUnit.YEARS.jodaTimeValue(); usedAmount = amount * FTimeUnit.YEARS_IN_DECADE; break; default: usedField = field.jodaTimeValue(); usedAmount = amount; break; } delegate.add(usedField, usedAmount); return new FDate(delegate); }
public void testCutoverAddYears() { testAdd("1582-01-01", DurationFieldType.years(), 1, "1583-01-01"); testAdd("1582-02-15", DurationFieldType.years(), 1, "1583-02-15"); testAdd("1582-02-28", DurationFieldType.years(), 1, "1583-02-28"); testAdd("1582-03-01", DurationFieldType.years(), 1, "1583-03-01"); testAdd("1582-09-30", DurationFieldType.years(), 1, "1583-09-30"); testAdd("1582-10-01", DurationFieldType.years(), 1, "1583-10-01"); testAdd("1582-10-04", DurationFieldType.years(), 1, "1583-10-04"); testAdd("1582-10-15", DurationFieldType.years(), 1, "1583-10-15"); testAdd("1582-10-16", DurationFieldType.years(), 1, "1583-10-16"); testAdd("1580-01-01", DurationFieldType.years(), 4, "1584-01-01"); testAdd("1580-02-29", DurationFieldType.years(), 4, "1584-02-29"); testAdd("1580-10-01", DurationFieldType.years(), 4, "1584-10-01"); testAdd("1580-10-10", DurationFieldType.years(), 4, "1584-10-10"); testAdd("1580-10-15", DurationFieldType.years(), 4, "1584-10-15"); testAdd("1580-12-31", DurationFieldType.years(), 4, "1584-12-31"); }
private void testAdd(String start, DurationFieldType type, int amt, String end) { DateTime dtStart = new DateTime(start, GJChronology.getInstance(DateTimeZone.UTC)); DateTime dtEnd = new DateTime(end, GJChronology.getInstance(DateTimeZone.UTC)); assertEquals(dtEnd, dtStart.withFieldAdded(type, amt)); assertEquals(dtStart, dtEnd.withFieldAdded(type, -amt)); DurationField field = type.getField(GJChronology.getInstance(DateTimeZone.UTC)); int diff = field.getDifference(dtEnd.getMillis(), dtStart.getMillis()); assertEquals(amt, diff); if (type == DurationFieldType.years() || type == DurationFieldType.months() || type == DurationFieldType.days()) { YearMonthDay ymdStart = new YearMonthDay(start, GJChronology.getInstance(DateTimeZone.UTC)); YearMonthDay ymdEnd = new YearMonthDay(end, GJChronology.getInstance(DateTimeZone.UTC)); assertEquals(ymdEnd, ymdStart.withFieldAdded(type, amt)); assertEquals(ymdStart, ymdEnd.withFieldAdded(type, -amt)); } }
public void testCutoverAddMonths() { testAdd("1582-01-01", DurationFieldType.months(), 1, "1582-02-01"); testAdd("1582-01-01", DurationFieldType.months(), 6, "1582-07-01"); testAdd("1582-01-01", DurationFieldType.months(), 12, "1583-01-01"); testAdd("1582-11-15", DurationFieldType.months(), 1, "1582-12-15"); testAdd("1582-09-04", DurationFieldType.months(), 2, "1582-11-04"); testAdd("1582-09-05", DurationFieldType.months(), 2, "1582-11-05"); testAdd("1582-09-10", DurationFieldType.months(), 2, "1582-11-10"); testAdd("1582-09-15", DurationFieldType.months(), 2, "1582-11-15"); // Leap years... testAdd("1580-01-01", DurationFieldType.months(), 48, "1584-01-01"); testAdd("1580-02-29", DurationFieldType.months(), 48, "1584-02-29"); testAdd("1580-10-01", DurationFieldType.months(), 48, "1584-10-01"); testAdd("1580-10-10", DurationFieldType.months(), 48, "1584-10-10"); testAdd("1580-10-15", DurationFieldType.months(), 48, "1584-10-15"); testAdd("1580-12-31", DurationFieldType.months(), 48, "1584-12-31"); }
public void testCutoverAddYears() { testAdd("1582-01-01", DurationFieldType.years(), 1, "1583-01-01"); testAdd("1582-02-15", DurationFieldType.years(), 1, "1583-02-15"); testAdd("1582-02-28", DurationFieldType.years(), 1, "1583-02-28"); testAdd("1582-03-01", DurationFieldType.years(), 1, "1583-03-01"); testAdd("1582-09-30", DurationFieldType.years(), 1, "1583-09-30"); testAdd("1582-10-01", DurationFieldType.years(), 1, "1583-10-01"); testAdd("1582-10-04", DurationFieldType.years(), 1, "1583-10-04"); testAdd("1582-10-15", DurationFieldType.years(), 1, "1583-10-15"); testAdd("1582-10-16", DurationFieldType.years(), 1, "1583-10-16"); // Leap years... testAdd("1580-01-01", DurationFieldType.years(), 4, "1584-01-01"); testAdd("1580-02-29", DurationFieldType.years(), 4, "1584-02-29"); testAdd("1580-10-01", DurationFieldType.years(), 4, "1584-10-01"); testAdd("1580-10-10", DurationFieldType.years(), 4, "1584-10-10"); testAdd("1580-10-15", DurationFieldType.years(), 4, "1584-10-15"); testAdd("1580-12-31", DurationFieldType.years(), 4, "1584-12-31"); }
public void testAddMonths() { testAdd("1582-01-01", DurationFieldType.months(), 1, "1582-02-01"); testAdd("1582-01-01", DurationFieldType.months(), 6, "1582-07-01"); testAdd("1582-01-01", DurationFieldType.months(), 12, "1583-01-01"); testAdd("1582-11-15", DurationFieldType.months(), 1, "1582-12-15"); testAdd("1582-09-04", DurationFieldType.months(), 2, "1582-11-04"); testAdd("1582-09-05", DurationFieldType.months(), 2, "1582-11-05"); testAdd("1582-09-10", DurationFieldType.months(), 2, "1582-11-10"); testAdd("1582-09-15", DurationFieldType.months(), 2, "1582-11-15"); testAdd("1580-01-01", DurationFieldType.months(), 48, "1584-01-01"); testAdd("1580-02-29", DurationFieldType.months(), 48, "1584-02-29"); testAdd("1580-10-01", DurationFieldType.months(), 48, "1584-10-01"); testAdd("1580-10-10", DurationFieldType.months(), 48, "1584-10-10"); testAdd("1580-10-15", DurationFieldType.months(), 48, "1584-10-15"); testAdd("1580-12-31", DurationFieldType.months(), 48, "1584-12-31"); }
public void testCutoverAddWeekyears() { testAdd("1582-W01-1", DurationFieldType.weekyears(), 1, "1583-W01-1"); testAdd("1582-W39-1", DurationFieldType.weekyears(), 1, "1583-W39-1"); testAdd("1583-W45-1", DurationFieldType.weekyears(), 1, "1584-W45-1"); // This test fails, but I'm not sure if its worth fixing. The date // falls after the cutover, but in the cutover year. The add operation // is performed completely within the gregorian calendar, with no // crossing of the cutover. As a result, no special correction is // applied. Since the full gregorian year of 1582 has a different week // numbers than the full julian year of 1582, the week number is off by // one after the addition. // //testAdd("1582-W42-1", DurationFieldType.weekyears(), 1, "1583-W42-1"); // Leap years... testAdd("1580-W01-1", DurationFieldType.weekyears(), 4, "1584-W01-1"); testAdd("1580-W30-7", DurationFieldType.weekyears(), 4, "1584-W30-7"); testAdd("1580-W50-7", DurationFieldType.weekyears(), 4, "1584-W50-7"); }
/** * Get the list of dates among the starting and ending date. * * @param LocalDate the starting date * @param LocalDate the ending date * @return List<String> the list of dates between the starting and ending date */ public List<String> getListOfDates(LocalDate startDate, LocalDate endDate) { List<String> datesList = new ArrayList<String>(); int days = Days.daysBetween(startDate, endDate).getDays(); for (int i = 0; i < days; i++) { LocalDate dt = startDate.withFieldAdded(DurationFieldType.days(), i); datesList.add(dt.toString()); } return datesList; }
/** * Creates a Joda duration/period object * * @param date object that represents the duration of the period through milisseconds */ private void initializeDuration(Date date) { // Get duration from Date mDuration = new Duration(date.getTime()); // Get period final DurationFieldType[] durationFields = new DurationFieldType[]{DurationFieldType.weeks(), DurationFieldType.days(), DurationFieldType.hours(), DurationFieldType.minutes()}; mPeriod = mDuration.toPeriod(PeriodType.forFields(durationFields)).normalizedStandard(); updatePeriod(); }
/** * This method is called when the selection in the view holder has changed * * @param slider The calling slider * @param value the new value */ @Override public void onSelectionChanged(TimePeriodSlider slider, int value) { DurationFieldType fieldType = mConversion.get(slider.getDateType()); mPeriod = mPeriod.withField(fieldType, value); updatePeriod(); }
/** * @param daysInYear The number of days in each year */ protected FixedYearLengthChronology(int daysInYear) { this.daysInYear = daysInYear; this.yearDuration = new PreciseDurationField(DurationFieldType.years(), daysInYear * dayDuration.getUnitMillis()); this.centuryDuration = new PreciseDurationField(DurationFieldType.centuries(), 100 * yearDuration.getUnitMillis()); this.dayOfYear = new OneBasedPreciseDateTimeField(DateTimeFieldType.dayOfYear(), dayDuration, this.yearDuration); this.yearOfCentury = new PreciseDateTimeField(DateTimeFieldType.yearOfCentury(), this.yearDuration, this.centuryDuration); this.year = new YearField(this.yearDuration); }
public String getLeft(int next, boolean showsecs) { LocalDateTime date = getTimeCal(null, next); Period period = new Period(LocalDateTime.now(), date, PeriodType.dayTime()); if (showsecs) { return Utils.toArabicNrs(PERIOD_FORMATTER_HMS.print(period)); } else if (Prefs.isDefaultWidgetMinuteType()) { return Utils.toArabicNrs(PERIOD_FORMATTER_HM.print(period)); } else { period = period.withFieldAdded(DurationFieldType.minutes(), 1); return Utils.toArabicNrs(PERIOD_FORMATTER_HM.print(period)); } }
private static DurationFieldType durationFieldType(SqlTypeName intervalTypeToCount) { if (!INTERVALS_DURATIONS_TYPES.containsKey(intervalTypeToCount)) { throw new IllegalArgumentException("Counting " + intervalTypeToCount.getName() + "s between dates is not supported"); } return INTERVALS_DURATIONS_TYPES.get(intervalTypeToCount); }
/** * Returns the revision of an article at a particular age * * @param articleId id of the article * @param creationTS explicitly defines the creation TimeStamp from where to count (e.g. if we want to retrieve the revision of the TalkPage based on the age of the article Page) * @param ageInDays age in days * @param revApi revisionApi object * @return the revision object, if it exists. null, else */ public static Revision getRevisionForAge(int articleId, Timestamp creationTS, int ageInDays, RevisionApi revApi) throws WikiApiException{ DateTime creation = new DateTime(creationTS); DateTime version = creation.withFieldAdded(DurationFieldType.days(), ageInDays); for(Timestamp curTS:revApi.getRevisionTimestamps(articleId)){ DateTime curDT = new DateTime(curTS); if(curDT.isEqual(version)||curDT.isAfter(version)){ return revApi.getRevision(articleId, curTS); } } return null; }
/** * Checks whether a field type is supported, and if so adds the new value * to the relevant index in the specified array. * * @param type the field type * @param values the array to update * @param newValue the new value to store if successful */ private void checkAndUpdate(DurationFieldType type, int[] values, int newValue) { int index = indexOf(type); if (index == -1) { if (newValue != 0) { throw new IllegalArgumentException( "Period does not support field '" + type.getName() + "'"); } } else { values[index] = newValue; } }
/** * Private method called from constructor. */ private void setPeriodInternal(ReadablePeriod period) { int[] newValues = new int[size()]; for (int i = 0, isize = period.size(); i < isize; i++) { DurationFieldType type = period.getFieldType(i); int value = period.getValue(i); checkAndUpdate(type, newValues, value); } setValues(newValues); }
/** * Private method called from constructor. */ private int[] setPeriodInternal(int years, int months, int weeks, int days, int hours, int minutes, int seconds, int millis) { int[] newValues = new int[size()]; checkAndUpdate(DurationFieldType.years(), newValues, years); checkAndUpdate(DurationFieldType.months(), newValues, months); checkAndUpdate(DurationFieldType.weeks(), newValues, weeks); checkAndUpdate(DurationFieldType.days(), newValues, days); checkAndUpdate(DurationFieldType.hours(), newValues, hours); checkAndUpdate(DurationFieldType.minutes(), newValues, minutes); checkAndUpdate(DurationFieldType.seconds(), newValues, seconds); checkAndUpdate(DurationFieldType.millis(), newValues, millis); return newValues; }
/** * Sets the value of a field in this period. * * @param values the array of values to update * @param field the field to set * @param value the value to set * @throws IllegalArgumentException if field is null or not supported. */ protected void setFieldInto(int[] values, DurationFieldType field, int value) { int index = indexOf(field); if (index == -1) { if (value != 0 || field == null) { throw new IllegalArgumentException( "Period does not support field '" + field + "'"); } } else { values[index] = value; } }
/** * Adds the value of a field in this period. * * @param values the array of values to update * @param field the field to set * @param value the value to set * @throws IllegalArgumentException if field is is null or not supported. */ protected void addFieldInto(int[] values, DurationFieldType field, int value) { int index = indexOf(field); if (index == -1) { if (value != 0 || field == null) { throw new IllegalArgumentException( "Period does not support field '" + field + "'"); } } else { values[index] = FieldUtils.safeAdd(values[index], value); } }
/** * Merges the fields from another period. * * @param values the array of values to update * @param period the period to add from, not null * @return the updated values * @throws IllegalArgumentException if an unsupported field's value is non-zero */ protected int[] mergePeriodInto(int[] values, ReadablePeriod period) { for (int i = 0, isize = period.size(); i < isize; i++) { DurationFieldType type = period.getFieldType(i); int value = period.getValue(i); checkAndUpdate(type, values, value); } return values; }
/** * Gets an array of the field types that this period supports. * <p> * The fields are returned largest to smallest, for example Hours, Minutes, Seconds. * * @return the fields supported in an array that may be altered, largest to smallest */ public DurationFieldType[] getFieldTypes() { DurationFieldType[] result = new DurationFieldType[size()]; for (int i = 0; i < result.length; i++) { result[i] = getFieldType(i); } return result; }
/** * Gets the index of the first fields to have the specified duration, * or -1 if the field is unsupported. * * @param type the type to check, may be null which returns -1 * @return the index of the field, -1 if unsupported */ protected int indexOf(DurationFieldType type) { for (int i = 0, isize = size(); i < isize; i++) { if (getFieldType(i).getDurationType() == type) { return i; } } return -1; }
/** * Constructor. * * @param field the base field * @param type the field type to use */ protected DelegatedDurationField(DurationField field, DurationFieldType type) { super(); if (field == null) { throw new IllegalArgumentException("The field must not be null"); } iField = field; iType = (type == null ? field.getType() : type); }
/** * Constructor. * * @param field the base field * @param type the type to actually use */ public DecoratedDurationField(DurationField field, DurationFieldType type) { super(type); if (field == null) { throw new IllegalArgumentException("The field must not be null"); } if (!field.isSupported()) { throw new IllegalArgumentException("The field must be supported"); } iField = field; }
protected BaseDurationField(DurationFieldType type) { super(); if (type == null) { throw new IllegalArgumentException("The type must not be null"); } iType = type; }
/** * Gets an instance of UnsupportedDurationField for a specific named field. * The returned instance is cached. * * @param type the type to obtain * @return the instance */ public static synchronized UnsupportedDurationField getInstance(DurationFieldType type) { UnsupportedDurationField field; if (cCache == null) { cCache = new HashMap<DurationFieldType, UnsupportedDurationField>(7); field = null; } else { field = cCache.get(type); } if (field == null) { field = new UnsupportedDurationField(type); cCache.put(type, field); } return field; }
public void test_compareTo() { assertEquals(0, MillisDurationField.INSTANCE.compareTo(MillisDurationField.INSTANCE)); assertEquals(-1, MillisDurationField.INSTANCE.compareTo(ISOChronology.getInstance().seconds())); DurationField dummy = new PreciseDurationField(DurationFieldType.seconds(), 0); assertEquals(1, MillisDurationField.INSTANCE.compareTo(dummy)); // try { // MillisDurationField.INSTANCE.compareTo(""); // fail(); // } catch (ClassCastException ex) {} try { MillisDurationField.INSTANCE.compareTo(null); fail(); } catch (NullPointerException ex) {} }