Example usage for java.util Calendar set

List of usage examples for java.util Calendar set

Introduction

In this page you can find the example usage for java.util Calendar set.

Prototype

public void set(int field, int value) 

Source Link

Document

Sets the given calendar field to the given value.

Usage

From source file:AIR.Common.Utilities.Dates.java

/**
 * @param date//  w w w . ja v  a2  s .  c o m
 * @return returns the midnight time for this date e.g. if date is
 *         "10/11/2012 12:10 pm" then this will return "10/11/2012 00:00 am"
 */
public static Date getStartOfDayDate(Date date) {
    Calendar calendarInstance = Calendar.getInstance();
    calendarInstance.setTime(date);
    calendarInstance.set(Calendar.HOUR_OF_DAY, 0);
    calendarInstance.set(Calendar.MINUTE, 0);
    calendarInstance.set(Calendar.SECOND, 0);
    return calendarInstance.getTime();
}

From source file:AIR.Common.Utilities.Dates.java

/**
 * @param date//from w  ww .j a  va  2s  .  c om
 * @return returns the midnight time for before date ends e.g. if date is
 *         "10/11/2012 12:10 pm" then this will return "10/11/2012 11:59 pm"
 */
public static Date getEndOfDayDate(Date date) {
    Calendar calendarInstance = Calendar.getInstance();
    calendarInstance.setTime(date);
    calendarInstance.set(Calendar.HOUR_OF_DAY, 23);
    calendarInstance.set(Calendar.MINUTE, 59);
    calendarInstance.set(Calendar.SECOND, 0);
    return calendarInstance.getTime();
}

From source file:Main.java

public static void moveToCalendarMillisecond(Calendar cal, int millisecond) {
    assertArgumentNotMinusInteger("millisecond", millisecond);
    cal.set(Calendar.MILLISECOND, millisecond);
}

From source file:Main.java

/**
 * @param startDateTime//w w  w  . j  a v a2  s.co m
 * @param endDateTime
 * @return the difference in days between the second timestamp and first
 */
public static double getDifferenceInDays(Timestamp startDateTime, Timestamp endDateTime) {
    int difference = 0;

    Calendar startCalendar = Calendar.getInstance();
    startCalendar.setTime(startDateTime);

    Calendar endCalendar = Calendar.getInstance();
    endCalendar.setTime(endDateTime);

    // First, get difference in whole days
    Calendar startCompare = Calendar.getInstance();
    startCompare.setTime(startDateTime);
    startCompare.set(Calendar.HOUR_OF_DAY, 0);
    startCompare.set(Calendar.MINUTE, 0);
    startCompare.set(Calendar.SECOND, 0);
    startCompare.set(Calendar.MILLISECOND, 0);

    Calendar endCompare = Calendar.getInstance();
    endCompare.setTime(endDateTime);
    endCompare.set(Calendar.HOUR_OF_DAY, 0);
    endCompare.set(Calendar.MINUTE, 0);
    endCompare.set(Calendar.SECOND, 0);
    endCompare.set(Calendar.MILLISECOND, 0);

    return (endCompare.getTimeInMillis() - startCompare.getTimeInMillis()) / (24 * 60 * 60 * 1000);
}

From source file:net.duckling.ddl.util.Utility.java

public static String getDateShort(Date date) {
    Calendar calToday = Calendar.getInstance();
    calToday.set(Calendar.HOUR_OF_DAY, 0);
    Calendar calYear = Calendar.getInstance();
    calYear.set(Calendar.DAY_OF_YEAR, 1);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);//from w  w w.  j  a  v  a  2  s.  c om
    if (cal.after(calToday)) {
        return (new SimpleDateFormat("HH:mm")).format(cal.getTime());
    } else if (cal.after(calYear)) {
        return (new SimpleDateFormat("MMdd")).format(cal.getTime());
    } else {
        return (new SimpleDateFormat("yyyy")).format(cal.getTime());
    }
}

From source file:Main.java

/**
 *
 * This method is a utility method to create a new java.sql.Date in one line.
 *
 * @param year//from w  w w. ja va  2 s  . c  o  m
 * @param month
 * @param day
 *
 * @return a populated java.sql.Date with the year, month, and day specified, and no values for hour, minute, second,
 *         millisecond
 *
 */
public static java.sql.Date newDate(Integer year, Integer month, Integer day) {

    // test for null arguments
    if (year == null) {
        throw new IllegalArgumentException("Argument 'year' passed in was null.");
    }
    if (month == null) {
        throw new IllegalArgumentException("Argument 'month' passed in was null.");
    }
    if (day == null) {
        throw new IllegalArgumentException("Argument 'day' passed in was null.");
    }

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.DAY_OF_MONTH, day);
    calendar.clear(Calendar.HOUR_OF_DAY);
    calendar.clear(Calendar.MINUTE);
    calendar.clear(Calendar.SECOND);
    calendar.clear(Calendar.MILLISECOND);

    return new java.sql.Date(calendar.getTimeInMillis());
}

From source file:Main.java

public static java.util.Date getDate(int y, int m, int d, boolean inclusive) {
    java.util.Date dt = null;/*from   w w  w . java2 s.  com*/
    Calendar c = Calendar.getInstance();
    c.clear();
    if (c.getActualMinimum(1) <= y && y <= c.getActualMaximum(1)) {
        c.set(1, y);
        if (c.getActualMinimum(2) <= m && m <= c.getActualMaximum(2)) {
            c.set(2, m);
            if (c.getActualMinimum(5) <= d && d <= c.getActualMaximum(5))
                c.set(5, d);
        }
        if (inclusive) {
            c.add(5, 1);
            c.add(14, -1);
        }
        dt = c.getTime();
    }
    return dt;
}

From source file:Main.java

public static Date toNearestWholeMinute(Date d) {
    Calendar c = new GregorianCalendar();
    c.setTime(d);//from   w ww  .  j  a  va2  s . c om

    if (c.get(Calendar.SECOND) >= 30)
        c.add(Calendar.MINUTE, 1);

    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);

    return c.getTime();
}

From source file:Util.java

public static Date getLastMonthLastDate() {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, -1);

    int max = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    calendar.set(Calendar.DAY_OF_MONTH, max);

    return calendar.getTime();
}

From source file:com.linuxbox.enkive.teststats.StatsDayGrainTest.java

@BeforeClass
public static void setUp() throws ParseException, GathererException {
    coll = TestHelper.GetTestCollection();
    client = TestHelper.BuildClient();/*from  w ww  . j  a  v a 2s. c o  m*/
    grain = new DayConsolidator(client);

    List<Map<String, Object>> stats = (new HourConsolidator(client)).consolidateData();
    Map<String, Object> timeMap = new HashMap<String, Object>();
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MILLISECOND, 0);
    cal.set(Calendar.SECOND, 1);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.HOUR, 0);
    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            cal.add(Calendar.DATE, -1);
        }
        timeMap.put(CONSOLIDATION_MAX, cal.getTime());
        timeMap.put(CONSOLIDATION_MIN, cal.getTime());
        for (Map<String, Object> data : stats) {
            data.put(STAT_TIMESTAMP, timeMap);
        }
        client.storeData(stats);
    }
    dataCount = coll.count();
}