Example usage for org.joda.time Period getYears

List of usage examples for org.joda.time Period getYears

Introduction

In this page you can find the example usage for org.joda.time Period getYears.

Prototype

public int getYears() 

Source Link

Document

Gets the years field part of the period.

Usage

From source file:info.culebrasgis.calculadoradefechas.IntervaloActivity.java

License:Open Source License

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intervalo);

    fechaUsuario = (Button) findViewById(R.id.buttonFechaUsuario);
    fechaUsuario.setOnClickListener(new View.OnClickListener() {
        @Override//from   ww w .  j  a  va  2 s .  co m
        public void onClick(View v) {
            DialogFragment newFragment = new DatePickerFragment();
            newFragment.show(getSupportFragmentManager(), "datePicker");
        }
    });

    fechaUsuario2 = (Button) findViewById(R.id.buttonFechaUsuario2);
    fechaUsuario2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment newFragment = new DatePickerFragment2();
            newFragment.show(getSupportFragmentManager(), "datePicker");
        }
    });

    resultado = (TextView) findViewById(R.id.textViewResultado);

    volver = (Button) findViewById(R.id.buttonVolver);
    volver.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    reiniciar = (Button) findViewById(R.id.buttonReiniciar);
    reiniciar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            resultado.setText(R.string.resultado);
            fechaUsuario.setText(R.string.fecha);
            fechaUsuario2.setText(R.string.fecha);
        }
    });

    calcular = (Button) findViewById(R.id.buttonCalcular);
    calcular.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                int dia, mes, anno, dia2, mes2, anno2;
                dia = Integer.parseInt(String.valueOf(fechaUsuario.getText()).substring(0, 2));
                mes = Integer.parseInt(String.valueOf(fechaUsuario.getText()).substring(3, 5));
                anno = Integer.parseInt(String.valueOf(fechaUsuario.getText()).substring(6, 10));

                dia2 = Integer.parseInt(String.valueOf(fechaUsuario2.getText()).substring(0, 2));
                mes2 = Integer.parseInt(String.valueOf(fechaUsuario2.getText()).substring(3, 5));
                anno2 = Integer.parseInt(String.valueOf(fechaUsuario2.getText()).substring(6, 10));

                DateTime fecha = new DateTime(anno, mes, dia, 0, 0);
                DateTime fecha2 = new DateTime(anno2, mes2, dia2, 0, 0);

                Period periodo;

                if (fecha.isBefore(fecha2)) { // si la primera fecha es anterior
                    periodo = new Period(fecha, fecha2, PeriodType.yearMonthDay());
                } else { // si la que es anterior es la segunda fecha
                    periodo = new Period(fecha2, fecha, PeriodType.yearMonthDay());
                }

                resultado.setText(String.format(getString(R.string.resultado_intervalo), periodo.getYears(),
                        periodo.getMonths(), periodo.getDays()));

            } catch (Exception e) {
                resultado.setText(R.string.fechas_incorrectas);
            }
        }
    });

}

From source file:io.coala.xml.XmlUtil.java

License:Apache License

/**
 * @param period/*from ww  w .ja  va2  s . com*/
 * @return a JAXP {@link Duration}
 */
public static Duration toDuration(final Period period) {
    return getDatatypeFactory().newDuration(true, period.getYears(), period.getMonths(), period.getDays(),
            period.getHours(), period.getMinutes(), period.getSeconds());
}

From source file:io.druid.sql.calcite.expression.builtin.TimeFloorOperatorConversion.java

License:Apache License

private static boolean periodIsDayMultiple(final Period period) {
    return period.getMillis() == 0 && period.getSeconds() == 0 && period.getMinutes() == 0
            && period.getHours() == 0 && (period.getDays() > 0 || period.getWeeks() > 0
                    || period.getMonths() > 0 || period.getYears() > 0);
}

From source file:io.konig.dao.core.ChartWriterFactory.java

License:Apache License

private Formatter categoryFormatter(Chart chart) {

    ChartCategories categories = chart.getCategories();
    if (categories instanceof DateTimeCategories) {
        String pattern = null;/*from  www. ja va2 s  .  co m*/
        DateTimeCategories c = (DateTimeCategories) categories;
        Period period = c.getInterval();

        if (period.getHours() > 0) {
            pattern = HOUR;
        } else if (period.getDays() > 0) {
            pattern = DATE;
        } else if (period.getMonths() > 0) {
            pattern = MONTH;
        } else if (period.getYears() > 0) {
            pattern = YEAR;
        } else {
            throw new RuntimeException("Unsupported period: " + period.toString());
        }
        return new TemporalFormatter(DateTimeFormat.forPattern(pattern));
    }
    if (categories instanceof LabelCategories) {
        return new Formatter() {
            @Override
            public String format(Object value) {
                return (String) value;
            }
        };
    }
    // TODO: support other categories
    throw new RuntimeException("Category type not supported");

}

