List of usage examples for org.joda.time Period getDays
public int getDays()
From source file:org.apache.drill.exec.vector.DateUtilities.java
License:Apache License
public static StringBuilder intervalStringBuilder(Period value) { return intervalStringBuilder(value.getYears() * 12 + value.getMonths(), value.getDays(), periodToMillis(value));/*from w ww.ja va 2s . c om*/ }
From source file:org.apigw.authserver.svc.impl.TokenServicesImpl.java
License:Open Source License
/** * @param residentIdentificationNumber in format yyyyMMddnnnn- for example 199212319876 * @param validitySeconds - for example 43200 (60 * 60 * 12) - 12 hours in the future * @return/*from w ww. j a va 2 s . co m*/ */ protected Date generateExpirationTime(String residentIdentificationNumber, long validitySeconds) { if (residentIdentificationNumber == null || residentIdentificationNumber.length() < 8) { throw new IllegalArgumentException( "Invalid residentIdentificationNumber " + residentIdentificationNumber); } long validityMilliseconds = validitySeconds * 1000L; final String birthdayString = residentIdentificationNumber.substring(0, 8); final DateTime birthDate = DateTime.parse(birthdayString, ISODateTimeFormat.basicDate()); Period period = new Period(birthDate, new DateTime(), PeriodType.yearMonthDay()); DateTime birthDatePlusLegalGuardianAgeLimit = birthDate.plusYears(legalGuardianAgeLimit); DateTime residentAdultDate = birthDate.plusYears(adultAge); DateTime expiration = new DateTime().withTimeAtStartOfDay(); //defaulting to midnight of today (token will be immediately invalid) if (validitySeconds > 0) { // Standard expiration final DateTime standardExpiration = new DateTime().plus(validityMilliseconds); if (birthDatePlusLegalGuardianAgeLimit.isAfter(now().plus(validityMilliseconds))) { // resident will hit the legal guardian age after max token validity expiration = standardExpiration; } else if (residentAdultDate.isBeforeNow()) { // resident is considered adult expiration = standardExpiration; } else if (birthDatePlusLegalGuardianAgeLimit.isAfterNow()) { //resident will hit the legal guardian age before max token validity expiration = birthDatePlusLegalGuardianAgeLimit; } // if we get here resident has passed legal guardian age but is not considered adult, using default } log.debug("calculated token exp time for resident who is ~ {} years, {} months and {} days old to {}", period.getYears(), period.getMonths(), period.getDays(), expiration); return expiration.toDate(); }
From source file:org.ash.gui.ASHrawdata.java
License:Open Source License
/** * Get period in mm, dd, hh, ss/*ww w .j ava 2s .com*/ * @param begind * @param endd * @return */ private String getPeriod(double begind, double endd) { String out = ""; Double beginD = begind; Double endD = endd; DateTime start = new DateTime(beginD.longValue()); DateTime end = new DateTime(endD.longValue()); Period period = new Period(start, end); if (period.getMonths() > 0) out = period.getMonths() + " m. "; if (period.getDays() > 0) out = out + period.getDays() + " d. "; if (period.getHours() > 0) out = out + period.getHours() + " h. "; if (period.getMinutes() > 0) out = out + period.getMinutes() + " min. "; if (period.getSeconds() > 0) out = out + period.getSeconds() + " sec. "; return out; }
From source file:org.ash.history.CalendarH.java
License:Open Source License
/** * Get period between start and end date from BDB * //from www.ja va 2 s . c o m * @return */ private String getPeriodBDB() { String out = ""; DateTime start = new DateTime(getStartBDB()); DateTime end = new DateTime(getEndBDB()); Period period = new Period(start, end); if (period.getMonths() > 0) out = period.getMonths() + " month(s) "; if (period.getDays() > 0) out = out + period.getDays() + " day(s) "; if (period.getHours() > 0) out = out + period.getHours() + " hour(s) "; if (period.getMinutes() > 0) out = out + period.getMinutes() + " minute(s) "; return out; }
From source file:org.dungeon.io.SavesTableWriter.java
License:Open Source License
private static String makePeriodString(long start, long end) { Period period = new Period(start, end); TimeStringBuilder builder = new TimeStringBuilder(); builder.set(EarthTimeUnit.YEAR, period.getYears()); builder.set(EarthTimeUnit.MONTH, period.getMonths()); builder.set(EarthTimeUnit.DAY, period.getDays()); builder.set(EarthTimeUnit.HOUR, period.getHours()); builder.set(EarthTimeUnit.MINUTE, period.getMinutes()); builder.set(EarthTimeUnit.SECOND, period.getSeconds()); return builder.toString(2) + " ago"; }
From source file:org.eclim.plugin.core.command.history.HistoryListCommand.java
License:Open Source License
private String delta(long time) { // FIXME: a formatter can probably do this. Period period = new Period(time, System.currentTimeMillis()); ArrayList<String> parts = new ArrayList<String>(); int years = period.getYears(); if (years > 0) { parts.add(years + " year" + (years == 1 ? "" : "s")); }//from w w w . ja v a2 s . c o m int months = period.getMonths(); if (months > 0) { parts.add(months + " month" + (months == 1 ? "" : "s")); } int weeks = period.getWeeks(); if (weeks > 0) { parts.add(weeks + " week" + (weeks == 1 ? "" : "s")); } int days = period.getDays(); if (days > 0) { parts.add(days + " day" + (days == 1 ? "" : "s")); } int hours = period.getHours(); if (hours > 0) { parts.add(hours + " hour" + (hours == 1 ? "" : "s")); } int minutes = period.getMinutes(); if (minutes > 0) { parts.add(minutes + " minute" + (minutes == 1 ? "" : "s")); } int seconds = period.getSeconds(); if (seconds > 0) { parts.add(seconds + " second" + (seconds == 1 ? "" : "s")); } if (parts.size() == 0) { int millis = period.getMillis(); if (millis > 0) { parts.add(millis + " millis"); } } return StringUtils.join(parts.toArray(), ' ') + " ago"; }
From source file:org.estatio.dom.valuetypes.AbstractInterval.java
License:Apache License
/** * The duration in days/* w ww . j a v a 2s .c om*/ * * @return */ public int days() { if (isInfinite()) { return 0; } Period p = new Period(asInterval(), PeriodType.days()); return p.getDays(); }
From source file:org.gdg.frisbee.android.utils.Utils.java
License:Apache License
public static String toHumanTimePeriod(Context ctx, DateTime start, DateTime end) { String result;// w w w . j ava 2 s . c o m 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; }
From source file:org.gephi.desktop.timeline.DateTick.java
License:Open Source License
public static DateTick create(double min, double max, int width) { DateTime minDate = new DateTime((long) min); DateTime maxDate = new DateTime((long) max); Period period = new Period(minDate, maxDate, PeriodType.yearMonthDayTime()); ;/*from w w w . ja va2s . c om*/ int years = period.getYears(); int months = period.getMonths(); int days = period.getDays(); int hours = period.getHours(); int minutes = period.getMinutes(); int seconds = period.getSeconds(); //Top type DateTimeFieldType topType; if (years > 0) { topType = DateTimeFieldType.year(); } else if (months > 0) { topType = DateTimeFieldType.monthOfYear(); } else if (days > 0) { topType = DateTimeFieldType.dayOfMonth(); } else if (hours > 0) { topType = DateTimeFieldType.hourOfDay(); } else if (minutes > 0) { topType = DateTimeFieldType.minuteOfHour(); } else if (seconds > 0) { topType = DateTimeFieldType.secondOfMinute(); } else { topType = DateTimeFieldType.millisOfSecond(); } //Bottom type if (topType != DateTimeFieldType.millisOfSecond()) { DateTimeFieldType bottomType; if (topType.equals(DateTimeFieldType.year())) { bottomType = DateTimeFieldType.monthOfYear(); } else if (topType.equals(DateTimeFieldType.monthOfYear())) { bottomType = DateTimeFieldType.dayOfMonth(); } else if (topType.equals(DateTimeFieldType.dayOfMonth())) { bottomType = DateTimeFieldType.hourOfDay(); } else if (topType.equals(DateTimeFieldType.hourOfDay())) { bottomType = DateTimeFieldType.minuteOfHour(); } else if (topType.equals(DateTimeFieldType.minuteOfHour())) { bottomType = DateTimeFieldType.secondOfMinute(); } else { bottomType = DateTimeFieldType.millisOfSecond(); } //Number of ticks Period p = new Period(minDate, maxDate, PeriodType.forFields(new DurationFieldType[] { bottomType.getDurationType() })); int intervals = p.get(bottomType.getDurationType()); if (intervals > 0) { int intervalSize = width / intervals; if (intervalSize >= MIN_PIXELS) { return new DateTick(minDate, maxDate, new DateTimeFieldType[] { topType, bottomType }); } } } return new DateTick(minDate, maxDate, new DateTimeFieldType[] { topType }); }
From source file:org.gephi.desktop.timeline.MinimalDrawer.java
License:Open Source License
private void paintUpperRulerForInterval(Graphics2D g2d, DateTime dtFrom, DateTime dtTo) { g2d.setFont(settings.graduations.font); g2d.setColor(settings.graduations.fontColor); int leftMargin = settings.graduations.leftMargin; int textTopPosition = settings.graduations.textTopPosition; int width = getWidth(); int height = getHeight(); // TODO take these from the model Interval interval = new Interval(dtFrom, dtTo); Period p = interval.toPeriod(PeriodType.days()); // try to determine length if we had to show milliseconds int n = p.getDays(); int unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("wednesday ", null)).getWidth(); if (n < (width / unitSize)) { //System.out.println("jour"); for (int i = 0; i < n; i++) { g2d.drawString(dtFrom.plusDays(i).dayOfWeek().getAsText(LOCALE), leftMargin + 2 + i * (width / n), textTopPosition);/* w ww .j a v a 2s.c o m*/ g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin); paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n), Hours.hoursBetween(dtFrom.plusDays(i), dtFrom.plusDays(i + 1)).getHours()); } return; } unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("wed ", null)).getWidth(); if (n < (width / unitSize)) { //System.out.println("jou"); for (int i = 0; i < n; i++) { g2d.drawString(dtFrom.plusDays(i).dayOfWeek().getAsShortText(LOCALE), leftMargin + 2 + i * (width / n), textTopPosition); g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin); paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n), Hours.hoursBetween(dtFrom.plusDays(i), dtFrom.plusDays(i + 1)).getHours()); } return; } p = interval.toPeriod(PeriodType.days()); n = p.getDays(); unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("30", null)).getWidth(); if (n < (width / unitSize)) { //System.out.println("j"); for (int i = 0; i < n; i++) { g2d.drawString("" + (dtFrom.getDayOfMonth() + i), leftMargin + 2 + i * (width / n), textTopPosition); g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin); paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n), Hours.hoursBetween(dtFrom.plusDays(i), dtFrom.plusDays(i + 1)).getHours()); } return; } p = interval.toPeriod(PeriodType.months()); n = p.getMonths(); unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("September ", null)).getWidth(); if (n < (width / unitSize)) { //System.out.println("mois"); for (int i = 0; i < n; i++) { g2d.drawString(dtFrom.plusMonths(i).monthOfYear().getAsText(LOCALE), leftMargin + 2 + i * (width / n), textTopPosition); g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin); paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n), Days.daysBetween(dtFrom.plusMonths(i), dtFrom.plusMonths(i + 1)).getDays()); } return; } unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("dec ", null)).getWidth(); if (n < (width / unitSize)) { //System.out.println("mo"); for (int i = 0; i < n; i++) { g2d.drawString(dtFrom.plusMonths(i).monthOfYear().getAsShortText(LOCALE), leftMargin + 2 + i * (width / n), textTopPosition); g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin); paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n), Days.daysBetween(dtFrom.plusMonths(i), dtFrom.plusMonths(i + 1)).getDays()); } return; } unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("29 ", null)).getWidth(); if (n < (width / unitSize)) { //System.out.println("m"); for (int i = 0; i < n; i++) { g2d.drawString("" + (dtFrom.getMonthOfYear() + i), leftMargin + 2 + i * (width / n), textTopPosition); g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin); paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n), Days.daysBetween(dtFrom.plusMonths(i), dtFrom.plusMonths(i + 1)).getDays()); } return; } p = interval.toPeriod(PeriodType.years()); n = p.getYears(); unitSize = (int) (settings.graduations.fontMetrics.getStringBounds("1980 ", null)).getWidth(); if (n < (width / unitSize)) { //System.out.println("year"); for (int i = 0; i < n; i++) { g2d.drawString("" + (dtFrom.getYear() + i), leftMargin + 2 + i * (width / n), textTopPosition); g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin); paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n), Months.monthsBetween(dtFrom.plusYears(i), dtFrom.plusYears(i + 1)).getMonths()); } return; } int group = 10; n = p.getYears() / group; if (n < (width / unitSize)) { //System.out.println("10 years"); for (int i = 0; i < n; i++) { g2d.drawString("" + (dtFrom.getYear() + i * group), leftMargin + 2 + i * (width / n), textTopPosition); g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin); paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n), Months .monthsBetween(dtFrom.plusYears(i * group), dtFrom.plusYears((i + 1) * group)).getMonths()); } return; } group = 20; n = p.getYears() / group; if (n < (width / unitSize)) { //System.out.println("20 years"); for (int i = 0; i < n; i++) { g2d.drawString("" + (dtFrom.getYear() + i * group), leftMargin + 2 + i * (width / n), textTopPosition); g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin); paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n), Months .monthsBetween(dtFrom.plusYears(i * group), dtFrom.plusYears((i + 1) * group)).getMonths()); } return; } group = 50; n = p.getYears() / group; if (n < (width / unitSize)) { //System.out.println("50 years"); for (int i = 0; i < n; i++) { g2d.drawString("" + (dtFrom.getYear() + i * group), leftMargin + 2 + i * (width / n), textTopPosition); g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin); paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n), Months .monthsBetween(dtFrom.plusYears(i * group), dtFrom.plusYears((i + 1) * group)).getMonths()); } return; } group = 100; n = p.getYears() / group; if (n / 100 < (width / unitSize)) { //System.out.println("100 years"); for (int i = 0; i < n; i++) { g2d.drawString("" + (dtFrom.getYear() + i * group), leftMargin + 2 + i * (width / n), textTopPosition); g2d.drawLine(leftMargin + i * (width / n), 2, leftMargin + i * (width / n), height - settings.graduations.textBottomMargin); paintSmallGraduations(g2d, leftMargin + i * (width / n), leftMargin + (i + 1) * (width / n), Months .monthsBetween(dtFrom.plusYears(i * group), dtFrom.plusYears((i + 1) * group)).getMonths()); } } return; }