Ejemplo n.º 1
0
  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;
  }
 /**
  * Calculates the number of days spanned in a period assuming 365 days per year, 30 days per
  * month, 7 days per week, 24 hours per day, 60 minutes per hour and 60 seconds per minute.
  *
  * @param period A period to retrieve the number of standard days for
  * @return The number of days spanned by the period.
  */
 protected static int getDaysInPeriod(final Period period) {
   int totalDays = 0;
   Period temp = new Period(period);
   if (period.getYears() > 0) {
     int years = period.getYears();
     totalDays += 365 * years;
     temp = temp.minusYears(years);
   }
   if (period.getMonths() > 0) {
     int months = period.getMonths();
     totalDays += 30 * period.getMonths();
     temp = temp.minusMonths(months);
   }
   return totalDays + temp.toStandardDays().getDays();
 }
Ejemplo n.º 3
0
 @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 "";
 }
Ejemplo n.º 4
0
 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);
  }
Ejemplo n.º 6
0
  @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 "";
  }
  /**
   * Convenience method to obtain the patient's (maximum) age in months.
   *
   * @return The patient's age in months at the time the given observations were taken.
   */
  protected int calculateAgeInMonths(Obs... observations) {
    long dateTaken = Long.MIN_VALUE;

    if (observations != null) {
      for (Obs obs : observations) {
        if (obs != null && obs.getObsDatetime() != null) {
          // determine the date the observation was taken (take the greater of the two)
          dateTaken = Math.max(dateTaken, obs.getObsDatetime().getTime());
        }
      }
    }

    if (dateTaken == Long.MIN_VALUE || dateTaken < birthdate.getTime()) {
      // unable to calculate patient's age!
      return 0;
    }

    // calculate how old this person was, in months, when the observations were taken
    final Period period = new Interval(birthdate.getTime(), dateTaken).toPeriod();
    final int ageInMonths = period.getYears() * 12 + period.getMonths();

    // return the patient's age in months
    return ageInMonths;
  }
Ejemplo n.º 8
0
 public static int NumberOfMonths(LocalDate firstDate, LocalDate secondDate) {
   Period period = new Period(firstDate, secondDate);
   return period.getMonths();
 }
Ejemplo n.º 9
0
        // when dialog box is closed, below method will be called.
        @Override
        public void onDateSet(
            DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {

          year = selectedYear;
          month = selectedMonth;
          day = selectedDay;

          // Show selected date
          expDate.setText(
              new StringBuilder()
                  .append(day)
                  .append("-")
                  .append(month + 1)
                  .append("-")
                  .append(year));

          StringBuilder sDate;
          StringBuilder eDate;

          eDate =
              new StringBuilder()
                  .append(year)
                  .append("-")
                  .append(month + 1)
                  .append("-")
                  .append(day);

          if (edDate.getText().toString().trim().length() == 0) {
            edDate.setText(
                new StringBuilder()
                    .append(day)
                    .append("-")
                    .append(month + 1)
                    .append("-")
                    .append(year));
            sDate =
                new StringBuilder()
                    .append(year)
                    .append("-")
                    .append(month + 1)
                    .append("-")
                    .append(day);
          } else {
            String tempDate = edDate.getText().toString();

            int tempdays =
                Integer.valueOf(tempDate.substring(0, edDate.getText().toString().indexOf("-")));
            int tempmonth =
                Integer.valueOf(
                    tempDate.substring(
                        edDate.getText().toString().indexOf("-") + 1,
                        edDate.getText().toString().lastIndexOf("-")));
            int tempyear =
                Integer.valueOf(
                    tempDate.substring(
                        edDate.getText().toString().lastIndexOf("-") + 1, edDate.length()));

            sDate =
                new StringBuilder()
                    .append(tempyear)
                    .append("-")
                    .append(tempmonth)
                    .append("-")
                    .append(tempdays);
          }

          LocalDate startDate = new LocalDate(sDate.toString().trim());
          LocalDate endDate = new LocalDate(eDate.toString().trim());

          Period period = new Period(startDate, endDate, PeriodType.months());
          Log.e("############## dif", "" + period.getMonths());

          if (period.getMonths() <= 0) {
            edTerm.setText("0");
          } else {
            edTerm.setText("" + period.getMonths());
          }
        }
Ejemplo n.º 10
0
    @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;
    }
Ejemplo n.º 11
0
 /** @return an integer representing the duration in months of the project. */
 public int getDurationInMonths() {
   Period period = getPeriod();
   return period.getYears() * 12 + period.getMonths();
 }