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:Main.java

public static Date firstTimeOfDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(date.getTime());

    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    Date dateBegin = new Date();
    dateBegin.setTime(calendar.getTimeInMillis());
    return dateBegin;
}

From source file:Main.java

public static Date getFloorDate(Date d) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);// www. ja  v  a2  s . c  om

    // Set time fields to zero
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    return cal.getTime();
}

From source file:DateHelper.java

/**
 * Calculating age from a current date/*from ww  w.ja v a 2s.  co m*/
 * 
 * @param current
 * @param birthdate
 * @return Age from the current (arg) date
 */
public static float getAge(final Date current, final Date birthdate) {

    if (birthdate == null) {
        return 0;
    }
    if (current == null) {
        return getAge(birthdate);
    } else {
        final Calendar calend = new GregorianCalendar();
        calend.set(Calendar.HOUR_OF_DAY, 0);
        calend.set(Calendar.MINUTE, 0);
        calend.set(Calendar.SECOND, 0);
        calend.set(Calendar.MILLISECOND, 0);

        calend.setTimeInMillis(current.getTime() - birthdate.getTime());

        float result = 0;
        result = calend.get(Calendar.YEAR) - 1970;
        result += (float) calend.get(Calendar.MONTH) / (float) 12;
        return result;
    }

}

From source file:Main.java

static Calendar toNextWholeHour(Calendar d) {
    Calendar c = new GregorianCalendar();
    c.setTime(d.getTime());/*from w  w w. ja v  a2 s . c  om*/
    c.add(Calendar.HOUR, 1);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    return c;
}

From source file:Main.java

public static Date startOfThisWeek() {
    Calendar cal = Calendar.getInstance();

    int firstDay = cal.getFirstDayOfWeek();
    cal.set(Calendar.DAY_OF_WEEK, firstDay);

    return startOfDay(cal.getTime());
}

From source file:Main.java

static Calendar toNextWholeMinute(Calendar d) {
    Calendar c = new GregorianCalendar();
    c.setTime(d.getTime());/*from ww  w  .  j a  v  a 2s  .  c o  m*/
    c.add(Calendar.MINUTE, 1);
    c.set(Calendar.SECOND, 0);
    return c;
}

From source file:Main.java

public static long getTomorrowZeroTimeInMillis() {
    Calendar calendar = getZeroSecondCalendar();
    calendar.add(Calendar.DAY_OF_YEAR, 1);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    return calendar.getTimeInMillis();
}

From source file:edu.zipcloud.core.util.DateUtil.java

public static Date getEndOfDay(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);/*  w w w.j  a  v  a2s .co m*/
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    calendar.set(Calendar.MILLISECOND, 999);
    return calendar.getTime();
}

From source file:edu.zipcloud.core.util.DateUtil.java

public static Date getStartOfDay(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);//from w w w  .java 2  s .co m
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar.getTime();
}

From source file:Main.java

public static void moveToCalendarHour(Calendar cal, int hour) {
    assertArgumentNotMinusInteger("hour", hour);
    cal.set(Calendar.HOUR_OF_DAY, hour);
}