Example usage for java.util GregorianCalendar setTime

List of usage examples for java.util GregorianCalendar setTime

Introduction

In this page you can find the example usage for java.util GregorianCalendar 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 timeFormat(Date date, String format) {
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(date);
    java.text.SimpleDateFormat fm = null;
    if (format != null) {
        fm = new java.text.SimpleDateFormat(format);
    } else {/*from  w w  w  .  j a va2  s  .  co m*/
        fm = new java.text.SimpleDateFormat(DEFAULT_FORMAT);
    }
    return fm.format(gc.getTime());
}

From source file:Main.java

/**
 * Method to check if a date is this week. Note that it checks if it is this
 * week and not 7 days ahead!/*from ww  w .  j a va  2  s.c  o  m*/
 * 
 * @param theDate
 *            the date to see if it is this week
 * @return true if it is this week
 */
public static boolean isThisWeek(Date theDate) {
    // Get a calendar with the events start time
    GregorianCalendar theCalendar = new GregorianCalendar();
    theCalendar.setTime(theDate);
    // Get a calendar with current system time
    Calendar tmpDate = Calendar.getInstance();

    return tmpDate.get(Calendar.YEAR) == theCalendar.get(Calendar.YEAR)
            && tmpDate.get(Calendar.WEEK_OF_YEAR) == theCalendar.get(Calendar.WEEK_OF_YEAR);
}

From source file:Main.java

/**
 * Method to check if a date is today. Also returns true if the date is
 * earlier than today/*w  w  w  .  ja  v a 2  s  .c o  m*/
 * 
 * @param theDate
 *            the date to see if it is today
 * @return true if theDate is today or earlier
 */
public static boolean isToday(Date theDate) {
    // Get a calendar with the events start time
    GregorianCalendar theCalendar = new GregorianCalendar();
    theCalendar.setTime(theDate);
    // Get a calendar with current system time to compare with
    Calendar systemCalendar = Calendar.getInstance();
    // If it should return true only if today and not before use == instead
    // of >=
    return systemCalendar.get(Calendar.YEAR) >= theCalendar.get(Calendar.YEAR)
            && systemCalendar.get(Calendar.DAY_OF_YEAR) >= theCalendar.get(Calendar.DAY_OF_YEAR);
}

From source file:Main.java

/**
 * Method to check if a date is tomorrow
 * /*w ww  .  ja va  2  s .co m*/
 * @param theDate
 *            the date to see if it is tomorrow
 * @return true of it is tomorrow
 */
public static boolean isTomorrow(Date theDate) {
    // Get a calendar with the events start time
    GregorianCalendar theCalendar = new GregorianCalendar();
    theCalendar.setTime(theDate);
    // Get a calendar with current system time and change its value so it
    // can be used in comparison with the given date.
    Calendar tmpDate = Calendar.getInstance();
    tmpDate.roll(Calendar.DAY_OF_YEAR, true);

    return tmpDate.get(Calendar.YEAR) == theCalendar.get(Calendar.YEAR)
            && tmpDate.get(Calendar.DAY_OF_YEAR) == theCalendar.get(Calendar.DAY_OF_YEAR);
}

From source file:Main.java

/**
 * Method to see if a date has already passed. the time of the day has no
 * relevance./*from  w  ww  . j a v a 2 s.  c om*/
 * 
 * @param theDate
 *            the date to see if it has passed
 * @return true if the date has passed
 */
public static boolean dateHasPassed(Date theDate) {
    // Get a calendar with the events start time
    GregorianCalendar theCalendar = new GregorianCalendar();
    theCalendar.setTime(theDate);
    // Get a calendar with current system time to compare with
    Calendar systemCalendar = Calendar.getInstance();
    // If it should return true only if today and not before use == instead
    // of >=
    return systemCalendar.get(Calendar.YEAR) > theCalendar.get(Calendar.YEAR)
            || (systemCalendar.get(Calendar.YEAR) == theCalendar.get(Calendar.YEAR)
                    && systemCalendar.get(Calendar.DAY_OF_YEAR) > theCalendar.get(Calendar.DAY_OF_YEAR));
}

From source file:Main.java

public static String DateToXSD_DateTime(Date d) {
    DatatypeFactory f;//from   w  ww  . ja  v a 2  s.  co  m
    try {
        f = DatatypeFactory.newInstance();
    } catch (DatatypeConfigurationException e) {
        throw new RuntimeException(e);
    }

    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(d);

    return f.newXMLGregorianCalendar(cal).toXMLFormat();
}

From source file:Main.java

public static XMLGregorianCalendar asXMLGregorianCalendar(java.util.Date date) {
    if (date == null) {
        return null;
    } else {/*from  w w  w.j  av a 2 s .com*/
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);

        XMLGregorianCalendar xmlGregorianCalendar = df.newXMLGregorianCalendar();
        xmlGregorianCalendar.setDay(calendar.get(Calendar.DAY_OF_MONTH));
        xmlGregorianCalendar.setMonth(calendar.get(Calendar.MONTH));
        xmlGregorianCalendar.setYear(calendar.get(Calendar.YEAR));

        return xmlGregorianCalendar;
    }
}

From source file:Main.java

public static GregorianCalendar getCalFromString(String data) {
    dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.ITALIAN);
    try {//from  w  ww  .jav a 2  s  .com
        Date dateTime = dateFormat.parse(data);
        GregorianCalendar gregCalendar = new GregorianCalendar();
        gregCalendar.setTime(dateTime);
        return gregCalendar;
    } catch (ParseException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static XMLGregorianCalendar toXMLGregorianCalendar(Date date) {
    if (date == null)
        return null;
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    return newInstance.newXMLGregorianCalendar(calendar);
}

From source file:Main.java

public static XMLGregorianCalendar toXMLGregorianCalendar(Timestamp t) {
    if (t == null)
        return null;
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(t);
    return newInstance.newXMLGregorianCalendar(calendar);
}