Example usage for java.util GregorianCalendar clear

List of usage examples for java.util GregorianCalendar clear

Introduction

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

Prototype

public final void clear() 

Source Link

Document

Sets all the calendar field values and the time value (millisecond offset from the Epoch) of this Calendar undefined.

Usage

From source file:com.zimbra.cs.mailbox.calendar.ZRecur.java

public static void main(String[] args) {
    ICalTimeZone tzUTC = ICalTimeZone.getUTC();
    TimeZoneMap tzmap = new TimeZoneMap(tzUTC);
    ParsedDateTime dtStart = null;// w  ww  .j av a  2  s . c  o  m
    try {
        dtStart = ParsedDateTime.parse("20050101T123456", tzmap, tzUTC, tzUTC);
    } catch (ParseException e) {
        System.out.println("Caught ParseException at start: " + e);
    }

    Date rangeStart;
    Date rangeEnd;

    GregorianCalendar cal = new GregorianCalendar();
    cal.clear();
    cal.setTimeZone(tzUTC);

    cal.set(2005, 4, 15, 0, 0, 0);
    rangeStart = cal.getTime();

    cal.set(2006, 0, 1, 0, 0, 0);
    rangeEnd = cal.getTime();

    try {
        ZRecur test = new ZRecur("FREQ=DAILY;BYMONTH=5,6", tzmap);
        System.out.println("\n\n" + test.toString() + "\n-------------------------------------------------");
        List<Date> dateList = test.expandRecurrenceOverRange(dtStart, rangeStart.getTime(), rangeEnd.getTime());
        for (Date d : dateList) {
            cal.setTime(d);
            cal.setTimeZone(tzUTC);
            System.out.printf("%tc\n", cal);
        }
    } catch (ServiceException e) {
        System.out.println("Caught ServiceException" + e);
        e.printStackTrace();
    }

    try {
        ZRecur test = new ZRecur("FREQ=DAILY;BYMONTH=5,6;BYDAY=TH,-1MO", tzmap);
        System.out.println("\n\n" + test.toString() + "\n-------------------------------------------------");
        List<Date> dateList = test.expandRecurrenceOverRange(dtStart, rangeStart.getTime(), rangeEnd.getTime());
        for (Date d : dateList) {
            cal.setTime(d);
            cal.setTimeZone(tzUTC);
            System.out.printf("%tc\n", cal);
        }
    } catch (ServiceException e) {
        System.out.println("Caught ServiceException" + e);
        e.printStackTrace();
    }

    try {
        ZRecur test = new ZRecur("FREQ=DAILY;BYMONTH=5,6;BYMONTHDAY=1,3,5,7,9,31", tzmap);
        System.out.println("\n\n" + test.toString() + "\n-------------------------------------------------");
        List<Date> dateList = test.expandRecurrenceOverRange(dtStart, rangeStart.getTime(), rangeEnd.getTime());
        for (Date d : dateList) {
            cal.setTime(d);
            System.out.printf("%tc\n", cal);
        }
    } catch (ServiceException e) {
        System.out.println("Caught ServiceException" + e);
        e.printStackTrace();
    }

    try {
        ZRecur test = new ZRecur("FREQ=DAILY;BYMONTH=5,6;BYMONTHDAY=1,3,5,7,9,31;BYDAY=SU,SA", tzmap);
        System.out.println(
                "\n\n" + test.toString() + "\n--------------------------------------------------------------");
        List<Date> dateList = test.expandRecurrenceOverRange(dtStart, rangeStart.getTime(), rangeEnd.getTime());
        for (Date d : dateList) {
            cal.setTime(d);
            System.out.printf("%tc\n", cal);
        }
    } catch (ServiceException e) {
        System.out.println("Caught ServiceException" + e);
        e.printStackTrace();
    }

    try {
        ZRecur test = new ZRecur("FREQ=DAILY;BYMONTH=5,6;BYMONTHDAY=1,3,5,7,9,31;BYDAY=SU,SA;BYHOUR=21,0",
                tzmap);
        System.out.println(
                "\n\n" + test.toString() + "\n--------------------------------------------------------------");
        List<Date> dateList = test.expandRecurrenceOverRange(dtStart, rangeStart.getTime(), rangeEnd.getTime());
        for (Date d : dateList) {
            cal.setTime(d);
            System.out.printf("%tc\n", cal);
        }
    } catch (ServiceException e) {
        System.out.println("Caught ServiceException" + e);
        e.printStackTrace();
    }

    try {
        ZRecur test = new ZRecur(
                "FREQ=DAILY;BYMONTH=5,6;BYMONTHDAY=1,3,5,7,9,31;BYDAY=SU;BYHOUR=21,0;BYMINUTE=23", tzmap);
        System.out.println(
                "\n\n" + test.toString() + "\n--------------------------------------------------------------");
        List<Date> dateList = test.expandRecurrenceOverRange(dtStart, rangeStart.getTime(), rangeEnd.getTime());
        for (Date d : dateList) {
            cal.setTime(d);
            System.out.printf("%tc\n", cal);
        }
    } catch (ServiceException e) {
        System.out.println("Caught ServiceException" + e);
        e.printStackTrace();
    }

    try {
        ZRecur test = new ZRecur(
                "FREQ=DAILY;BYMONTH=5,6;BYMONTHDAY=1,3,5,7,9,31;BYDAY=SU;BYHOUR=1,21,0;BYSECOND=0,59", tzmap);
        System.out.println(
                "\n\n" + test.toString() + "\n--------------------------------------------------------------");
        List<Date> dateList = test.expandRecurrenceOverRange(dtStart, rangeStart.getTime(), rangeEnd.getTime());
        for (Date d : dateList) {
            cal.setTime(d);
            System.out.printf("%tc\n", cal);
        }
    } catch (ServiceException e) {
        System.out.println("Caught ServiceException" + e);
        e.printStackTrace();
    }

    try {
        // parse error testing
        ZRecur test = new ZRecur(
                "FREQ=DAILY;BIYMONTH=5,6;BYMONTHDAY=1,3,5,7,9,31;BYDAY=SU;BYHOUR=1,21,0;BYSECOND=0,59;BYSETPOS=1,-1,3,1000,,-1000",
                tzmap);
        System.out.println(
                "\n\n" + test.toString() + "\n--------------------------------------------------------------");
        List<Date> dateList = test.expandRecurrenceOverRange(dtStart, rangeStart.getTime(), rangeEnd.getTime());
        for (Date d : dateList) {
            cal.setTime(d);
            System.out.printf("%tc\n", cal);
        }
    } catch (ServiceException e) {
        System.out.println("Caught ServiceException" + e);
        e.printStackTrace();
    }

    try {
        ZRecur test = new ZRecur("FREQ=HOURLY;BIYMONTH=6;BYMONTHDAY=1,3;BYHOUR=2,14", tzmap);
        System.out.println(
                "\n\n" + test.toString() + "\n--------------------------------------------------------------");
        List<Date> dateList = test.expandRecurrenceOverRange(dtStart, rangeStart.getTime(), rangeEnd.getTime());
        for (Date d : dateList) {
            cal.setTime(d);
            System.out.printf("%tc\n", cal);
        }
    } catch (ServiceException e) {
        System.out.println("Caught ServiceException" + e);
        e.printStackTrace();
    }

    try {
        ZRecur test = new ZRecur("FREQ=HOURLY;BIYMONTH=6;BYMONTHDAY=1;;BYMINUTE=10;BYSECOND=11,12", tzmap);
        System.out.println(
                "\n\n" + test.toString() + "\n--------------------------------------------------------------");
        List<Date> dateList = test.expandRecurrenceOverRange(dtStart, rangeStart.getTime(), rangeEnd.getTime());
        for (Date d : dateList) {
            cal.setTime(d);
            System.out.printf("%tc\n", cal);
        }
    } catch (ServiceException e) {
        System.out.println("Caught ServiceException" + e);
        e.printStackTrace();
    }

    cal.set(2010, 0, 1, 0, 0, 0);
    rangeEnd = cal.getTime();

    try {
        ZRecur test = new ZRecur("FREQ=YEARLY", tzmap);
        System.out.println(
                "\n\n" + test.toString() + "\n--------------------------------------------------------------");
        List<Date> dateList = test.expandRecurrenceOverRange(dtStart, rangeStart.getTime(), rangeEnd.getTime());
        for (Date d : dateList) {
            cal.setTime(d);
            System.out.printf("%tc\n", cal);
        }
    } catch (ServiceException e) {
        System.out.println("Caught ServiceException" + e);
        e.printStackTrace();
    }

    try {
        ZRecur test = new ZRecur("FREQ=YEARLY;BYYEARDAY=-1", tzmap);
        System.out.println(
                "\n\n" + test.toString() + "\n--------------------------------------------------------------");
        List<Date> dateList = test.expandRecurrenceOverRange(dtStart, rangeStart.getTime(), rangeEnd.getTime());
        for (Date d : dateList) {
            cal.setTime(d);
            System.out.printf("%tc\n", cal);
        }
    } catch (ServiceException e) {
        System.out.println("Caught ServiceException" + e);
        e.printStackTrace();
    }

    try {
        ZRecur test = new ZRecur("FREQ=SECONDLY", tzmap);
        System.out.println(
                "\n\n" + test.toString() + "\n--------------------------------------------------------------");
        List<Date> dateList = test.expandRecurrenceOverRange(dtStart, rangeStart.getTime(), rangeEnd.getTime());
        for (Date d : dateList) {
            cal.setTime(d);
            System.out.printf("%tc\n", cal);
        }
    } catch (ServiceException e) {
        System.out.println("Caught ServiceException" + e);
        e.printStackTrace();
    }

    try {
        ParsedDateTime myDtStart = ParsedDateTime.parse("16010101T020000", tzmap, tzUTC, tzUTC);
        ZRecur test = new ZRecur("FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=12;BYDAY=-1SU", tzmap);
        System.out.println(
                "\n\n" + test.toString() + "\n--------------------------------------------------------------");
        List<Date> dateList = test.expandRecurrenceOverRange(myDtStart, rangeStart.getTime(),
                rangeEnd.getTime());
        for (Date d : dateList) {
            cal.setTime(d);
            System.out.printf("%tc\n", cal);
        }
    } catch (ParseException e) {
        System.out.println("Caught ParseException" + e);
        e.printStackTrace();
    } catch (ServiceException e) {
        System.out.println("Caught ServiceException" + e);
        e.printStackTrace();
    }

    cal.set(2010, 0, 1, 0, 0, 0);
    rangeEnd = cal.getTime();

    try {
        ZRecur test = new ZRecur("FREQ=YEARLY;BYMONTH=12;BYDAY=1WE", tzmap);
        System.out.println(
                "\n\n" + test.toString() + "\n--------------------------------------------------------------");
        List<Date> dateList = test.expandRecurrenceOverRange(dtStart, rangeStart.getTime(), rangeEnd.getTime());
        for (Date d : dateList) {
            cal.setTime(d);
            System.out.printf("%tc\n", cal);
        }
    } catch (ServiceException e) {
        System.out.println("Caught ServiceException" + e);
        e.printStackTrace();
    }

}

