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 truncateDate(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR, 0);/*w ww .j a  v a  2 s.  co  m*/
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
}

From source file:Main.java

/**
 * @param date//from  ww w.  j a  va2s .  c  o m
 * @return Returns month of the argument date. The returned values is based
 *         on zero-index i.e. for month January, the values returned is "0"
 */
private static String getMonthFromDate(Date date) {
    if (date == null) {
        return null;
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return String.valueOf(cal.get(Calendar.MONTH));
}

From source file:Main.java

/**
 * Convert a date/time into milliseconds.
 *
 * @param date//from   w  w w.j  ava 2  s .  c  o  m
 * @return
 */
public static long dateToMilliseconds(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.getTimeInMillis();
}

From source file:Main.java

public static Date setDateTime(Date originalDate, int hour, int minute, int second) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(originalDate);
    cal.set(Calendar.HOUR, hour);
    cal.set(Calendar.MINUTE, minute);
    cal.set(Calendar.SECOND, second);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
}

From source file:Main.java

public static Date weekDayOfWeek(Date startWeekDate, int day) {
    Calendar calendar = Calendar.getInstance(Locale.GERMANY);
    calendar.setTime(startWeekDate);
    calendar.add(Calendar.DAY_OF_YEAR, day);

    return calendar.getTime();
}

From source file:Main.java

/**
 * Returns a date formatted in ANSI SQL.
 *
 * @param date the Date./*  w  w w . j  a  va2  s  .c  om*/
 * @return a date String.
 */
public static String getSqlDateString(Date date) {
    Calendar cal = Calendar.getInstance();

    cal.setTime(date);

    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int day = cal.get(Calendar.DAY_OF_MONTH);

    String yearString = String.valueOf(year);
    String monthString = month < 10 ? "0" + month : String.valueOf(month);
    String dayString = day < 10 ? "0" + day : String.valueOf(day);

    return yearString + "-" + monthString + "-" + dayString;
}

From source file:Main.java

/**
 * date -> XMLGregorianCalendar/* w  w w  .j a  v  a 2 s. c om*/
 *
 * @param date
 * @return XMLGregorianCalendar
 * @throws DatatypeConfigurationException
 */
@SuppressWarnings("static-access")
public static XMLGregorianCalendar getXMLGregorianCalendar(java.util.Date date)
        throws DatatypeConfigurationException {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    DatatypeFactory dtf = DatatypeFactory.newInstance();
    return dtf.newXMLGregorianCalendar(calendar.get(calendar.YEAR), calendar.get(calendar.MONTH) + 1,
            calendar.get(calendar.DAY_OF_MONTH), calendar.get(calendar.HOUR), calendar.get(calendar.MINUTE),
            calendar.get(calendar.SECOND), calendar.get(calendar.MILLISECOND),
            calendar.get(calendar.ZONE_OFFSET) / (1000 * 60));
}

From source file:Main.java

public static boolean isCardExpired(String expiry, Date referenceDate) {
    boolean expired = false;

    int monthExpiry = Integer.parseInt(expiry.substring(0, 2));
    int yearExpiry = 2000 + Integer.parseInt(expiry.substring(2, 4));

    Calendar c = Calendar.getInstance();
    c.setTime(referenceDate);

    int monthRef = c.get(Calendar.MONTH) + 1; // months indexed from 0 in java
    int yearRef = c.get(Calendar.YEAR);

    if (yearExpiry < yearRef) {
        expired = true;// w  ww  .j  a va  2 s . co  m
    } else if (yearExpiry == yearRef && monthExpiry < monthRef) {
        expired = true;
    }

    return expired;
}

From source file:Main.java

public static String parseDateToLongForm(String origDate) {
    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
    Date date;//  ww  w. j av  a  2  s . c  o  m
    String newDate = "";
    try {
        date = formatter.parse(origDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        newDate = String.format("%1$tA, %1$tb %1$td, %1$tY", cal);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return newDate;
}

From source file:Main.java

public static int getMonthFromDate(String origDate) {
    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
    Date date;/*from   ww w  . j  av a 2  s . c  o m*/
    int month = 1;
    try {
        date = formatter.parse(origDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        month = cal.get(Calendar.MONTH);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return month;
}