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

/**
 * Converts Date into XML-Tag with type xsd:dateTime
 *///from  ww w .j  a v  a  2 s  .  c o m
public static String getXMLDate(final Date date) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return DatatypeConverter.printDateTime(calendar);
}

From source file:Main.java

public static Date getNextDay(Date date) {
    Calendar day = Calendar.getInstance();
    day.setTime(date);
    day.add(Calendar.DAY_OF_MONTH, 1);

    return day.getTime();
}

From source file:Main.java

public static int daysBetween(Date smdate, Date bdate) throws Exception {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    smdate = sdf.parse(sdf.format(smdate));
    bdate = sdf.parse(sdf.format(bdate));
    Calendar cal = Calendar.getInstance();
    cal.setTime(smdate);
    long time1 = cal.getTimeInMillis();
    cal.setTime(bdate);//from ww w.j a  va 2s . com
    long time2 = cal.getTimeInMillis();
    long between_days = (time2 - time1) / (1000 * 3600 * 24);

    return Integer.parseInt(String.valueOf(between_days));
}

From source file:Main.java

/**
 * Returns the previous day of the passed {@link Date}
 *
 * @return The date - 24 hours/*ww w.j a v a  2  s.  co m*/
 */
public static Date getYesterday(Date date) {
    if (date == null) {
        return getYesterday(new Date());
    }

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DAY_OF_WEEK, -1);

    return new Date(cal.getTimeInMillis());
}

From source file:Main.java

public static String getShortFormattedDate(Date date, Locale locale) {
    Calendar calendarToday = Calendar.getInstance();
    Calendar calendarDate = Calendar.getInstance();
    calendarDate.setTime(date);
    if (calendarToday.get(Calendar.DAY_OF_WEEK) == calendarDate.get(Calendar.DAY_OF_WEEK)) {
        return new SimpleDateFormat("HH:mm", locale).format(date);
    } else {//from   w w  w . j  a  v  a2  s. co  m
        return new SimpleDateFormat("dd/MM", locale).format(date);
    }
}

From source file:Main.java

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

    calendar.setTime(date);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);

    calendar.add(Calendar.HOUR_OF_DAY, n);

    return calendar.getTime();
}

From source file:Main.java

public static boolean inSameDay(Date date1, Date Date2) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date1);
    int year1 = calendar.get(Calendar.YEAR);
    int day1 = calendar.get(Calendar.DAY_OF_YEAR);

    calendar.setTime(Date2);//from w  ww  .j  av  a 2 s  .com
    int year2 = calendar.get(Calendar.YEAR);
    int day2 = calendar.get(Calendar.DAY_OF_YEAR);

    return (year1 == year2) && (day1 == day2);
}

From source file:Main.java

/**
 * getDateEnd//from  www .  ja va2 s  .c o  m
 * @param date the date that needs to be converted to an end date
 * @return a date at 11:59 PM
 */
public static Date getDateEnd(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    cal.set(Calendar.MILLISECOND, 59);
    long time = cal.getTimeInMillis();
    date.setTime(time);
    return date;
}

From source file:Main.java

public static boolean isSameYear(Date targetTime, Date compareTime) {
    Calendar tarCalendar = Calendar.getInstance();
    tarCalendar.setTime(targetTime);
    int tarYear = tarCalendar.get(Calendar.YEAR);

    Calendar compareCalendar = Calendar.getInstance();
    compareCalendar.setTime(compareTime);
    int comYear = compareCalendar.get(Calendar.YEAR);

    return tarYear == comYear;
}

From source file:Main.java

public static String getNextDateByMonth(String s, int i) {

    SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd");
    java.util.Date date = simpledateformat.parse(s, new ParsePosition(0));
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(2, i);// ww w  .j a  va 2 s. co m
    date = calendar.getTime();
    s = simpledateformat.format(date);
    return s;
}