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

public static XMLGregorianCalendar asXMLGregorianCalendar(java.util.Date date) {
    if (date == null) {
        return null;
    } else {//from   w ww.  j ava2s. c o  m
        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

/**
 * Returns the year (index 0), month (index 1) and day (index 2) of the given timestamp.
 * @param timestamp the timestamp//www .jav  a2  s  .  c  o  m
 * @return the year (index 0), month (index 1) and day (index 2) of the given timestamp
 */
public static int[] getDayMonthYear(long timestamp) {
    GregorianCalendar gregorianCalendar = new GregorianCalendar();
    gregorianCalendar.setTimeInMillis(timestamp);

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

    return new int[] { year, month, day };
}

From source file:Main.java

public static String getStringByOffset(String strDate, String format, int calendarField, int offset) {
    String mDateTime = null;// w w  w.j  ava  2s  .c  o  m
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        c.setTime(mSimpleDateFormat.parse(strDate));
        c.add(calendarField, offset);
        mDateTime = mSimpleDateFormat.format(c.getTime());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return mDateTime;
}

From source file:Main.java

public static XMLGregorianCalendar longToXMLGregorianCalendar(long time) throws DatatypeConfigurationException {
    if (time <= 0) {
        return null;
    }//w  w w.j  a v a2 s .  c om

    GregorianCalendar cal = new GregorianCalendar();
    cal.setTimeInMillis(time);
    XMLGregorianCalendar newXMLGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
    return newXMLGregorianCalendar;
}

From source file:Main.java

/**
 * Convenience method to format a Date as an XML DateTime String.
 *
 * @param date/*from  w ww  . j  a  v a2  s .c  o  m*/
 *            the date to format.
 * @return the XML representation as a string.
 */
public static String formatDate(final Date date) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTimeInMillis(date.getTime());
    return formatGregorianCalendar(calendar);
}

From source file:Main.java

private static String getDayOfWeek(String format, int calendarField) {
    String strDate = null;/*from   ww w. jav a  2s .c  o m*/
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        int week = c.get(Calendar.DAY_OF_WEEK);
        if (week == calendarField) {
            strDate = mSimpleDateFormat.format(c.getTime());
        } else {
            int offectDay = calendarField - week;
            if (calendarField == Calendar.SUNDAY) {
                offectDay = 7 - Math.abs(offectDay);
            }
            c.add(Calendar.DATE, offectDay);
            strDate = mSimpleDateFormat.format(c.getTime());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return strDate;
}

From source file:Main.java

/**
 * Method to check if a date is tomorrow
 * //from   w ww  . j ava 2s .  c  o  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

/**
 * returns the string representation of the current date in the appropriate format for the URL
 * @return string representation of the current date
 *//* ww w. j a  v  a 2  s  .  c o m*/
public static String getTodayUrlTimeString() {
    return getUrlTimeString(new GregorianCalendar());
}

From source file:Main.java

public static String date2String(Date date, boolean printTime) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);/*from   w ww.  j a  v  a2s  .c o  m*/
    if (cal.get(Calendar.YEAR) > 10000) {
        return "---";
    }
    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 int monthsBetweenDates(String start, String end) throws ParseException {
    SimpleDateFormat format = new SimpleDateFormat("MMMM, yyyy", Locale.getDefault());
    Date startDate = format.parse(start);
    Date endDate = format.parse(end);

    Calendar startCalendar = new GregorianCalendar();
    startCalendar.setTime(startDate);//from   w  w  w.  ja v a  2s  .c  o  m
    Calendar endCalendar = new GregorianCalendar();
    endCalendar.setTime(endDate);

    int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
    return diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
}