List of usage examples for org.joda.time DateTime getYear
public int getYear()
From source file:com.cloudhopper.commons.util.time.DateTimePeriodSelector.java
License:Apache License
static public DateTimePeriod thisMonth(DateTimeZone zone) { DateTime now = new DateTime(zone); return DateTimePeriod.createMonth(now.getYear(), now.getMonthOfYear(), zone); }
From source file:com.cloudhopper.commons.util.time.DateTimePeriodSelector.java
License:Apache License
/** * Create a list of DateTimePeriods that represent the last 12 month periods * based on the current date. The list will be arranged in ascending order * from earliest to latest date. For example, if its currently January 2009, * a list containing "February 2008, March 2008, ... , January 2009" would * be returned. If you need this list in reverse order to show the most * recent month first, just call Collections.reverse() on the returned list. * @param zone The time zone used for calculations * @return A list of the last 12 months// w ww .j a va 2s . com */ static public List<DateTimePeriod> last12Months(DateTimeZone zone) { ArrayList<DateTimePeriod> periods = new ArrayList<DateTimePeriod>(); // get today's date DateTime now = new DateTime(zone); // start with today's current month and 11 others (last 12 months) for (int i = 0; i < 12; i++) { // create a new period DateTimePeriod period = DateTimePeriod.createMonth(now.getYear(), now.getMonthOfYear(), zone); periods.add(period); // subtract 1 month now = now.minusMonths(1); } Collections.reverse(periods); return periods; }
From source file:com.core.beans.HomeBean.java
public String formatDate(Date d) { DateTime time = new DateTime(d); return time.getDayOfMonth() + "/" + (time.getMonthOfYear() < 10 ? "0" : "") + time.getMonthOfYear() + "/" + time.getYear() + " " + (time.getHourOfDay() < 9 ? "0" : "") + time.getHourOfDay() + ":" + (time.getMinuteOfHour() < 9 ? "0" : "") + time.getMinuteOfHour(); }
From source file:com.core.meka.Util.java
public static String now() { DateTime n = new DateTime(new Date()); return n.getDayOfMonth() + "/" + n.getMonthOfYear() + "/" + n.getYear() + " " + (n.getHourOfDay() < 10 ? "0" + n.getHourOfDay() : n.getHourOfDay()) + ":" + (n.getMinuteOfHour() < 10 ? "0" + n.getMinuteOfHour() : n.getMinuteOfHour()); }
From source file:com.core.meka.Util.java
public static String fecha(Date fechaAdquisicion) { String result = "Sin especificar"; if (fechaAdquisicion != null) { DateTime d = new DateTime(fechaAdquisicion); result = d.getDayOfMonth() + "/" + d.getMonthOfYear() + "/" + d.getYear(); }/*from ww w. j av a 2 s . co m*/ return result; }
From source file:com.core.meka.Util.java
public static String fechaLarga(Date fecha) { String result = "Sin especificar"; if (fecha != null) { DateTime d = new DateTime(fecha); result = d.getDayOfMonth() + "/" + d.getMonthOfYear() + "/" + d.getYear() + " " + (d.getHourOfDay() < 10 ? "0" : "") + d.getHourOfDay() + ":" + (d.getMinuteOfHour() < 10 ? "0" : "") + d.getMinuteOfHour() + ":" + (d.getSecondOfMinute() < 10 ? "0" : "") + d.getSecondOfMinute(); }//from ww w . j a v a 2s.c om return result; }
From source file:com.core.meka.Util.java
public static String fechaYhora(Date d) { DateTime n = new DateTime(d); return n.getDayOfMonth() + "/" + n.getMonthOfYear() + "/" + n.getYear() + " " + (n.getHourOfDay() < 10 ? "0" + n.getHourOfDay() : n.getHourOfDay()) + ":" + (n.getMinuteOfHour() < 10 ? "0" + n.getMinuteOfHour() : n.getMinuteOfHour()); }
From source file:com.cronutils.model.time.ExecutionTime.java
License:Apache License
/** * If date is not match, will return next closest match. * If date is match, will return this date. * @param date - reference DateTime instance - never null; * @return DateTime instance, never null. Value obeys logic specified above. * @throws NoSuchValueException//from w ww . j a v a2 s. co m */ DateTime nextClosestMatch(DateTime date) throws NoSuchValueException { List<Integer> year = yearsValueGenerator.generateCandidates(date.getYear(), date.getYear()); TimeNode days = null; int lowestMonth = months.getValues().get(0); int lowestHour = hours.getValues().get(0); int lowestMinute = minutes.getValues().get(0); int lowestSecond = seconds.getValues().get(0); NearestValue nearestValue; DateTime newDate; if (year.isEmpty()) { int newYear = yearsValueGenerator.generateNextValue(date.getYear()); days = generateDays(cronDefinition, new DateTime(newYear, lowestMonth, 1, 0, 0)); return initDateTime(yearsValueGenerator.generateNextValue(date.getYear()), lowestMonth, days.getValues().get(0), lowestHour, lowestMinute, lowestSecond, date.getZone()); } if (!months.getValues().contains(date.getMonthOfYear())) { nearestValue = months.getNextValue(date.getMonthOfYear(), 0); int nextMonths = nearestValue.getValue(); if (nearestValue.getShifts() > 0) { newDate = new DateTime(date.getYear(), 1, 1, 0, 0, 0, date.getZone()) .plusYears(nearestValue.getShifts()); return nextClosestMatch(newDate); } if (nearestValue.getValue() < date.getMonthOfYear()) { date = date.plusYears(1); } days = generateDays(cronDefinition, new DateTime(date.getYear(), nextMonths, 1, 0, 0)); return initDateTime(date.getYear(), nextMonths, days.getValues().get(0), lowestHour, lowestMinute, lowestSecond, date.getZone()); } days = generateDays(cronDefinition, date); if (!days.getValues().contains(date.getDayOfMonth())) { nearestValue = days.getNextValue(date.getDayOfMonth(), 0); if (nearestValue.getShifts() > 0) { newDate = new DateTime(date.getYear(), date.getMonthOfYear(), 1, 0, 0, 0, date.getZone()) .plusMonths(nearestValue.getShifts()); return nextClosestMatch(newDate); } if (nearestValue.getValue() < date.getDayOfMonth()) { date = date.plusMonths(1); } return initDateTime(date.getYear(), date.getMonthOfYear(), nearestValue.getValue(), lowestHour, lowestMinute, lowestSecond, date.getZone()); } if (!hours.getValues().contains(date.getHourOfDay())) { nearestValue = hours.getNextValue(date.getHourOfDay(), 0); int nextHours = nearestValue.getValue(); if (nearestValue.getShifts() > 0) { newDate = new DateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), 0, 0, 0, date.getZone()).plusDays(nearestValue.getShifts()); return nextClosestMatch(newDate); } if (nearestValue.getValue() < date.getHourOfDay()) { date = date.plusDays(1); } return initDateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), nextHours, lowestMinute, lowestSecond, date.getZone()); } if (!minutes.getValues().contains(date.getMinuteOfHour())) { nearestValue = minutes.getNextValue(date.getMinuteOfHour(), 0); int nextMinutes = nearestValue.getValue(); if (nearestValue.getShifts() > 0) { newDate = new DateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), date.getHourOfDay(), 0, 0, date.getZone()).plusHours(nearestValue.getShifts()); return nextClosestMatch(newDate); } if (nearestValue.getValue() < date.getMinuteOfHour()) { date = date.plusHours(1); } return initDateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), date.getHourOfDay(), nextMinutes, lowestSecond, date.getZone()); } if (!seconds.getValues().contains(date.getSecondOfMinute())) { nearestValue = seconds.getNextValue(date.getSecondOfMinute(), 0); int nextSeconds = nearestValue.getValue(); if (nearestValue.getShifts() > 0) { newDate = new DateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), date.getHourOfDay(), date.getMinuteOfHour(), 0, date.getZone()) .plusMinutes(nearestValue.getShifts()); return nextClosestMatch(newDate); } if (nearestValue.getValue() < date.getSecondOfMinute()) { date = date.plusMinutes(1); } return initDateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), date.getHourOfDay(), date.getMinuteOfHour(), nextSeconds, date.getZone()); } return date; }
From source file:com.cronutils.model.time.ExecutionTime.java
License:Apache License
/** * If date is not match, will return previous closest match. * If date is match, will return this date. * @param date - reference DateTime instance - never null; * @return DateTime instance, never null. Value obeys logic specified above. * @throws NoSuchValueException//from w ww . j a v a2 s .c o m */ DateTime previousClosestMatch(DateTime date) throws NoSuchValueException { List<Integer> year = yearsValueGenerator.generateCandidates(date.getYear(), date.getYear()); TimeNode days = generateDays(cronDefinition, date); int highestMonth = months.getValues().get(months.getValues().size() - 1); int highestDay = days.getValues().get(days.getValues().size() - 1); int highestHour = hours.getValues().get(hours.getValues().size() - 1); int highestMinute = minutes.getValues().get(minutes.getValues().size() - 1); int highestSecond = seconds.getValues().get(seconds.getValues().size() - 1); NearestValue nearestValue; DateTime newDate; if (year.isEmpty()) { int previousYear = yearsValueGenerator.generatePreviousValue(date.getYear()); if (highestDay > 28) { int highestDayOfMonth = new DateTime(previousYear, highestMonth, 1, 0, 0).dayOfMonth() .getMaximumValue(); if (highestDay > highestDayOfMonth) { nearestValue = days.getPreviousValue(highestDay, 1); if (nearestValue.getShifts() > 0) { newDate = new DateTime(previousYear, highestMonth, 1, 23, 59, 59) .minusMonths(nearestValue.getShifts()).dayOfMonth().withMaximumValue(); return previousClosestMatch(newDate); } else { highestDay = nearestValue.getValue(); } } } return initDateTime(previousYear, highestMonth, highestDay, highestHour, highestMinute, highestSecond, date.getZone()); } if (!months.getValues().contains(date.getMonthOfYear())) { nearestValue = months.getPreviousValue(date.getMonthOfYear(), 0); int previousMonths = nearestValue.getValue(); if (nearestValue.getShifts() > 0) { newDate = new DateTime(date.getYear(), 12, 31, 23, 59, 59).minusYears(nearestValue.getShifts()); return previousClosestMatch(newDate); } return initDateTime(date.getYear(), previousMonths, highestDay, highestHour, highestMinute, highestSecond, date.getZone()); } if (!days.getValues().contains(date.getDayOfMonth())) { nearestValue = days.getPreviousValue(date.getDayOfMonth(), 0); if (nearestValue.getShifts() > 0) { newDate = new DateTime(date.getYear(), date.getMonthOfYear(), 1, 23, 59, 59) .minusMonths(nearestValue.getShifts()).dayOfMonth().withMaximumValue(); return previousClosestMatch(newDate); } return initDateTime(date.getYear(), date.getMonthOfYear(), nearestValue.getValue(), highestHour, highestMinute, highestSecond, date.getZone()); } if (!hours.getValues().contains(date.getHourOfDay())) { nearestValue = hours.getPreviousValue(date.getHourOfDay(), 0); if (nearestValue.getShifts() > 0) { newDate = new DateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), 23, 59, 59) .minusDays(nearestValue.getShifts()); return previousClosestMatch(newDate); } return initDateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), nearestValue.getValue(), highestMinute, highestSecond, date.getZone()); } if (!minutes.getValues().contains(date.getMinuteOfHour())) { nearestValue = minutes.getPreviousValue(date.getMinuteOfHour(), 0); if (nearestValue.getShifts() > 0) { newDate = new DateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), date.getHourOfDay(), 59, 59).minusHours(nearestValue.getShifts()); return previousClosestMatch(newDate); } return initDateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), date.getHourOfDay(), nearestValue.getValue(), highestSecond, date.getZone()); } if (!seconds.getValues().contains(date.getSecondOfMinute())) { nearestValue = seconds.getPreviousValue(date.getSecondOfMinute(), 0); int previousSeconds = nearestValue.getValue(); if (nearestValue.getShifts() > 0) { newDate = new DateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), date.getHourOfDay(), date.getMinuteOfHour(), 59).minusMinutes(nearestValue.getShifts()); return previousClosestMatch(newDate); } return initDateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), date.getHourOfDay(), date.getMinuteOfHour(), previousSeconds, date.getZone()); } return date; }
From source file:com.cronutils.model.time.ExecutionTime.java
License:Apache License
TimeNode generateDays(CronDefinition cronDefinition, DateTime date) { boolean questionMarkSupported = cronDefinition.getFieldDefinition(DAY_OF_WEEK).getConstraints() .isSpecialCharAllowed(QUESTION_MARK); if (questionMarkSupported) { return new TimeNode(generateDayCandidatesQuestionMarkSupported(date.getYear(), date.getMonthOfYear(), ((DayOfWeekFieldDefinition) cronDefinition.getFieldDefinition(DAY_OF_WEEK)) .getMondayDoWValue())); } else {/*from w ww . j a v a2 s .c om*/ return new TimeNode(generateDayCandidatesQuestionMarkNotSupported(date.getYear(), date.getMonthOfYear(), ((DayOfWeekFieldDefinition) cronDefinition.getFieldDefinition(DAY_OF_WEEK)) .getMondayDoWValue())); } }