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 getDatePart(Date date, int part) {
    if (date == null)
        return 0;
    Calendar cal = Calendar.getInstance(Locale.getDefault());
    cal.setTime(date);
    return cal.get(part);
}

From source file:Main.java

public static String getTimeQuantum(String strDate, String format) {
    Date data = getDateByFormat(strDate, format);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(data);
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    if (hour >= 12) {
        return "PM";
    } else {//from   www  . j av a  2s  .c  o  m
        return "AM";
    }
}

From source file:Main.java

private static void populateDayRanges(Date start, Date end) {
    Calendar startCal = Calendar.getInstance();
    startCal.setTime(start);
    Calendar endCal = Calendar.getInstance();
    endCal.setTime(end);/*from www  .  j  a  v  a  2s .  co  m*/

    sDayNames.clear();
    sDayAbbreviations.clear();

    for (Date date = startCal.getTime(); startCal.before(endCal); startCal.add(Calendar.DATE,
            1), date = startCal.getTime()) {
        sDayNames.add(dayLabelFormatter.format(date));
        sDayAbbreviations.add(dayAbbrevFormatter.format(date));
    }
}

From source file:Main.java

public static Calendar parseDateAndTime(String dateAndTime) {
    Calendar calendar = Calendar.getInstance();
    try {//from   w  ww.j  a  v a2 s . c o m
        calendar.setTime(DATE_AND_TIME_FORMAT.parse(dateAndTime));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return calendar;
}

From source file:Main.java

/**
 * Check if date1 is after date2 or not// ww  w .jav a2s.  c  o  m
 * @return true if date1 after date 2, otherwise false.
 */
public static boolean isBefore(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);

    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);

    if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR)) {
        return true;
    } else if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.MONTH) < cal2.get(Calendar.MONTH)) {
        return true;
    } else if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
            && cal1.get(Calendar.DAY_OF_MONTH) < cal2.get(Calendar.DAY_OF_MONTH)) {
        return true;
    }

    return false;
}

From source file:Main.java

public static XMLGregorianCalendar dateToXmlCalendar(Date date) {
    if (date != null) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        try {// w  w w. j  a  v  a  2  s. c  o  m
            XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
            xmlGregorianCalendar.setDay(calendar.get(Calendar.DAY_OF_MONTH));
            xmlGregorianCalendar.setMonth(calendar.get(Calendar.MONTH) + 1);
            xmlGregorianCalendar.setYear(calendar.get(Calendar.YEAR));
            return xmlGregorianCalendar;
        } catch (DatatypeConfigurationException e) {
            return null;
        }
    }

    return null;
}

From source file:Main.java

public static long getTimeStampStartOfDay(long time) {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    cal.setTime(new Date(time));
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);//from www  .  ja v a2 s .  com
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTimeInMillis();
}

From source file:Main.java

public static List<String> getWeekDate() {
    List<String> list = new ArrayList<String>();
    Calendar calendar = Calendar.getInstance(Locale.CHINA);
    calendar.setTime(new Date());
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    list.add(calendar.get(Calendar.MONTH) + 1 + "." + calendar.get(Calendar.DATE));
    for (int i = 0; i < 6; i++) {
        calendar.add(Calendar.DATE, 1);
        String str = calendar.get(Calendar.MONTH) + 1 + "." + calendar.get(Calendar.DATE);
        list.add(str);//from   ww  w  .ja v a2s .c o  m
    }
    return list;
}

From source file:Util.java

/**
 * <p>Checks if two dates are on the same day ignoring time.</p>
 * @param date1  the first date, not altered, not null
 * @param date2  the second date, not altered, not null
 * @return true if they represent the same day
 * @throws IllegalArgumentException if either date is <code>null</code>
 *///  w  w  w .  j a va 2  s  .  c om
public static boolean isSameDay(Date date1, Date date2) {
    if (date1 == null || date2 == null) {
        throw new IllegalArgumentException("The dates must not be null");
    }
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);
    return isSameDay(cal1, cal2);
}

From source file:Main.java

public static int calculateDayOfWeek(Date date) {
    final Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    calendar.setTime(date);
    return calendar.get(Calendar.DAY_OF_WEEK);
}