Example usage for org.joda.time PeriodType yearMonthDayTime

List of usage examples for org.joda.time PeriodType yearMonthDayTime

Introduction

In this page you can find the example usage for org.joda.time PeriodType yearMonthDayTime.

Prototype

public static PeriodType yearMonthDayTime() 

Source Link

Document

Gets a type that defines all standard fields except weeks.

Usage

From source file:com.google.livingstories.server.util.TimeUtil.java

License:Apache License

/**
 * Return a String representation of the time that has passed from the given time to
 * right now./*from   w w  w.j  ava 2  s .  co  m*/
 * This method returns an approximate user-friendly duration. Eg. If 2 months, 12 days and 4 hours
 * have passed, the method will return "2 months ago".
 * TODO: the results of this method need to be internationalized
 */
public static String getElapsedTimeString(Date updateCreationTime) {
    Period period = new Period(updateCreationTime.getTime(), new Date().getTime(),
            PeriodType.yearMonthDayTime());

    int years = period.getYears();
    int months = period.getMonths();
    int days = period.getDays();
    int hours = period.getHours();
    int minutes = period.getMinutes();
    int seconds = period.getSeconds();

    String timeLabel = "";

    if (years > 0) {
        timeLabel = years == 1 ? " year " : " years ";
        return "" + years + timeLabel + "ago";
    } else if (months > 0) {
        timeLabel = months == 1 ? " month " : " months ";
        return "" + months + timeLabel + "ago";
    } else if (days > 0) {
        timeLabel = days == 1 ? " day " : " days ";
        return "" + days + timeLabel + "ago";
    } else if (hours > 0) {
        timeLabel = hours == 1 ? " hour " : " hours ";
        return "" + hours + timeLabel + "ago";
    } else if (minutes > 0) {
        timeLabel = minutes == 1 ? " minute " : " minutes ";
        return "" + minutes + timeLabel + "ago";
    } else if (seconds > 0) {
        timeLabel = seconds == 1 ? " second " : " seconds ";
        return "" + seconds + timeLabel + "ago";
    } else {
        return "1 second ago";
    }
}

From source file:com.reclabs.recomendar.common.helpers.types.DateHelper.java

License:Open Source License

/**
 * Dada una fecha de referencia y una fecha a comparar obtenemos la diferencia de la primera con la segunda, el
 * formato depender del rango de la diferencia, es decir, si hay menos de
 * un mes de diferencia obtendremos la diferencia en das.
 * @param referenceDate The date of reference
 * @param compareDate The date to compare
 * @return Diferencia entre las fechas/* w w  w.  j  a v  a  2  s.co  m*/
 */
public static String getDifToCompareDateInUnitTimeLowRound(final Date referenceDate, final Date compareDate) {
    Period period = new Period(compareDate.getTime(), referenceDate.getTime(),
            PeriodType.yearMonthDayTime().withSecondsRemoved().withMillisRemoved());
    if ((period.getYears() > 0) || (period.getMonths() > 0) || (period.getDays() > 0)) {
        return REDUCED_DATE_FORMAT.format(compareDate);
    } else if (period.getHours() > 0) {
        return period.getHours() + " Hora" + (period.getHours() > 1 ? "s" : "");
    } else if (period.getMinutes() > 0) {
        return period.getMinutes() + " Minuto" + (period.getMinutes() > 1 ? "s" : "");
    } else {
        return "Ahora";
    }
}

From source file:com.wellsandwhistles.android.redditsp.common.SRTime.java

License:Open Source License

public static String formatDurationFrom(final Context context, final long startTime) {
    final String space = " ";
    final String comma = ",";
    final String separator = comma + space;

    final long endTime = utcCurrentTimeMillis();
    final DateTime dateTime = new DateTime(endTime);
    final DateTime localDateTime = dateTime.withZone(DateTimeZone.getDefault());
    Period period = new Duration(startTime, endTime).toPeriodTo(localDateTime);

    PeriodFormatter periodFormatter = new PeriodFormatterBuilder().appendYears().appendSuffix(space)
            .appendSuffix(context.getString(R.string.time_year), context.getString(R.string.time_years))
            .appendSeparator(separator).appendMonths().appendSuffix(space)
            .appendSuffix(context.getString(R.string.time_month), context.getString(R.string.time_months))
            .appendSeparator(separator).appendDays().appendSuffix(space)
            .appendSuffix(context.getString(R.string.time_day), context.getString(R.string.time_days))
            .appendSeparator(separator).appendHours().appendSuffix(space)
            .appendSuffix(context.getString(R.string.time_hour), context.getString(R.string.time_hours))
            .appendSeparator(separator).appendMinutes().appendSuffix(space)
            .appendSuffix(context.getString(R.string.time_min), context.getString(R.string.time_mins))
            .appendSeparator(separator).appendSeconds().appendSuffix(space)
            .appendSuffix(context.getString(R.string.time_sec), context.getString(R.string.time_secs))
            .appendSeparator(separator).appendMillis().appendSuffix(space)
            .appendSuffix(context.getString(R.string.time_ms)).toFormatter();

    String duration = periodFormatter.print(period.normalizedStandard(PeriodType.yearMonthDayTime()));

    List<String> parts = Arrays.asList(duration.split(comma));
    if (parts.size() >= 2) {
        duration = parts.get(0) + comma + parts.get(1);
    }/*from  w  w  w  .  j a  va  2s. c o  m*/

    return String.format(context.getString(R.string.time_ago), duration);
}

