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

private static Calendar prepareCalendar(int hourOfDay, int minute) {
    Calendar calNow = Calendar.getInstance();
    Calendar calSet = (Calendar) calNow.clone();

    calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calSet.set(Calendar.MINUTE, minute);
    calSet.set(Calendar.SECOND, 0);
    calSet.set(Calendar.MILLISECOND, 0);

    if (calSet.compareTo(calNow) <= 0) {
        // Today Set time passed, count to tomorrow
        calSet.add(Calendar.DATE, 1);
    }/*  w ww .j a  va2s.  com*/
    return calSet;
}

From source file:Main.java

/**
 * @param time selected in TimePicker//from  w ww . j  a  va2s  .  c o m
 * @return Converted to calendar
 */

public static Calendar timeToCalendar(String time) {
    Calendar calTime = Calendar.getInstance();
    calTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time.split(":")[0]));
    calTime.set(Calendar.MINUTE, Integer.parseInt(time.split(":")[1]));
    return calTime;
}

From source file:Main.java

public static Date getDateAfter(Date d, int day) {
    Calendar now = Calendar.getInstance();
    now.setTime(d);/*ww w .  j a v  a 2  s  .  c  o m*/
    now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
    return now.getTime();
}

From source file:Main.java

/**
 * calculate day count//from   w ww  . j  a v a  2s . c  o  m
 * @param startYear start year
 * @param startMonth start month
 * @param startDay start day
 * @param endYear end year
 * @param endMonth end month
 * @param endDay end day
 * @return day count
 */
public static int countDays(int startYear, int startMonth, int startDay, int endYear, int endMonth,
        int endDay) {
    Calendar startC = Calendar.getInstance();
    startC.set(Calendar.YEAR, startYear);
    startC.set(Calendar.MONTH, startMonth - 1);
    startC.set(Calendar.DAY_OF_MONTH, startDay);
    Calendar endC = Calendar.getInstance();
    endC.set(Calendar.YEAR, endYear);
    endC.set(Calendar.MONTH, endMonth - 1);
    endC.set(Calendar.DAY_OF_MONTH, endDay);
    return (int) ((endC.getTimeInMillis() - startC.getTimeInMillis()) / 86400000 + 1);
}

From source file:Main.java

public static long getStartOfWeek(long week) {
    Calendar calendar = getCalendarWithTime(week);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.clear(Calendar.MINUTE);
    calendar.clear(Calendar.SECOND);
    calendar.clear(Calendar.MILLISECOND);

    // get start of this week in milliseconds
    calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());

    return calendar.getTimeInMillis();
}

From source file:Main.java

public static Calendar getCalendarSetToFirstDayOfNextMonth(Calendar currentMonth) {
    currentMonth.add(Calendar.MONTH, 1);
    currentMonth.set(Calendar.DAY_OF_MONTH, currentMonth.getActualMinimum(Calendar.DAY_OF_MONTH));
    return currentMonth;
}

From source file:Main.java

public static void clearCalendarMinuteWithRear(Calendar cal) {
    cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
    clearCalendarSecondWithRear(cal);/*w ww  .  ja v  a  2s  .co  m*/
}

From source file:Main.java

public static Calendar getCalendarSetToLastDayOfPreviousMonth(Calendar currentMonth) {
    currentMonth.add(Calendar.MONTH, -1);
    currentMonth.set(Calendar.DAY_OF_MONTH, currentMonth.getActualMaximum(Calendar.DAY_OF_MONTH));
    return currentMonth;
}

From source file:Main.java

public static Date dayBegin(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);//from   w  w  w  . j  a v  a2s.  c o m
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return c.getTime();
}

From source file:Main.java

public static void clearCalendarMillisecond(Calendar cal) {
    cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));
}