From source file:Main.java

public static String weekDay(String year, String month, String day) {
    String strWeekday = "";
    try {/*from w  w  w  .j  av  a2 s . c  o m*/
        GregorianCalendar cal = new GregorianCalendar();
        cal.setLenient(false);
        cal.clear();
        cal.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));
        strWeekday = String.valueOf(cal.get(Calendar.DAY_OF_WEEK) - 1);
    } catch (IllegalArgumentException e) {
        strWeekday = "";
    }
    return strWeekday;
}

From source file:Main.java

public static boolean isPastDate(String year, String month, String day) {
    try {/*from w  ww. j a  v a2 s .c  om*/
        GregorianCalendar cal = new GregorianCalendar();
        cal.setLenient(false);
        SimpleDateFormat fmDate = new SimpleDateFormat("yyyyMMdd");
        String today = fmDate.format(cal.getTime());
        cal.clear();
        cal.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));
        String inputday = fmDate.format(cal.getTime());

        if (inputday.compareTo(today) < 0) {
            return true;
        } else {
            return false;
        }
    } catch (IllegalArgumentException e) {
        return false;
    }
}

From source file:org.openanzo.rdf.datatype.TypeMaps.java

/**
 * Create an XML Calendar for the time in millis
 * /*from  ww  w  . j a va2  s.  c om*/
 * @param millis
 * @return XML Calendar
 */