From source file:io.warp10.script.functions.DURATION.java

License:Apache License

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {

    Object o = stack.pop();// w  w w . ja va 2 s.  c o  m

    if (!(o instanceof String)) {
        throw new WarpScriptException(getName()
                + " expects an ISO8601 duration (a string) on top of the stack. See http://en.wikipedia.org/wiki/ISO_8601#Durations");
    }

    ReadWritablePeriod period = new MutablePeriod();

    ISOPeriodFormat.standard().getParser().parseInto(period, o.toString(), 0, Locale.US);

    Period p = period.toPeriod();

    if (p.getMonths() != 0 || p.getYears() != 0) {
        throw new WarpScriptException(getName()
                + " doesn't support ambiguous durations containing years or months, please convert those to days.");
    }

    Duration duration = p.toDurationFrom(new Instant());

    stack.push(duration.getMillis() * Constants.TIME_UNITS_PER_MS);

    return stack;
}

From source file:ipr.projekt.dialogi.ZalozKontoDialog.java

private boolean mniejNiz18(int urDzien, int urMiesiac, int urRok) {

    DateTime now = new DateTime();
    DateTime dataUrodzenia = new DateTime(urRok, urMiesiac, urDzien, 0, 0);
    Period period = new Period(dataUrodzenia, now);
    if (period.getYears() < 18) {
        return true;
    }//from   w w  w .j  ava 2s  .  co m

    return false;
}

From source file:it.f2informatica.core.model.builder.ConsultantModelBuilder.java

License:Apache License

public ConsultantModelBuilder withBirthDate(Date birthDate) {
    consultant.setBirthDate(birthDate);//  w w w  .  j  a  v a2s  .  c  o m
    if (birthDate != null) {
        Period periodFromBirthdayToday = new Period(new DateTime(birthDate), new DateTime());
        consultant.setAge(periodFromBirthdayToday.getYears());
    }
    return this;
}

From source file:it.f2informatica.webapp.utils.PeriodParser.java

License:Apache License

private String appendYears(Period period) {
    int years = period.getYears();
    if (years <= 0) {
        return "";
    }/*from   ww w.  j a va2  s  .  c  o  m*/
    return String.valueOf(years) + " " + ((years == 1) ? getMessage("global.year") : getMessage("global.years"))
            + " ";
}

From source file:name.gluino.webmailfeed.ClubMember.java

License:Open Source License

public int getAge() {
    if (birthday != null) {
        Interval iv = new Interval(birthday, new DateTime());
        Period p = iv.toPeriod();
        return p.getYears();
    } else {//w  ww  . jav  a2s.c  om
        // some default value...
        return 30;
    }
}

From source file:net.longfalcon.view.DateView.java

License:Open Source License

public Period roundPeriod(Period period) {
    int fieldCount = 0;
    int years = period.getYears();
    int months = period.getMonths();
    int weeks = period.getWeeks();
    int days = period.getDays();
    int hours = period.getHours();
    int minutes = period.getMinutes();
    int seconds = period.getSeconds();
    if (years > 0) {
        fieldCount++;//  www. ja  va 2 s.c  o m
    }
    if (months > 0) {
        fieldCount++;
    }

    if (fieldCount > 1) {
        return new Period(years, months, 0, 0, 0, 0, 0, 0);
    }

    if (weeks > 0) {
        fieldCount++;
    }

    if (fieldCount > 1) {
        return new Period(0, months, weeks, 0, 0, 0, 0, 0);
    }

    if (days > 0) {
        fieldCount++;
    }

    if (fieldCount > 1) {
        return new Period(0, 0, weeks, days, 0, 0, 0, 0);
    }

    if (hours > 0) {
        fieldCount++;
    }

    if (fieldCount > 1) {
        return new Period(0, 0, 0, days, hours, 0, 0, 0);
    }

    if (minutes > 0) {
        fieldCount++;
    }

    if (fieldCount > 1) {
        return new Period(0, 0, 0, 0, hours, minutes, 0, 0);
    }

    return new Period(0, 0, 0, 0, 0, minutes, seconds, 0);
}