Example usage for org.joda.time DateTime getMinuteOfHour

List of usage examples for org.joda.time DateTime getMinuteOfHour

Introduction

In this page you can find the example usage for org.joda.time DateTime getMinuteOfHour.

Prototype

public int getMinuteOfHour() 

Source Link

Document

Get the minute of hour field value.

Usage

From source file:com.clevercloud.bianca.lib.date.DateModule.java

License:Open Source License

/**
 * Returns the parsed date./*w  w w  .j a  va2  s .  c om*/
 */
public Value strptime(Env env, String date, String format) {
    ArrayValueImpl array = new ArrayValueImpl();
    DateTimeFormatterBuilder fb = new DateTimeFormatterBuilder();

    int length = format.length();

    for (int i = 0; i < length; i++) {
        char ch = format.charAt(i);
        if (ch != '%') {
            fb.appendLiteral(ch);
            continue;
        }

        switch (format.charAt(++i)) {
        case 'a':
            fb.appendDayOfWeekShortText();
            break;

        case 'A':
            fb.appendDayOfWeekText();
            break;

        case 'h':
        case 'b':
            fb.appendMonthOfYearShortText();
            ;
            break;

        case 'B':
            fb.appendMonthOfYearText();
            break;

        // TODO: case 'c'

        case 'C':
            fb.appendCenturyOfEra(2, 2);
            break;

        case 'd':
            fb.appendDayOfMonth(2);
            break;

        case 'D':
            fb.appendMonthOfYear(2);
            fb.appendLiteral('/');
            fb.appendDayOfMonth(2);
            fb.appendLiteral('/');
            fb.appendYear(2, 2);
            break;

        // TODO: case 'e'

        case 'F':
            fb.appendYear(4, 4);
            fb.appendLiteral('-');
            fb.appendMonthOfYear(2);
            fb.appendLiteral('-');
            fb.appendDayOfMonth(2);
            break;

        // TODO: case 'g'
        // TODO: case 'G'

        case 'H':
            fb.appendHourOfDay(2);
            break;

        case 'I':
            fb.appendHourOfHalfday(2);
            break;

        case 'j':
            fb.appendDayOfYear(3);
            break;

        // TODO: case 'l'

        case 'm':
            fb.appendMonthOfYear(2);
            break;

        case 'M':
            fb.appendMinuteOfHour(2);
            break;

        case 'n':
            fb.appendLiteral("\n");
            break;

        case 'p':
        case 'P':
            fb.appendHalfdayOfDayText();
            break;

        case 'r':
            fb.appendHourOfHalfday(2);
            fb.appendLiteral(':');
            fb.appendMinuteOfHour(2);
            fb.appendLiteral(':');
            fb.appendSecondOfMinute(2);
            fb.appendLiteral(' ');
            fb.appendHalfdayOfDayText();
            break;

        case 'R':
            fb.appendHourOfDay(2);
            fb.appendLiteral(':');
            fb.appendMinuteOfHour(2);
            break;

        // TODO: case 's'

        case 'S':
            fb.appendSecondOfMinute(2);
            break;

        case 't':
            fb.appendLiteral("\t");
            break;

        case 'T':
            fb.appendHourOfDay(2);
            fb.appendLiteral(':');
            fb.appendMinuteOfHour(2);
            fb.appendLiteral(':');
            fb.appendSecondOfMinute(2);
            break;

        // TODO: case 'u'
        // TODO: case 'U'
        // TODO: case 'V'
        // TODO: case 'w'
        // TODO: case 'W'
        // TODO: case 'x'
        // TODO: case 'X'

        case 'y':
            fb.appendYear(2, 2);
            break;

        case 'Y':
            fb.appendYear(4, 4);
            break;

        case 'z':
            fb.appendTimeZoneOffset(null, true, 2, 2);
            break;

        case 'Z':
            fb.appendTimeZoneName();
            break;

        case '%':
            fb.appendLiteral('%');
            break;

        default:
            fb.appendLiteral(ch);
        }
    }

    DateTimeFormatter dtf = fb.toFormatter().withLocale(Locale.getDefault()).withOffsetParsed();

    org.joda.time.DateTime dt = new org.joda.time.DateTime();

    String unparsed = "";

    try {
        dt = dtf.parseDateTime(date);
    } catch (IllegalArgumentException e) {
        String delims = "[\"]+";

        String[] splits = e.getMessage().split(delims);

        unparsed = unparsed.concat(splits[3]);
    }

    // According to manual strptime(3)
    if (dt.getCenturyOfEra() == 0) {
        if (dt.getYear() > 68) {
            dt = dt.withCenturyOfEra(19);
        } else {
            dt = dt.withCenturyOfEra(20);
        }
    }

    array.put("tm_sec", dt.getSecondOfMinute());
    array.put("tm_min", dt.getMinuteOfHour());
    array.put("tm_hour", dt.getHourOfDay());
    array.put("tm_mday", dt.getDayOfMonth());
    array.put("tm_mon", dt.getMonthOfYear() - 1);
    array.put("tm_year", dt.getYearOfCentury() + ((dt.getCenturyOfEra() - 19) * 100)); // Years since 1900
    array.put("tm_wday", dt.getDayOfWeek() % 7);
    array.put("tm_yday", dt.getDayOfYear() - 1);
    array.put("unparsed", unparsed);

    return array;
}

