public static String toHumanTimePeriod(Context ctx, DateTime start, DateTime end) { String result; Resources res = ctx.getResources(); Period p = new Period(start, end); if (p.getYears() == 0 && p.getMonths() == 0 && p.getWeeks() == 0 && p.getDays() == 0 && p.getHours() == 0 && p.getMinutes() == 0) { result = res.getQuantityString(R.plurals.seconds_ago, p.getSeconds(), p.getSeconds()); } else if (p.getYears() == 0 && p.getMonths() == 0 && p.getWeeks() == 0 && p.getDays() == 0 && p.getHours() == 0) { result = res.getQuantityString(R.plurals.minutes_ago, p.getMinutes(), p.getMinutes()); } else if (p.getYears() == 0 && p.getMonths() == 0 && p.getWeeks() == 0 && p.getDays() == 0) { result = res.getQuantityString(R.plurals.hours_ago, p.getHours(), p.getHours()); } else if (p.getYears() == 0 && p.getMonths() == 0 && p.getWeeks() == 0) { result = res.getQuantityString(R.plurals.days_ago, p.getDays(), p.getDays()); } else { result = start .toLocalDateTime() .toString(DateTimeFormat.patternForStyle("M-", res.getConfiguration().locale)); } return result; }
@Transient public String getWorkage() { if (this.dob != null) { Calendar c = Calendar.getInstance(); if (this.firstworktime == null) c.setTime(new Date()); else c.setTime(firstworktime); Calendar cnow = Calendar.getInstance(); cnow.setTime(new Date()); Period p = new Period(c.getTimeInMillis(), cnow.getTimeInMillis(), PeriodType.yearMonthDay()); Integer yearint = p.getYears(); Integer monthint = p.getMonths(); Integer dayint = p.getDays(); return yearint.toString() + "年" + monthint.toString() + "月" + dayint.toString() + "日"; } else return ""; }
private String epocToSimpleDate(long epoc) { Period diff = new Period(epoc, System.currentTimeMillis(), PeriodType.standard()); PeriodType type; if (diff.getMonths() > 0) { type = PeriodType.yearMonthDay(); } else if (diff.getWeeks() > 0) { type = PeriodType.yearWeekDay(); } else if (diff.getDays() > 0) { type = PeriodType.dayTime().withSecondsRemoved().withMillisRemoved().withMinutesRemoved(); } else if (diff.getMinutes() > 0) { type = PeriodType.time().withMillisRemoved().withSecondsRemoved(); } else { type = PeriodType.time().withMillisRemoved(); } return PeriodFormat.getDefault().print(new Period(epoc, System.currentTimeMillis(), type)); }
/** * Adds adjustment to the shortest set time range in period. E.g. period("5 days 3 hours", 1) -> * "5 days 4 hours". This will fall back to adjusting years if no field in the period is set. * * @param period The period to be adjusted * @param adjustment The adjustment. Note that positive values will result in larger periods and * an earlier time * @return The adjusted period */ private Period adjust(final Period period, int adjustment) { if (adjustment == 0) return period; // Order is VERY important here LinkedHashMap<Integer, DurationFieldType> map = new LinkedHashMap<>(); map.put(period.getSeconds(), DurationFieldType.seconds()); map.put(period.getMinutes(), DurationFieldType.minutes()); map.put(period.getHours(), DurationFieldType.hours()); map.put(period.getDays(), DurationFieldType.days()); map.put(period.getWeeks(), DurationFieldType.weeks()); map.put(period.getMonths(), DurationFieldType.months()); map.put(period.getYears(), DurationFieldType.years()); for (Map.Entry<Integer, DurationFieldType> entry : map.entrySet()) { if (entry.getKey() > 0) { return period.withFieldAdded(entry.getValue(), adjustment); } } // Fall back to modifying years return period.withFieldAdded(DurationFieldType.years(), adjustment); }
@Transient public String getAge() { if (this.dob != null) { Calendar c = Calendar.getInstance(); c.setTime(this.dob); Calendar cnow = Calendar.getInstance(); cnow.setTime(new Date()); Period p = new Period(c.getTimeInMillis(), cnow.getTimeInMillis(), PeriodType.yearMonthDay()); Integer yearint = p.getYears(); Integer monthint = p.getMonths(); Integer dayint = p.getDays(); // Integer yearint = cnow.get(Calendar.YEAR) - c.get(Calendar.YEAR); // if(cnow.get(Calendar.DAY_OF_YEAR) < c.get(Calendar.DAY_OF_YEAR)) // yearint--; // Integer monthint = (cnow.get(Calendar.MONTH) - c.get(Calendar.MONTH) + 12)%12; // Integer dayint = (cnow.get(Calendar.DAY_OF_MONTH) - c.get(Calendar.DAY_OF_MONTH)+30)%30; return yearint.toString() + "岁" + monthint.toString() + "月" + dayint.toString() + "日"; } else return ""; }
/** * Builds and returns earliest text when given a resources bundle. * * @param formats an array of strings containing formats for [no-alarm-active, < minute, xx-yy-zz, * xx-yy, xx] where xx, yy, zz can be either day, hours, minutes (non-respectively). * @param partsFormat an array of strings containing part formats for [day, month, hour]. * @param diff difference between now and ats. * @param ats an AlarmTimestamp: the information about the alarm & its timestamp. * @return the built time-to string. */ public static final String getTimeToText( String[] formats, String[] partFormats, Period diff, AlarmTimestamp ats) { String earliestText; // Not real? = we don't have any alarms active. if (ats == AlarmTimestamp.INVALID) { earliestText = formats[0]; } else { int[] diffVal = { Math.abs(diff.getDays()), Math.abs(diff.getHours()), Math.abs(diff.getMinutes()) }; // What fields are set? BitSet setFields = new BitSet(3); setFields.set(0, diffVal[0] != 0); setFields.set(1, diffVal[1] != 0); setFields.set(2, diffVal[2] != 0); int cardinality = setFields.cardinality(); earliestText = formats[cardinality + 1]; if (cardinality > 0) { List<String> args = new ArrayList<String>(3); for (int i = setFields.nextSetBit(0); i >= 0; i = setFields.nextSetBit(i + 1)) { args.add(partFormats[i]); } // Finally format everything. earliestText = String.format(earliestText, args.toArray()); earliestText = String.format(earliestText, diffVal[0], diffVal[1], diffVal[2]); } else { // only seconds remains until it goes off. earliestText = formats[1]; } } return earliestText; }
@Override public long truncate(long t) { if (isCompound) { try { return truncateMillisPeriod(t); } catch (UnsupportedOperationException e) { return truncateCompoundPeriod(t); } } final int years = period.getYears(); if (years > 0) { if (years > 1 || hasOrigin) { int y = chronology.years().getDifference(t, origin); y -= y % years; long tt = chronology.years().add(origin, y); // always round down to the previous period (for timestamps prior to origin) if (t < tt) t = chronology.years().add(tt, -years); else t = tt; return t; } else { return chronology.year().roundFloor(t); } } final int months = period.getMonths(); if (months > 0) { if (months > 1 || hasOrigin) { int m = chronology.months().getDifference(t, origin); m -= m % months; long tt = chronology.months().add(origin, m); // always round down to the previous period (for timestamps prior to origin) if (t < tt) t = chronology.months().add(tt, -months); else t = tt; return t; } else { return chronology.monthOfYear().roundFloor(t); } } final int weeks = period.getWeeks(); if (weeks > 0) { if (weeks > 1 || hasOrigin) { // align on multiples from origin int w = chronology.weeks().getDifference(t, origin); w -= w % weeks; long tt = chronology.weeks().add(origin, w); // always round down to the previous period (for timestamps prior to origin) if (t < tt) t = chronology.weeks().add(tt, -weeks); else t = tt; return t; } else { t = chronology.dayOfWeek().roundFloor(t); // default to Monday as beginning of the week return chronology.dayOfWeek().set(t, 1); } } final int days = period.getDays(); if (days > 0) { if (days > 1 || hasOrigin) { // align on multiples from origin int d = chronology.days().getDifference(t, origin); d -= d % days; long tt = chronology.days().add(origin, d); // always round down to the previous period (for timestamps prior to origin) if (t < tt) t = chronology.days().add(tt, -days); else t = tt; return t; } else { t = chronology.hourOfDay().roundFloor(t); return chronology.hourOfDay().set(t, 0); } } final int hours = period.getHours(); if (hours > 0) { if (hours > 1 || hasOrigin) { // align on multiples from origin long h = chronology.hours().getDifferenceAsLong(t, origin); h -= h % hours; long tt = chronology.hours().add(origin, h); // always round down to the previous period (for timestamps prior to origin) if (t < tt) t = chronology.hours().add(tt, -hours); else t = tt; return t; } else { t = chronology.minuteOfHour().roundFloor(t); return chronology.minuteOfHour().set(t, 0); } } final int minutes = period.getMinutes(); if (minutes > 0) { // align on multiples from origin if (minutes > 1 || hasOrigin) { long m = chronology.minutes().getDifferenceAsLong(t, origin); m -= m % minutes; long tt = chronology.minutes().add(origin, m); // always round down to the previous period (for timestamps prior to origin) if (t < tt) t = chronology.minutes().add(tt, -minutes); else t = tt; return t; } else { t = chronology.secondOfMinute().roundFloor(t); return chronology.secondOfMinute().set(t, 0); } } final int seconds = period.getSeconds(); if (seconds > 0) { // align on multiples from origin if (seconds > 1 || hasOrigin) { long s = chronology.seconds().getDifferenceAsLong(t, origin); s -= s % seconds; long tt = chronology.seconds().add(origin, s); // always round down to the previous period (for timestamps prior to origin) if (t < tt) t = chronology.seconds().add(tt, -seconds); else t = tt; return t; } else { return chronology.millisOfSecond().set(t, 0); } } final int millis = period.getMillis(); if (millis > 0) { if (millis > 1) { long ms = chronology.millis().getDifferenceAsLong(t, origin); ms -= ms % millis; long tt = chronology.millis().add(origin, ms); // always round down to the previous period (for timestamps prior to origin) if (t < tt) t = chronology.millis().add(tt, -millis); else t = tt; return t; } else { return t; } } return t; }