Example usage for java.util Calendar setTime

List of usage examples for java.util Calendar setTime

Introduction

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

Prototype

public final void setTime(Date date) 

Source Link

Document

Sets this Calendar's time with the given Date.

Usage

From source file:Main.java

public static int getFirstDayOfWeek(Date date) {
    int n = getDayOfWeek(date);
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) - n + 1);
    return c.get(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

public static String getDate(int hour, int min) {
    Date d = new Date();
    Calendar now = Calendar.getInstance();
    now.setTime(d);
    now.set(Calendar.HOUR, now.get(Calendar.HOUR) + hour);
    now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + min);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH/mm");

    return sdf.format(now.getTime());
}

From source file:Main.java

public static String toDate(Date date, int add) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.DATE, add);
    return toDate(calendar.getTime());
}

From source file:Main.java

public static Date moveToToday(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);

    Calendar result = Calendar.getInstance();
    result.set(Calendar.HOUR_OF_DAY, hour);
    result.set(Calendar.MINUTE, minute);
    return result.getTime();
}

From source file:Main.java

static Calendar toNearestWholeMinute(Calendar d) {
    Calendar c = new GregorianCalendar();
    c.setTime(d.getTime());

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

    c.set(Calendar.SECOND, 0);//from  w ww .j  a  v  a  2  s. c o m

    return c;
}

From source file:Main.java

public static Date datePlusNDay(Date date, int n) {
    Calendar calendar = Calendar.getInstance();

    calendar.setTime(date);
    calendar.add(Calendar.DAY_OF_YEAR, n);

    return calendar.getTime();
}

From source file:Util.java

public static String getFilesystemPathFromDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    NumberFormat format = new DecimalFormat("00");
    return File.separator + format.format(calendar.get(Calendar.YEAR)) + File.separator
            + format.format((calendar.get(Calendar.MONTH) + 1)) + File.separator
            + format.format(calendar.get(Calendar.DAY_OF_MONTH)) + File.separator;
}

From source file:Main.java

public static Date nextMonth(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(getFirstDate(date));
    calendar.add(Calendar.MONTH, 1);
    return calendar.getTime();
}

From source file:Main.java

/**
 * This method adds days to a date/*from   w  w w .j a  va 2 s .  c  o m*/
 *
 * @param date the date.
 * @param days the number of days to add.
 */
public static Date getDateAfterAddition(Date date, int days) {
    Calendar cal = Calendar.getInstance();

    cal.setTime(date);
    cal.add(Calendar.DATE, days);

    return cal.getTime();
}

From source file:Main.java

public static int daysOfTwo(Date fDate, Date oDate) {
    Calendar aCalendar = Calendar.getInstance();
    aCalendar.setTime(fDate);
    int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
    aCalendar.setTime(oDate);/*w w  w.  j av a 2s  . c  om*/
    int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
    return day2 - day1;
}