Package java.util

Examples of java.util.SimpleTimeZone


{
  public void test (TestHarness harness)
  {
    harness.checkPoint("set/restore default TimeZone");
   
    SimpleTimeZone stz = new SimpleTimeZone(60 * 60 * 1000, "MyTZ");
   
    TimeZone old = TimeZone.getDefault();   
    TimeZone.setDefault(stz);
   
    harness.check(TimeZone.getDefault().getID(), stz.getID());

    TimeZone.setDefault(null);
    harness.check(TimeZone.getDefault().getID(), old.getID());
  }
View Full Code Here


     * package-private for testing
     */
    static String formatTZoffset(long millis, String sep)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("Z"); // #hhmm
        sdf.setTimeZone(new SimpleTimeZone(restrainTZoffset(millis),"unknown"));
        String tz = sdf.format(new Date());
        return tz.substring(0,3) + sep + tz.substring(3);
    }
View Full Code Here

     * package-private for testing
     */
    static GregorianCalendar newGreg()
    {
        GregorianCalendar retCal = new GregorianCalendar(Locale.ENGLISH);
        retCal.setTimeZone(new SimpleTimeZone(0, "UTC"));
        retCal.setLenient(false);
        retCal.set(Calendar.MILLISECOND, 0);
        return retCal;
    }
