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 Date beginOfTheDay(@NonNull Date date) {
    Calendar inputDate = Calendar.getInstance();
    inputDate.setTime(date);

    Calendar resultDate = Calendar.getInstance();
    resultDate.set(inputDate.get(Calendar.YEAR), inputDate.get(Calendar.MONTH),
            inputDate.get(Calendar.DAY_OF_YEAR));
    return resultDate.getTime();
}

From source file:Main.java

public static int[] getStartEndDay(Date[] startEnd) {
    int[] startEndDay = new int[2];
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(startEnd[0]);
    startEndDay[0] = calendar.get(Calendar.DAY_OF_MONTH);
    calendar.setTime(startEnd[1]);//from  ww  w  .ja  v  a 2  s . c  om
    startEndDay[1] = calendar.get(Calendar.DAY_OF_MONTH);

    return startEndDay;
}

From source file:Main.java

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

    if (c.get(Calendar.MINUTE) >= 30)
        c.add(Calendar.HOUR, 1);/*from www.j  a va  2  s  . co  m*/

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

    return c;
}

From source file:Main.java

public static String getShortMonthStringOnlyFromDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    SimpleDateFormat format = new SimpleDateFormat("MMM", Locale.US);

    return format.format(date);
}

From source file:Main.java

public static java.util.Date getDateStart(java.util.Date d) {

    Calendar c = new GregorianCalendar();
    c.clear();//  ww  w  .j a  v  a2  s .  c  om
    Calendar co = new GregorianCalendar();
    co.setTime(d);
    c.set(Calendar.DAY_OF_MONTH, co.get(Calendar.DAY_OF_MONTH));
    c.set(Calendar.MONTH, co.get(Calendar.MONTH));
    c.set(Calendar.YEAR, co.get(Calendar.YEAR));
    // c.add(Calendar.DAY_OF_MONTH,1);
    // c.add(Calendar.MILLISECOND,-1);
    return c.getTime();
}

From source file:Main.java

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

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

    c.set(Calendar.SECOND, 0);/*from   www .j a v a 2s .  c om*/
    c.set(Calendar.MILLISECOND, 0);

    return c.getTime();
}

From source file:Main.java

public static Date getFetchDelayDate(Date startDate, Integer dateDelayMinutes) {
    Calendar c = Calendar.getInstance();
    c.setTime(startDate);
    if (dateDelayMinutes > 0) {
        dateDelayMinutes = -dateDelayMinutes;
    }/*from   w  ww . ja va  2 s  . co m*/
    c.add(Calendar.MINUTE, dateDelayMinutes);
    return c.getTime();
}

From source file:Main.java

public static boolean isPast(Date date) {
    boolean isPast = false;
    Date currentTime = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(currentTime);

    Calendar calCurrentView = Calendar.getInstance();
    calCurrentView.setTime(date);//from   www  . j  ava2 s . c  o m

    isPast = cal.getTime().after(calCurrentView.getTime());

    return isPast;
}

From source file:Main.java

/**
 * Gets Date with UTC time zone/*from  w  ww  .j  ava2s  .c o  m*/
 *
 * @param date is concrete date
 * @return new instance calendar
 */
public static Calendar getCalendarUTC(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    return calendar;
}

From source file:Main.java

public static Calendar initCalendar(Date date) {

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR, 10);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar;
}