Example usage for java.util GregorianCalendar setTimeZone

List of usage examples for java.util GregorianCalendar setTimeZone

Introduction

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

Prototype

@Override
    public void setTimeZone(TimeZone zone) 

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {

    GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();

    // set to another time zone
    cal.setTimeZone(TimeZone.getTimeZone("GMT"));
    System.out.println("New Time Zone:" + cal.getTimeZone().getDisplayName());

}

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;//from w w w  .j  a  v a 2s  . 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 long currentYearInMills() {
    GregorianCalendar gregoriancalendar = new GregorianCalendar((new GregorianCalendar()).get(1), 1, 1);
    gregoriancalendar.setTimeZone(GMT);
    return gregoriancalendar.getTimeInMillis();
}

From source file:net.darkmist.clf.LogEntryTest.java

private static final Date mkStaticDate() {
    GregorianCalendar cal = new GregorianCalendar(2007, 5, 5, 8, 9, 10);
    cal.setTimeZone(TimeZone.getTimeZone("GMT-1"));
    return cal.getTime();
}

From source file:Main.java

public static XMLGregorianCalendar getXMLDate(final Date dateAndTime) {
    GregorianCalendar c = new GregorianCalendar();
    c.setTimeZone(UTC);
    c.setTime(dateAndTime);/*from  w w  w . j  a  v a 2 s . c  om*/

    try {
        XMLGregorianCalendar ret = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
        ret.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
        return ret;
    } catch (DatatypeConfigurationException e) {
        return null;
    }
}

From source file:Main.java

public static long currentMonthInMills() {
    GregorianCalendar gregoriancalendar = new GregorianCalendar();
    GregorianCalendar gregoriancalendar1 = new GregorianCalendar(gregoriancalendar.get(1),
            gregoriancalendar.get(2), 1);
    gregoriancalendar1.setTimeZone(GMT);
    return gregoriancalendar1.getTimeInMillis();
}

From source file:Main.java

public static long currentWeekInMills() {
    GregorianCalendar gregoriancalendar = new GregorianCalendar();
    GregorianCalendar gregoriancalendar1 = new GregorianCalendar(gregoriancalendar.get(1),
            gregoriancalendar.get(2), gregoriancalendar.get(5));
    gregoriancalendar1.setTimeZone(GMT);
    gregoriancalendar1.add(6, -(gregoriancalendar.get(7) - gregoriancalendar.getFirstDayOfWeek()));
    return gregoriancalendar1.getTimeInMillis();
}

From source file:net.darkmist.clf.LogParserTest.java

private static Date mkLogTimeAsDate() {
    GregorianCalendar cal = new GregorianCalendar(2007, 5, 5, 8, 9, 10);
    cal.setTimeZone(TimeZone.getTimeZone("GMT-1"));
    return cal.getTime();
}

From source file:Main.java

private static GregorianCalendar gmtStringToGregorianCalendar(String stringTypeDate)
        throws DatatypeConfigurationException {
    String yyyy = stringTypeDate.substring(0, 4);
    String mm = stringTypeDate.substring(5, 7);
    String dd = stringTypeDate.substring(8, 10);

    String hh = "00";
    String mi = "00";
    String ss = "00";
    String ms = "00";
    String tz = null;/* w w w .j a va2s .  com*/

    if (stringTypeDate.length() > 23) {
        hh = stringTypeDate.substring(11, 13);
        mi = stringTypeDate.substring(14, 16);
        ss = stringTypeDate.substring(17, 19);
        ms = stringTypeDate.substring(20, 23);
        tz = stringTypeDate.substring(23);
    } else {
        tz = stringTypeDate.substring(10);
        //tz = "+09:00";
    }

    if (tz.equals("Z")) {
        tz = "UTC";
    } else {
        tz = "GMT" + tz;
    }

    int iyyyy = Integer.parseInt(yyyy);
    int imm = Integer.parseInt(mm) - 1;
    int idd = Integer.parseInt(dd);
    int ihh = Integer.parseInt(hh);
    int imi = Integer.parseInt(mi);
    int iss = Integer.parseInt(ss);
    int ims = Integer.parseInt(ms);

    Calendar c = Calendar.getInstance();
    c.setTimeZone(TimeZone.getTimeZone(tz));
    c.set(iyyyy, imm, idd, ihh, imi, iss);
    c.set(Calendar.MILLISECOND, ims);

    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(c.getTime());
    cal.setTimeZone(TimeZone.getTimeZone("ROK"));

    return cal;
}

From source file:Main.java

public static GregorianCalendar fromXSDateTime(String dateTime) throws DatatypeConfigurationException {
    GregorianCalendar cal = null;

    if (dateTime != null && dateTime.length() != 0) {
        cal = DatatypeFactory.newInstance().newXMLGregorianCalendar(dateTime).toGregorianCalendar();
        cal.setTimeZone(TimeZone.getTimeZone("UTC"));
    }// ww  w  . j a  va  2 s.co  m

    return cal;
}