From source file:com.cloudhopper.commons.util.DateTimeUtil.java

License:Apache License

/**
 * Null-safe method that returns a new instance of a DateTime object rounded
 * downwards to the nearest specified period in minutes. For example, if
 * a period of 5 minutes is requested, a time of "2009-06-24 13:24:51.476 -8:00"
 * would return a datetime of "2009-06-24 13:20:00.000 -8:00". The time zone of the
 * returned DateTime instance will be the same as the argument. Similar to a
 * floor() function on a float.<br>
 * NOTE: While any period in minutes between 1 and 59 can be passed into this
 * method, its only useful if the value can be evenly divided into 60 to make
 * it as useful as possible.<br>//w w w  . j a  va2  s.  c  o  m
 * Examples:
 * <ul>
 *      <li>null -> null
 *      <li>5: "2009-06-24 13:39:51.476 -8:00" -> "2009-06-24 13:35:00.000 -8:00"
 *      <li>10: "2009-06-24 13:39:51.476 -8:00" -> "2009-06-24 13:30:00.000 -8:00"
 *      <li>15: "2009-06-24 13:39:51.476 -8:00" -> "2009-06-24 13:30:00.000 -8:00"
 *      <li>20: "2009-06-24 13:39:51.476 UTC" -> "2009-06-24 13:20:00.000 UTC"
 * </ul>
 * @param value The DateTime value to round downward
 * @return Null if the argument is null or a new instance of the DateTime
 *      value rounded downwards to the nearest period in minutes.
 */
public static DateTime floorToMinutePeriod(DateTime value, int periodInMinutes) {
    if (value == null) {
        return null;
    }
    if (periodInMinutes <= 0 || periodInMinutes > 59) {
        throw new IllegalArgumentException("period in minutes must be > 0 and <= 59");
    }
    int min = value.getMinuteOfHour();
    min = (min / periodInMinutes) * periodInMinutes;
    return new DateTime(value.getYear(), value.getMonthOfYear(), value.getDayOfMonth(), value.getHourOfDay(),
            min, 0, 0, value.getZone());
}

From source file:com.cloudhopper.commons.util.DateTimeUtil.java

License:Apache License

/**
 * Null-safe method that returns a new instance of a DateTime object rounded
 * downwards to the nearest minute. The time zone of the returned DateTime
 * instance will be the same as the argument. Similar to a floor() function
 * on a float.<br>/*from  w ww . ja v  a 2  s  .  co m*/
 * Examples:
 * <ul>
 *      <li>null -> null
 *      <li>"2009-06-24 13:24:51.476 -8:00" -> "2009-06-24 13:24:00.000 -8:00"
 * </ul>
 * @param value The DateTime value to round downward
 * @return Null if the argument is null or a new instance of the DateTime
 *      value rounded downwards to the nearest minute.
 */
public static DateTime floorToMinute(DateTime value) {
    if (value == null) {
        return null;
    }
    return new DateTime(value.getYear(), value.getMonthOfYear(), value.getDayOfMonth(), value.getHourOfDay(),
            value.getMinuteOfHour(), 0, 0, value.getZone());
}

From source file:com.cloudhopper.commons.util.DateTimeUtil.java

License:Apache License

/**
 * Null-safe method that returns a new instance of a DateTime object rounded
 * downwards to the nearest second. The time zone of the returned DateTime
 * instance will be the same as the argument. Similar to a floor() function
 * on a float.<br>/*from w w w  .  j  ava  2 s .c o  m*/
 * Examples:
 * <ul>
 *      <li>null -> null
 *      <li>"2009-06-24 13:24:51.476 -8:00" -> "2009-06-24 13:24:51.000 -8:00"
 * </ul>
 * @param value The DateTime value to round downward
 * @return Null if the argument is null or a new instance of the DateTime
 *      value rounded downwards to the nearest second.
 */
public static DateTime floorToSecond(DateTime value) {
    if (value == null) {
        return null;
    }
    return new DateTime(value.getYear(), value.getMonthOfYear(), value.getDayOfMonth(), value.getHourOfDay(),
            value.getMinuteOfHour(), value.getSecondOfMinute(), 0, value.getZone());
}

From source file:com.collective.celos.Util.java

License:Apache License

public static boolean isFullHour(DateTime dt) {
    return isFullMinute(dt) && dt.getMinuteOfHour() == 0;
}

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 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();
    }/*  w  w w  . j  av  a  2  s  .co  m*/
    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/*www . j a va  2 s.c  o  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;
}