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

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

From source file:Main.java

public static Date startOfDay(Date origDate) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(origDate);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);//from  w  w w  .jav  a2 s . c  o m
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    return cal.getTime();
}

From source file:Main.java

public static Calendar StringToCal(String date) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm", Locale.KOREA);
    Date scheduleTime = null;//from   ww w .ja  v  a 2  s. c o m
    try {
        scheduleTime = sdf.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    Calendar cal = Calendar.getInstance();
    cal.setTime(scheduleTime);

    return cal;

}

From source file:Main.java

public static int[] splitDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return splitDate(calendar);
}

From source file:Main.java

public static String getNowTime(int i) {
    SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
    Date beginDate = new Date();
    Calendar date = Calendar.getInstance();
    date.setTime(beginDate);
    date.set(Calendar.DATE, date.get(Calendar.DATE) - i);
    Date endDate = null;//from  www. ja  v a  2  s .  co  m
    try {
        endDate = dft.parse(dft.format(date.getTime()));
        String format = dft.format(endDate);
        return format;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static int getDayCounts(Date startDate, Date endDate) {
    int days = 0;
    Calendar startCalendar = Calendar.getInstance();
    startCalendar.setTime(startDate);
    Calendar endCalendar = Calendar.getInstance();
    endCalendar.setTime(endDate);//  w w w  .ja v a 2 s.  co  m
    while (startCalendar.before(endCalendar)) {
        days++;
        startCalendar.add(Calendar.DATE, 1);
    }
    return days;
}

From source file:Main.java

public static int getDayOFYear(String date) {
    try {/*from   ww w  .  j  a v a 2s .c om*/
        Date parse = yearFormat.parse(date);
        Calendar instance = Calendar.getInstance();
        instance.setTime(parse);
        return instance.get(Calendar.DAY_OF_YEAR);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return 0;
}

From source file:Main.java

public static Calendar stringToCalendar(String string) {
    try {// ww  w.  j  ava  2 s  .com
        SimpleDateFormat formatter = new SimpleDateFormat("yyy-MM-dd HH:mm", Locale.CHINESE);
        Date date = formatter.parse(string);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Calendar timeStringToCalendar(String timeString) {
    try {/*from w  ww  .ja  v a  2s.c om*/
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm", Locale.CHINESE);
        Date date = formatter.parse(timeString);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * @param date//from   w  ww  .  ja  v a  2  s. c om
 * @return
 */
private static String getDayFromDate(Date date) {
    if (date == null) {
        return null;
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
}