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 startOfNextDay(Date origDate) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(origDate);
    cal.add(Calendar.DATE, 1);/*from   w  w w  .  j a va 2 s  . co  m*/

    return startOfDay(cal.getTime());
}

From source file:Main.java

public static int getYearFromDate(String origDate) {
    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
    Date date;//from ww  w.  j  ava2s .c  o m
    int year = 1;
    try {
        date = formatter.parse(origDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        year = cal.get(Calendar.YEAR);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return year;
}

From source file:Main.java

public static String timeToDeparture(String startTime) {
    int diffMinutes = -1;
    Calendar now = Calendar.getInstance();
    now.setTime(new Date());
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date date = null;/*from  w w  w  .j av a 2  s.c o m*/
    try {
        date = dateFormat.parse(startTime);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    long millisDiff = cal.getTimeInMillis() - now.getTimeInMillis();
    if (millisDiff > 0) {
        diffMinutes = ((int) millisDiff / (60 * 1000));
    }
    return String.valueOf(diffMinutes);
}

From source file:Main.java

public static boolean isSameYear(Date targetTime, Date compareTime) {
    if (targetTime == null || compareTime == null) {
        return false;
    }/*from w ww.  j a va2  s  . c  o  m*/

    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 int getDayFromDate(String origDate) {
    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
    Date date;/*w  w  w  .j  a v a2 s.  co m*/
    int day = 1;
    try {
        date = formatter.parse(origDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        day = cal.get(Calendar.DAY_OF_MONTH);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return day;
}

From source file:Main.java

public static Calendar getCalendar(Date date) {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    cal.setTime(date);

    return cal;/*w w w  . j  a  va2  s .c  o  m*/
}

From source file:Main.java

public static int calculateDayDiffOfSameYear(Date targetTime, Date compareTime) {
    if (targetTime == null || compareTime == null) {
        return 0;
    }//w  w  w  .  ja va 2 s.c o m

    Calendar tarCalendar = Calendar.getInstance();
    tarCalendar.setTime(targetTime);
    int tarDayOfYear = tarCalendar.get(Calendar.DAY_OF_YEAR);

    Calendar compareCalendar = Calendar.getInstance();
    compareCalendar.setTime(compareTime);
    int comDayOfYear = compareCalendar.get(Calendar.DAY_OF_YEAR);

    return tarDayOfYear - comDayOfYear;
}

From source file:Main.java

public static boolean isSameDateInCalendar(Calendar calendar, Date date) {
    Calendar dateCalendar = Calendar.getInstance();
    dateCalendar.setTime(date);
    return dateCalendar.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH)
            && dateCalendar.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)
            && dateCalendar.get(Calendar.YEAR) == calendar.get(Calendar.YEAR);
}

From source file:Main.java

/**
 * @param date/*from w  ww .j  av a2s .com*/
 * @return
 */
public static String encodeTime(Date date) {
    StringBuilder result = new StringBuilder("T_");
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int month = cal.get(Calendar.MONTH) + 1;
    int year = cal.get(Calendar.YEAR);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);
    int second = cal.get(Calendar.SECOND);
    //yyyy-MM-DD'T'kk:mm:ss
    result.append(year).append("-").append((month < 10) ? "0" : "").append(month).append("-")
            .append((day < 10) ? "0" : "").append(day).append("T").append((hour < 10) ? "0" : "").append(hour)
            .append(":").append((minute < 10) ? "0" : "").append(minute).append(":")
            .append((second < 10) ? "0" : "").append(second);
    return result.toString();
}

From source file:Main.java

public static Date toTimeZone(Date date, TimeZone source, TimeZone target) {
    Calendar cal1 = Calendar.getInstance(source);
    cal1.setTime(date);

    int[] dateSplit = splitDate(date);

    Calendar cal2 = Calendar.getInstance(target);
    cal2.set(dateSplit[0], dateSplit[1], dateSplit[2], dateSplit[3], dateSplit[4], dateSplit[5]);
    return cal2.getTime();
}