public static XMLGregorianCalendar getXMLCaledar(long millis) {
    GregorianCalendar cal = new GregorianCalendar(DateUtils.UTC_TIME_ZONE);
    cal.clear();
    cal.setGregorianChange(MIN_DATE);
    cal.setTimeInMillis(millis);
    return TypeMaps.datatypeFactory.newXMLGregorianCalendar(cal);
}

From source file:org.openanzo.rdf.datatype.TypeMaps.java

/**
 * Create an XML Calendar for the time in millis
 * //  www  .  ja v  a 2 s. c o m
 * @param millis
 * @return XML Calendar
 */
public static XMLGregorianCalendar getXMLCaledar(Date date) {
    GregorianCalendar cal = new GregorianCalendar(DateUtils.UTC_TIME_ZONE);
    cal.clear();
    cal.setGregorianChange(MIN_DATE);
    cal.setTime(date);
    return TypeMaps.datatypeFactory.newXMLGregorianCalendar(cal);
}

From source file:org.mariotaku.twidere.api.twitter.util.TwitterDateConverter.java

private Date parseTwitterDate(String string) {
    final String[] segs = StringUtils.split(string, ' ');
    if (segs.length != 6) {
        return null;
    }/* w  ww  .j  a va 2 s.  c  o m*/
    final String[] timeSegs = StringUtils.split(segs[3], ':');
    if (timeSegs.length != 3) {
        return null;
    }

    final GregorianCalendar calendar = new GregorianCalendar(TIME_ZONE, LOCALE);
    calendar.clear();
    final int monthIdx = ArrayUtils.indexOf(MONTH_NAMES, segs[1]);
    if (monthIdx < 0) {
        return null;
    }
    calendar.set(Calendar.YEAR, Integer.parseInt(segs[5]));
    calendar.set(Calendar.MONTH, monthIdx);
    calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(segs[2]));
    calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeSegs[0]));
    calendar.set(Calendar.MINUTE, Integer.parseInt(timeSegs[1]));
    calendar.set(Calendar.SECOND, Integer.parseInt(timeSegs[2]));
    calendar.set(Calendar.ZONE_OFFSET, SimpleTimeZone.getTimeZone("GMT" + segs[4]).getRawOffset());
    final Date date = calendar.getTime();
    if (!WEEK_NAMES[calendar.get(Calendar.DAY_OF_WEEK) - 1].equals(segs[0])) {
        AbsLogger.error("Week mismatch " + string + " => " + date);
        return null;
    }
    return date;
}

