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 String date2String(Date date, boolean printTime) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    if (cal.get(Calendar.YEAR) > 10000) {
        return "---";
    }/*from  w w w.ja v a 2 s  .c om*/
    return makeTimeString(cal.get(Calendar.DAY_OF_MONTH), 2) + "."
            + makeTimeString(cal.get(Calendar.MONTH) + 1, 2) + "." + makeTimeString(cal.get(Calendar.YEAR), 4)
            + (printTime
                    ? " " + makeTimeString(cal.get(Calendar.HOUR_OF_DAY), 2) + ":"
                            + makeTimeString(cal.get(Calendar.MINUTE), 2) + ":"
                            + makeTimeString(cal.get(Calendar.SECOND), 2)
                    : "");
}

From source file:Main.java

public static void previousDateString(String dateString) throws ParseException {
    DateFormat inputDateFormat = new SimpleDateFormat("dd-MM-yyyy");
    DateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    Calendar now = Calendar.getInstance();
    Calendar calendar = Calendar.getInstance();
    Date myDate = inputDateFormat.parse(dateString);
    calendar.setTime(myDate);
    calendar.set(Calendar.HOUR_OF_DAY, now.get(Calendar.HOUR_OF_DAY));
    calendar.set(Calendar.MINUTE, now.get(Calendar.MINUTE));

    String output = outputDateFormat.format(calendar.getTime());
    System.out.println(output);/*www.  ja v  a2  s.c o m*/
}

From source file:Main.java

public static int getDayOfWeek(Date date, int defSunday) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    int day = c.get(Calendar.DAY_OF_WEEK);
    switch (day) {
    case Calendar.MONDAY:
        return 1;
    case Calendar.TUESDAY:
        return 2;
    case Calendar.WEDNESDAY:
        return 3;
    case Calendar.THURSDAY:
        return 4;
    case Calendar.FRIDAY:
        return 5;
    case Calendar.SATURDAY:
        return 6;
    case Calendar.SUNDAY:
        return defSunday;
    default://  ww w  .  j av  a  2  s  . c o m
        return 0;
    }
}

From source file:Main.java

public static Date getDate(String crisisDate) {
    try {/*w w  w  .ja  va2s .c  o m*/
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        Date date = (Date) formatter.parse(crisisDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.getTime();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Date getDateAdd(Date date, int amount) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    cal.add(GregorianCalendar.DATE, amount);
    return cal.getTime();
}

From source file:Main.java

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

    cal.setTime(startOfThisWeek());
    cal.add(Calendar.WEEK_OF_YEAR, 1);

    return cal.getTime();
}

From source file:Main.java

/** Helper method - creates a Phoenix date <I>String</I> from a Date object.
   @param dteValue <I>Date</I> object to be converted.
   @return A <I>String</I> that follows the format "YYYY-MM-DDTHH:NN:SS.00000".
 *///from   www  . ja v a 2  s  .c o  m
public static String getXMLStringFromDate(Date dteValue) {
    if (null == dteValue)
        return null;

    Calendar pCalendar = Calendar.getInstance();

    pCalendar.setTime(dteValue);

    // Return the String value.
    // Special treatment for the month because the current implementation
    // values January as 0. Code below should work if that ever changes.
    return pCalendar.get(Calendar.YEAR) + "-" + padNumber(pCalendar.get(Calendar.MONTH) + 1 - Calendar.JANUARY)
            + "-" + padNumber(pCalendar.get(Calendar.DAY_OF_MONTH)) + "T"
            + padNumber(pCalendar.get(Calendar.HOUR_OF_DAY)) + ":" + padNumber(pCalendar.get(Calendar.MINUTE))
            + ":" + padNumber(pCalendar.get(Calendar.SECOND));
}

From source file:Main.java

public static boolean isDateAfterToday(Calendar calendar) {
    Calendar validationCalendar = Calendar.getInstance();
    validationCalendar.setTime(calendar.getTime());
    validationCalendar.set(Calendar.MILLISECOND, todayCalendar.get(Calendar.MILLISECOND));
    validationCalendar.set(Calendar.SECOND, todayCalendar.get(Calendar.SECOND));
    validationCalendar.set(Calendar.MINUTE, todayCalendar.get(Calendar.MINUTE));
    validationCalendar.set(Calendar.HOUR_OF_DAY, todayCalendar.get(Calendar.HOUR_OF_DAY));
    return validationCalendar.getTime().after(todayCalendar.getTime());
}

From source file:Main.java

public static Calendar str2Calendar(String str, String format) {

    Date date = str2Date(str, format);
    if (date == null) {
        return null;
    }/*from   w  ww.j a  va2s  .com*/
    Calendar c = Calendar.getInstance();
    c.setTime(date);

    return c;

}

From source file:Main.java

public static boolean isDateBeforeToday(Calendar calendar) {
    Calendar validationCalendar = Calendar.getInstance();
    validationCalendar.setTime(calendar.getTime());
    validationCalendar.set(Calendar.MILLISECOND, todayCalendar.get(Calendar.MILLISECOND));
    validationCalendar.set(Calendar.SECOND, todayCalendar.get(Calendar.SECOND));
    validationCalendar.set(Calendar.MINUTE, todayCalendar.get(Calendar.MINUTE));
    validationCalendar.set(Calendar.HOUR_OF_DAY, todayCalendar.get(Calendar.HOUR_OF_DAY));
    return validationCalendar.getTime().before(todayCalendar.getTime());
}