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 Calendar parseDate(String date, String format) throws ParseException {
    Calendar lCalendar = Calendar.getInstance();
    SimpleDateFormat formatter = new SimpleDateFormat(format);
    lCalendar.setTime(formatter.parse(date));
    return lCalendar;
}

From source file:com.my373.common.util.MyDateUtils.java

/**
 * 2endDate - beginDate/*from  w ww.  j a  v a2  s  .  co  m*/
 * 
 * @param endDate
 * @param beginDate
 * @return
 */
public static int subDate(Date endDate, Date beginDate) {
    Calendar d1 = Calendar.getInstance();
    d1.setTime(endDate);
    d1 = setTimeToMidnight(d1);

    Calendar d2 = Calendar.getInstance();
    d2.setTime(beginDate);
    d2 = setTimeToMidnight(d2);

    // 1000 * 60 * 60 * 24 = 86400000,??
    return (int) ((d1.getTimeInMillis() - d2.getTimeInMillis()) / 86400000);
}

From source file:Main.java

public static Date nextDay(Date date) {
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    calendar.add(calendar.DATE, 1);//from www  .  j a v  a 2  s .  c  o m
    date = calendar.getTime();
    return date;
}

From source file:gabriel.domain.BirthdateValidatorTest.java

private static Date getDate(int years) {
    Date date = new Date();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(YEAR, calendar.get(YEAR) - years);
    return calendar.getTime();
}

From source file:Main.java

public static String formatDate(Date date) {
    // TODO Auto-generated method stub
    Calendar calender = Calendar.getInstance();
    int thisyear = calender.get(Calendar.YEAR);
    calender.setTime(date);
    int dateyear = calender.get(Calendar.YEAR);
    if (thisyear == dateyear) {
        return DAY.format(date);
    }//  w ww. j  a  va2  s . c om
    return DATE.format(date);
}

From source file:Main.java

public static Calendar parseCalendarString(String dateTimeString) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date date = null;//from w  w w. j a  v  a2s  . co  m
    try {
        date = dateFormat.parse(dateTimeString);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("Europe/Stockholm"));
    cal.setTime(date);
    return cal;
}

From source file:Main.java

private static Date getNextBus(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    if (cal.get(Calendar.DAY_OF_WEEK) >= Calendar.MONDAY
            && cal.get(Calendar.DAY_OF_WEEK) <= Calendar.THURSDAY) {
        if (isBefore(cal, mon_thu[mon_thu.length - 1])) {
            setTime(cal, findNext(cal, mon_thu));
        } else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
            setTime(cal, fri[0]);//  w  ww  .j a  v a  2 s .  co  m
            cal.add(Calendar.DAY_OF_YEAR, 1);
        } else {
            setTime(cal, mon_thu[0]);
            cal.add(Calendar.DAY_OF_YEAR, 1);
        }
    } else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
        if (isBefore(cal, fri[fri.length - 1])) {
            setTime(cal, findNext(cal, fri));
        } else {
            setTime(cal, mon_thu[0]);
            cal.add(Calendar.DAY_OF_YEAR, 3);
        }
    } else {
        setTime(cal, mon_thu[0]);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    }
    return cal.getTime();
}

From source file:org.openmrs.module.uiframeworkpatientsummarysupport.fragment.controller.PatientObsFlowsheetFragmentController.java

/**
 * This method was actually added in core as of 1.9
 *//*from w  w w . j  a  v a  2  s .  c om*/
private static Date startOfDay(Date date) {
    if (date == null)
        return null;

    Calendar c = Calendar.getInstance();
    c.setTime(date);
    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:Util.java

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

    cal.setTime(today);

    int dow = cal.get(Calendar.DAY_OF_WEEK);

    while (dow != Calendar.SUNDAY) {
        int date = cal.get(Calendar.DATE);

        int month = cal.get(Calendar.MONTH);

        int year = cal.get(Calendar.YEAR);

        if (date == getMonthLastDate(month, year)) {

            if (month == Calendar.DECEMBER) {
                month = Calendar.JANUARY;

                cal.set(Calendar.YEAR, year + 1);
            } else {
                month++;/*from  w w w. j a v  a2  s.co  m*/
            }

            cal.set(Calendar.MONTH, month);

            date = 1;
        } else {
            date++;
        }

        cal.set(Calendar.DATE, date);

        dow = cal.get(Calendar.DAY_OF_WEEK);
    }

    return cal.getTime();
}

From source file:Utils.java

public static Date getEndOfMinute(Date day, Calendar cal) {
    if (day == null || cal == null) {
        return day;
    }//from w  w w . j a  va2  s.  c o  m

    cal.setTime(day);
    cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));
    return cal.getTime();
}