From source file:org.kuali.kfs.coa.batch.dataaccess.impl.AccountingPeriodFiscalYearMakerImpl.java

/**
 * Adds one year to the given date/*w w w. j  a  v  a  2s.c  om*/
 * 
 * @param inDate date to increment
 * @return Date incoming date plus one year
 */
protected java.sql.Date addYearToDate(Date inDate) {
    GregorianCalendar currentCalendarDate = new GregorianCalendar();
    currentCalendarDate.clear();

    currentCalendarDate.setTimeInMillis(inDate.getTime());
    currentCalendarDate.add(GregorianCalendar.YEAR, 1);

    return new Date(currentCalendarDate.getTimeInMillis());
}

From source file:com.bt.heliniumstudentapp.ScheduleFragment.java

private static GregorianCalendar getDate() {
    GregorianCalendar date = new GregorianCalendar(HeliniumStudentApp.LOCALE);
    date.clear();
    date.set(Calendar.YEAR, new GregorianCalendar(HeliniumStudentApp.LOCALE).get(Calendar.YEAR)); //FIXME BAD
    date.set(Calendar.WEEK_OF_YEAR, scheduleFocus);
    date.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

    return date;//w ww.j a  v a 2  s .  c  o  m
}

From source file:ca.uhn.hl7v2.model.primitive.tests.CommonDTTest.java

/**
 * Testing toHl7DTFormat() with a calendar
 *//*from  w w w.  ja v a  2  s .  c o  m*/
@Test
public void testConvertCalToHL7DateFormat() throws DataTypeException {
    GregorianCalendar cal = new GregorianCalendar();
    cal.clear();
    cal.setLenient(false);
    cal.set(2002, 5, 24);
    String convertedDate = CommonDT.toHl7DTFormat(cal);
    assertEquals("20020624", convertedDate);
}

From source file:ca.uhn.hl7v2.model.primitive.CommonTSTest.java

@Test
public void testToHl7TMFormat() throws DataTypeException {
    GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("America/Toronto"));
    cal.clear();
    cal.setLenient(false);/*from   ww w.  j av  a2s. c o  m*/
    cal.set(Calendar.HOUR_OF_DAY, 20);
    cal.set(Calendar.MINUTE, 6);
    cal.set(Calendar.SECOND, 24);
    cal.set(Calendar.MILLISECOND, 528);
    String convertedDate = CommonTM.toHl7TMFormat(cal);
    assertEquals("Should get a HL7 formatted date back", "200624.528-0500", convertedDate);
}