View Full Code Here

     */
    static boolean parseTZoffset(String text, GregorianCalendar cal,
                                        ParsePosition initialWhere)
    {
        ParsePosition where = new ParsePosition(initialWhere.getIndex());
        TimeZone tz = new SimpleTimeZone(0, "GMT");
        int tzHours, tzMin;
        char sign = skipOptionals(text, where, "Z+- ");
        boolean hadGMT = (sign == 'Z' || skipString(text, "GMT", where) ||
                         skipString(text, "UTC", where));
        sign = (!hadGMT) ? sign : skipOptionals(text, where, "+- ");
       
        tzHours = parseTimeField(text, where, 2, -999);
        skipOptionals(text, where, "\': ");
        tzMin = parseTimeField(text, where, 2, 0);
        skipOptionals(text, where, "\' ");
       
        if (tzHours != -999)
        {
            // we parsed a time zone in default format
            int hrSign = (sign == '-' ? -1 : 1);
            tz.setRawOffset(restrainTZoffset(hrSign * (tzHours * MILLIS_PER_HOUR + tzMin *
                                                       MILLIS_PER_MINUTE)));
            tz.setID("unknown");
        }
        else if ( ! hadGMT)
        {
            // try to process as a name; "GMT" or "UTC" has already been processed
            String tzText = text.substring(initialWhere.getIndex()).trim();
            tz = TimeZone.getTimeZone(tzText);
            // getTimeZone returns "GMT" for unknown ids
            if ("GMT".equals(tz.getID())) 
            {
                // no timezone in text, cal amd initialWhere are unchanged
                return false;
            }
            else
View Full Code Here

        checkParseTZ(0, "Z");
    }
   
    private static void checkFormatOffset(double off, String expect)
    {
        TimeZone tz = new SimpleTimeZone((int)(off*60*60*1000), "junkID");
        String got = DateConverter.formatTZoffset(tz.getRawOffset(), ":");
        assertEquals(expect, got);
    }
View Full Code Here

            int minute = 0;
            int second = 0;
            // first string off the prefix if it exists
            try
            {
                SimpleTimeZone zone = null;
                if (date.startsWith("D:"))
                {
                    date = date.substring(2, date.length());
                }

                date = date.replaceAll("[-:T]", "");

                if (date.length() < 4)
                {
                    throw new IOException("Error: Invalid date format '" + date + "'");
                }
                year = Integer.parseInt(date.substring(0, 4));
                if (date.length() >= 6)
                {
                    month = Integer.parseInt(date.substring(4, 6));
                }
                if (date.length() >= 8)
                {
                    day = Integer.parseInt(date.substring(6, 8));
                }
                if (date.length() >= 10)
                {
                    hour = Integer.parseInt(date.substring(8, 10));
                }
                if (date.length() >= 12)
                {
                    minute = Integer.parseInt(date.substring(10, 12));
                }

                int timeZonePos = 12;
                if (date.length() - 12 > 5 || (date.length() - 12 == 3 && date.endsWith("Z")))
                {
                    if (date.length() >= 14)
                    {
                        second = Integer.parseInt(date.substring(12, 14));
                    }
                    timeZonePos = 14;
                }
                else
                {
                    second = 0;
                }

                if (date.length() >= (timeZonePos + 1))
                {
                    char sign = date.charAt(timeZonePos);
                    if (sign == 'Z')
                    {
                        zone = new SimpleTimeZone(0, "Unknown");
                    }
                    else
                    {
                        int hours = 0;
                        int minutes = 0;
                        if (date.length() >= (timeZonePos + 3))
                        {
                            if (sign == '+')
                            {
                                // parseInt cannot handle the + sign
                                hours = Integer.parseInt(date.substring((timeZonePos + 1), (timeZonePos + 3)));
                            }
                            else
                            {
                                hours = -Integer.parseInt(date.substring(timeZonePos, (timeZonePos + 2)));
                            }
                        }
                        if (sign == '+')
                        {
                            if (date.length() >= (timeZonePos + 5))
                            {
                                minutes = Integer.parseInt(date.substring((timeZonePos + 3), (timeZonePos + 5)));
                            }
                        }
                        else
                        {
                            if (date.length() >= (timeZonePos + 4))
                            {
                                minutes = Integer.parseInt(date.substring((timeZonePos + 2), (timeZonePos + 4)));
                            }
                        }
                        zone = new SimpleTimeZone(hours * 60 * 60 * 1000 + minutes * 60 * 1000, "Unknown");
                    }
                }

                if (zone == null)
                {
View Full Code Here

  {
    int offset = -6 * 60 * 60 * 1000;
    int t1 = 5 * 60 * 60 * 1000;
    int t2 = 6 * 60 * 60 * 1000;
    int dst = 2 * 60 * 60 * 1000;
    SimpleTimeZone z = new SimpleTimeZone(offset, "Z1",
      Calendar.APRIL, 26, 0, t1,
      Calendar.OCTOBER, 25, 0, t2, dst);
   
    GregorianCalendar c = new GregorianCalendar(z);
    c.set(2004, Calendar.APRIL, 25, 8, 0, 0);
    harness.check(!z.inDaylightTime(c.getTime()));
    c.set(2004, Calendar.APRIL, 26, 8, 0, 0);
    harness.check(z.inDaylightTime(c.getTime()));
   
    c.set(2004, Calendar.OCTOBER, 24, 8, 0, 0);
    harness.check(z.inDaylightTime(c.getTime()));
    c.set(2004, Calendar.OCTOBER, 25, 8, 0, 0);
    harness.check(!z.inDaylightTime(c.getTime()));
   
    try
    {
      boolean b = z.inDaylightTime(null);
      harness.check(false);
    }
    catch (NullPointerException e)
    {
      harness.check(true);
View Full Code Here

{
  public void
  test(TestHarness harness)
  {
     // Set a common timezone to get the same result everywhere.
    SimpleTimeZone stz = new SimpleTimeZone(-5 * 1000 * 3600, "GMT");
    TimeZone.setDefault(stz);

    try {
      Timestamp.valueOf("NoSuchTime");
      harness.check(false, "valueOf");
View Full Code Here

  // Tests converted from the SimpleTimeZoneTest.java attachment
  // of http://gcc.gnu.org/ml/java-patches/2007-q1/msg00587.html.
  private void test2(TestHarness harness)
  {
    TimeZone utc = (TimeZone) new SimpleTimeZone(0, "GMT");
    TimeZone.setDefault(utc);
    Calendar cal = Calendar.getInstance(utc);

    harness.checkPoint("test 1");

    TimeZone tz1 = new SimpleTimeZone(
      -12600000, "Canada/Newfoundland",
      Calendar.MARCH, 8, -Calendar.SUNDAY, 60000,
      Calendar.NOVEMBER, 1, -Calendar.SUNDAY, 60000);

    cal.set(2037, Calendar.NOVEMBER, 1, 2, 30, 0);
    harness.check(tz1.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2037, Calendar.NOVEMBER, 1, 2, 31, 0);
    harness.check(!tz1.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2038, Calendar.JANUARY, 1, 2, 29, 0);
    harness.check(!tz1.inDaylightTime(new Date(cal.getTimeInMillis())));

    harness.checkPoint("test 2");

    TimeZone tz2 = new SimpleTimeZone(
      -12600000, "Test1",
      Calendar.MARCH, 8, -Calendar.SUNDAY, 60000,
      SimpleTimeZone.WALL_TIME,
      Calendar.NOVEMBER, 1, -Calendar.SUNDAY, 60000,
      SimpleTimeZone.STANDARD_TIME,
      3600000);

    // NB this particular check fails on several proprietary JVMs
    // because the end time is interpreted with startTimeMode.
    cal.set(2037, Calendar.NOVEMBER, 1, 3, 30, 0);
    harness.check(tz2.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2037, Calendar.NOVEMBER, 1, 3, 31, 0);
    harness.check(!tz2.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2038, Calendar.JANUARY, 1, 3, 29, 0);
    harness.check(!tz2.inDaylightTime(new Date(cal.getTimeInMillis())));

    harness.checkPoint("test 3");

    TimeZone tz3 = new SimpleTimeZone(
      -12600000, "Test2",
      Calendar.MARCH, 8, -Calendar.SUNDAY, 60000,
      Calendar.NOVEMBER, 1, -Calendar.SUNDAY, 3660000,
      3600000);

    cal.set(2037, Calendar.NOVEMBER, 1, 3, 30, 0);
    harness.check(tz3.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2037, Calendar.NOVEMBER, 1, 3, 31, 0);
    harness.check(!tz3.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2038, Calendar.JANUARY, 1, 3, 29, 0);
    harness.check(!tz3.inDaylightTime(new Date(cal.getTimeInMillis())));

    harness.checkPoint("test 4");

    ((SimpleTimeZone) tz2).setEndRule(
      Calendar.NOVEMBER, 1, -Calendar.SUNDAY, 3660000);

    cal.set(2037, Calendar.NOVEMBER, 1, 3, 30, 0);
    harness.check(tz2.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2037, Calendar.NOVEMBER, 1, 3, 31, 0);
    harness.check(!tz2.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2038, Calendar.JANUARY, 1, 3, 29, 0);
    harness.check(!tz2.inDaylightTime(new Date(cal.getTimeInMillis())));

    harness.checkPoint("test 5");

    TimeZone tz4 = new SimpleTimeZone(
      -12600000, "Test1",
      Calendar.MARCH, 8, -Calendar.SUNDAY, 60000,
      SimpleTimeZone.STANDARD_TIME,
      Calendar.NOVEMBER, 1, -Calendar.SUNDAY, 60000,
      SimpleTimeZone.STANDARD_TIME,
      3600000);

    cal.set(2037, Calendar.NOVEMBER, 1, 3, 30, 0);
    harness.check(tz4.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2037, Calendar.NOVEMBER, 1, 3, 31, 0);
    harness.check(!tz4.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2038, Calendar.JANUARY, 1, 3, 29, 0);
    harness.check(!tz4.inDaylightTime(new Date(cal.getTimeInMillis())));

    harness.checkPoint("test 6");

    TimeZone tz5 = new SimpleTimeZone(
      -12600000, "Test3",
      Calendar.MARCH, 8, -Calendar.SUNDAY, 60000,
      Calendar.JANUARY, 1, 0, 60000,
      3600000);

    cal.set(2007, Calendar.DECEMBER, 31, 23, 59, 0);
    harness.check(tz5.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2007, Calendar.JANUARY, 1, 2, 29, 0);
    harness.check(tz5.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2007, Calendar.JANUARY, 1, 2, 30, 0);
    harness.check(tz5.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2007, Calendar.JANUARY, 1, 2, 31, 0);
    harness.check(!tz5.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2007, Calendar.JANUARY, 3, 2, 31, 0);
    harness.check(!tz5.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2007, Calendar.JANUARY, 3, 2, 31, 0);
    harness.check(!tz5.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2007, Calendar.MARCH, 11, 3, 30, 0);
    harness.check(!tz5.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2007, Calendar.MARCH, 11, 3, 31, 0);
    harness.check(tz5.inDaylightTime(new Date(cal.getTimeInMillis())));

    harness.checkPoint("test 7");

    TimeZone tz6 = new SimpleTimeZone(
      12600000, "Test4",
      Calendar.MARCH, 6, 0, 18000000 - 12600000,
      SimpleTimeZone.UTC_TIME,
      Calendar.NOVEMBER, -16, -Calendar.THURSDAY, 18000000 - 3600000-12600000,
      SimpleTimeZone.UTC_TIME,
      3600000);

    cal.set(2007, Calendar.MARCH, 6, 1, 29, 0);
    harness.check(!tz6.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2007, Calendar.MARCH, 6, 1, 30, 0);
    harness.check(tz6.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2007, Calendar.NOVEMBER, 15, 0, 29, 0);
    harness.check(tz6.inDaylightTime(new Date(cal.getTimeInMillis())));

    cal.set(2007, Calendar.NOVEMBER, 15, 0, 30, 0);
    harness.check(!tz6.inDaylightTime(new Date(cal.getTimeInMillis())));
  }
View Full Code Here

   *
   * @param harness  the test harness (<code>null</code> not permitted).
   */
  public void test(TestHarness harness)
  {
    SimpleTimeZone z1 = new SimpleTimeZone(5 * 60 * 60 * 1000, "Zone 1",
      Calendar.APRIL, 26, 0, 2*60*60*1000,
      Calendar.OCTOBER, 25, 0, 2*60*60*1000, 36000000);
    z1.setDSTSavings(12345);
    harness.check(z1.getDSTSavings(), 12345);
   
    // here is a special case (bug?), the zone has no daylight saving rule, so
    // setting the DSTSavings amount doesn't have any effect
    SimpleTimeZone z2 = new SimpleTimeZone(5*60*60*1000, "Zone 2");
    z2.setDSTSavings(12345);
    harness.check(z2.getDSTSavings(), 0)// zero!
  }
View Full Code Here

TOP

Related Classes of java.util.SimpleTimeZone

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.