Example usage for org.joda.time DateTime getMonthOfYear

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

Introduction

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

Prototype

public int getMonthOfYear() 

Source Link

Document

Get the month of year field value.

Usage

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 day. 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  www.  j a  v  a  2s .c  om*/
 * Examples:
 * <ul>
 *      <li>null -> null
 *      <li>"2009-06-24 13:24:51.476 -8:00" -> "2009-06-24 00:00: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 day.
 */
public static DateTime floorToDay(DateTime value) {
    if (value == null) {
        return null;
    }
    return new DateTime(value.getYear(), value.getMonthOfYear(), value.getDayOfMonth(), 0, 0, 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 hour. 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  ww 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:00: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 hour.
 */
public static DateTime floorToHour(DateTime value) {
    if (value == null) {
        return null;
    }
    return new DateTime(value.getYear(), value.getMonthOfYear(), value.getDayOfMonth(), value.getHourOfDay(), 0,
            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 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>/*from   www  .  jav  a  2s  .  c  om*/
 * 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.  j a va2  s  .c om*/
 * 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  a v a 2 s. com*/
 * 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.cloudhopper.commons.util.time.DateTimePeriod.java

License:Apache License

/**
 * Create a list of DateTimePeriods that represent the last year of
 * YearMonth periods.  For example, if its currently January 2009, this
 * would return periods representing "January 2009, December 2008, ... February 2008"
 * @param zone//  w w  w .  j a  v  a2 s.  com
 * @return
 */
static public List<DateTimePeriod> createLastYearMonths(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 = createMonth(now.getYear(), now.getMonthOfYear(), zone);
        periods.add(period);
        // subtract 1 month
        now = now.minusMonths(1);
    }

    return periods;
}

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  . ja  v  a2s  .c  o m
 */
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());
}