From source file:models.utils.DateUtils.java

License:Apache License

/**
 * Returns time difference/*from   ww w.j a  va  2 s.c o m*/
 * 
 * @param d1
 * @param d2
 * @return
 */
public static String getDateDifference(Date d1, Date d2) {
    Period period = new Period(new DateTime(d1), new DateTime(d2), PeriodType.yearMonthDayTime());
    return PERIOD_FORMATTER.print(period);
}

From source file:net.bither.viewsystem.froms.VanitygenPanel.java

License:Apache License

private String secondsToString(long seconds) {
    long now = System.currentTimeMillis();
    return remainingTimeFormatter.print(new Period(now, now + seconds * 1000, PeriodType.yearMonthDayTime()));
}

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());
    ;// w w w  .  j a v  a  2 s  .  c o m
    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.jadira.usertype.dateandtime.joda.columnmapper.StringColumnPeriodMapper.java

License:Apache License

private PeriodType determinePeriodType(String s) {

    PeriodType periodType = PeriodType.standard();

    String current = s;//  ww  w  . j  a v  a2s .com

    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:ventanas.parqueadero.ventas.java

private void btnPagoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPagoActionPerformed

    try {/* ww  w  .  j a  va  2s  .  co m*/
        int indice = dBTable1.getSelectedRow();
        Billing b = ventas.get(indice);

        Object state = controller.getEm().createNativeQuery("Select estado from factura where id=" + b.getId())
                .getSingleResult();

        if (state.toString().equals("Pagada")) {
            JOptionPane.showMessageDialog(this, "El ticket ya ha sido PAGADO.\n Actualice la tabla de datos",
                    "ERROR", JOptionPane.ERROR_MESSAGE);
            verTabla(true);
            return;
        }

        if (state.toString().equals("ANULADA")) {
            JOptionPane.showMessageDialog(this, "El ticket ha sido ANULADO.\n Actualice la tabla de datos",
                    "ERROR", JOptionPane.ERROR_MESSAGE);
            verTabla(true);
            return;
        }

        if (state.equals("CONTRATO")) {
            JOptionPane.showMessageDialog(this, "El ticket es de tipo CONTRATO.\n No se puede pagar.", "ERROR",
                    JOptionPane.ERROR_MESSAGE);
            verTabla(true);
            return;
        }

        //calcular el tiempo de pago
        Date timeStart = b.getDetailBillingList().get(0).getTimestart();
        DateTime start = new DateTime(timeStart);

        //obtener la fecha de la base de datos
        //generar la hora de la bd
        Date endJava = convertToDate_DatabaseDate();
        DateTime end = new DateTime(endJava);

        Period $period = new Period(start, end, PeriodType.yearMonthDayTime());
        int days = $period.getDays();
        int hours = $period.getHours();
        int minutes = $period.getMinutes();

        System.out.println("DIA " + $period.getDays());
        System.out.println("HORAS: " + $period.getHours());
        System.out.println("MINUTOS: " + $period.getMinutes());

        Product product = b.getDetailBillingList().get(0).getProductId();
        BigDecimal price = product.getSaleprice();
        BigDecimal quantity = BigDecimal.ZERO;
        String tiempo = days + ":" + hours + ":" + minutes;

        if (hours >= 1) {
            quantity = new BigDecimal(hours);
        }

        if (minutes > 0) {
            quantity = quantity.add(BigDecimal.ONE);
        }

        if (days >= 1) {
            int hoursPerDay = days * 24;
            quantity = quantity.add(new BigDecimal(hoursPerDay));
        }

        BigDecimal totalIva = price.multiply(quantity).multiply(product.getPercentageIva());
        BigDecimal total = product.getSaleprice().multiply(quantity).add(totalIva);
        total = total.setScale(2, RoundingMode.HALF_UP);

        CobroParkForm dialog = new CobroParkForm(new javax.swing.JFrame(), Boolean.TRUE, b, tiempo, days, hours,
                minutes, quantity, endJava, total);
        dialog.setVisible(true);
        verTabla(true);
    } catch (ParseException ex) {
        Logger.getLogger(ventas.class.getName()).log(Level.SEVERE, null, ex);
    }

}