Example usage for java.util GregorianCalendar GregorianCalendar

List of usage examples for java.util GregorianCalendar GregorianCalendar

Introduction

In this page you can find the example usage for java.util GregorianCalendar GregorianCalendar.

Prototype

public GregorianCalendar() 

Source Link

Document

Constructs a default GregorianCalendar using the current time in the default time zone with the default Locale.Category#FORMAT FORMAT locale.

Usage

From source file:Main.java

/**
 *
 * @param date//from  w ww .  ja  va 2s .c  o  m
 * @return Converts a java.util.Date into an instance of
 * XMLGregorianCalendar
 */
public static XMLGregorianCalendar asXMLGregorianCalendar(java.util.Date date) {
    if (date == null) {
        return null;
    } else {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(date.getTime());
        return df.newXMLGregorianCalendar(gc);
    }
}

From source file:Main.java

/**
 * Method to see if a date has already passed. the time of the day has no
 * relevance.//from w  w w .j  a v  a2s .  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

/**
 * Method to check if a date is today. Also returns true if the date is
 * earlier than today//from  w  w w  .  ja v  a2  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 this week. Note that it checks if it is this
 * week and not 7 days ahead!/*from   w w w  .ja v a2 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

public static GregorianCalendar getCalFromString(String data) {
    dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.ITALIAN);
    try {//from w  w w.  ja  v a  2s. co  m
        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 Date getDateObj(int year, int month, int day) {
    Calendar c = new GregorianCalendar();
    c.set(year, month - 1, day);/*from  ww  w .  java 2s  . com*/
    return c.getTime();
}

From source file:Main.java

/**
 * returns the string representation of the date X days ago
 * in the appropriate format for the URL
 * @param _numDaysAgo number of days before the current day
 * @return string representation of X days ago
 *//*from  w ww .  j  a  va  2 s .  c o  m*/
public static String getUrlTimeStringForXDaysAgo(int _numDaysAgo) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.add(Calendar.DAY_OF_MONTH, -1 * _numDaysAgo);
    return getUrlTimeString(cal);
}

From source file:Spring.Repaso02.Principal.java

public static GregorianCalendar inicializaCal() {
    GregorianCalendar cal = new GregorianCalendar();
    Date fecha = new Date();
    cal.setTime(fecha);//  w  w w.j  a  va  2s. co m
    return cal;
}

From source file:Main.java

/**
 * Format a date in xs:dateTime format.//from   w w w .  java2s. c o m
 * 
 * @param date
 *            The date.
 * @return The xs:dateTime
 * @throws DatatypeConfigurationException
 *             For errors.
 */
public static String formatXmlDateTimeWithZone(Date date) throws DatatypeConfigurationException {
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(date);
    DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
    XMLGregorianCalendar xmlGregorianCalendar = datatypeFactory.newXMLGregorianCalendar(gc);
    return xmlGregorianCalendar.toXMLFormat();
}

From source file:Main.java

public static XMLGregorianCalendar xmlGregorianCalendar(Date date) throws DatatypeConfigurationException {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(date);//from  w  ww .j  a  va 2s .c o  m
    return DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
}