Java Code Examples for android.icu.util.TimeZone#getDefault()

The following examples show how to use android.icu.util.TimeZone#getDefault() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimeZoneNamesImpl.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize the transient fields, called from the constructor and
 * readObject.
 *
 * @param locale The locale
 */
private void initialize(ULocale locale) {
    ICUResourceBundle bundle = (ICUResourceBundle)ICUResourceBundle.getBundleInstance(
            ICUData.ICU_ZONE_BASE_NAME, locale);
    _zoneStrings = (ICUResourceBundle)bundle.get(ZONE_STRINGS_BUNDLE);

    // TODO: Access is synchronized, can we use a non-concurrent map?
    _tzNamesMap = new ConcurrentHashMap<String, ZNames>();
    _mzNamesMap = new ConcurrentHashMap<String, ZNames>();
    _namesFullyLoaded = false;

    _namesTrie = new TextTrieMap<NameInfo>(true);
    _namesTrieFullyLoaded = false;

    // Preload zone strings for the default time zone
    TimeZone tz = TimeZone.getDefault();
    String tzCanonicalID = ZoneMeta.getCanonicalCLDRID(tz);
    if (tzCanonicalID != null) {
        loadStrings(tzCanonicalID);
    }
}
 
Example 2
Source File: TimeZoneGenericNames.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Private method initializing the instance of <code>TimeZoneGenericName</code>.
 * This method should be called from a constructor and readObject.
 */
private void init() {
    if (_tznames == null) {
        _tznames = TimeZoneNames.getInstance(_locale);
    }
    _genericLocationNamesMap = new ConcurrentHashMap<String, String>();
    _genericPartialLocationNamesMap = new ConcurrentHashMap<String, String>();

    _gnamesTrie = new TextTrieMap<NameInfo>(true);
    _gnamesTrieFullyLoaded = false;

    // Preload zone strings for the default time zone
    TimeZone tz = TimeZone.getDefault();
    String tzCanonicalID = ZoneMeta.getCanonicalCLDRID(tz);
    if (tzCanonicalID != null) {
        loadStrings(tzCanonicalID);
    }
}
 
Example 3
Source File: CompatibilityTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestEpochStartFields() {
    TimeZone z = TimeZone.getDefault();
    Calendar c = Calendar.getInstance();
    Date d = new Date(-z.getRawOffset());
    if (z.inDaylightTime(d)) {
        logln("Warning: Skipping test because " + d +
              " is in DST.");
    }
    else {
        c.setTime(d);
        c.setMinimalDaysInFirstWeek(1);
        for (int i=0; i<Calendar.ZONE_OFFSET; ++i) {
            if (c.get(i) != EPOCH_FIELDS[i])
                errln("Expected field " + i + " to have value " + EPOCH_FIELDS[i] +
                      "; saw " + c.get(i) + " instead");
        }
        if (c.get(Calendar.ZONE_OFFSET) != z.getRawOffset())
            errln("Expected field ZONE_OFFSET to have value " + z.getRawOffset() +
                  "; saw " + c.get(Calendar.ZONE_OFFSET) + " instead");
        if (c.get(Calendar.DST_OFFSET) != 0)
            errln("Expected field DST_OFFSET to have value 0" +
                  "; saw " + c.get(Calendar.DST_OFFSET) + " instead");
    }
}
 
Example 4
Source File: DangiTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestCoverage() {
    // DangiCalendar()
    // DangiCalendar(Date)
    // DangiCalendar(TimeZone, ULocale)
    Date d = new Date();

    DangiCalendar cal1 = new DangiCalendar();
    cal1.setTime(d);

    DangiCalendar cal2 = new DangiCalendar(d);

    DangiCalendar cal3 = new DangiCalendar(TimeZone.getDefault(), ULocale.getDefault());
    cal3.setTime(d);

    assertEquals("DangiCalendar() and DangiCalendar(Date)", cal1, cal2);
    assertEquals("DangiCalendar() and DangiCalendar(TimeZone,ULocale)", cal1, cal3);

    // String getType()
    String type = cal1.getType();
    assertEquals("getType()", "dangi", type);
}
 
Example 5
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * @bug 4089106
 */
@Test
public void Test4089106() {
    TimeZone def = TimeZone.getDefault();
    try {
        TimeZone z = new SimpleTimeZone((int) (1.25 * 3600000), "FAKEZONE");
        TimeZone.setDefault(z);
        // Android patch (http://b/28949992) start.
        // ICU TimeZone.setDefault() not supported on Android.
        z = TimeZone.getDefault();
        // Android patch (http://b/28949992) end.
        SimpleDateFormat f = new SimpleDateFormat();
        if (!f.getTimeZone().equals(z))
            errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
    } finally {
        TimeZone.setDefault(def);
    }
}
 
Example 6
Source File: DateFormatRegressionTestJ.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void run() {
    SimpleDateFormat sdf = (SimpleDateFormat) sdf_.clone();
    TimeZone defaultTZ = TimeZone.getDefault();
    TimeZone PST = TimeZone.getTimeZone("PST");
    int defaultOffset = defaultTZ.getRawOffset();
    int PSTOffset = PST.getRawOffset();
    int offset = defaultOffset - PSTOffset;
    long ms = UTC_LONG - offset;
    try {
        int i = 0;
        while (i < 10000) {
            Date date = sdf.parse(TIME_STRING);
            long t = date.getTime();
            i++;
            if (t != ms) {
                throw new ParseException("Parse Error: " + i + " (" + sdf.format(date) 
                          + ") " + t + " != " + ms, 0);
            }
        }
    } catch (Exception e) {
        errln("parse error: " + e.getMessage());
    }
}
 
Example 7
Source File: DateIntervalFormat.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Get the TimeZone
 * @return A copy of the TimeZone associated with this date interval formatter.
 */
public TimeZone getTimeZone()
{
    if ( fDateFormat != null ) {
        // Here we clone, like other getters here, but unlike
        // DateFormat.getTimeZone() and Calendar.getTimeZone()
        // which return the TimeZone from the Calendar's zone variable
        return (TimeZone)(fDateFormat.getTimeZone().clone());
    }
    // If fDateFormat is null (unexpected), return default timezone.
    return TimeZone.getDefault();
}
 
Example 8
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * @bug 4103341
 */
@Test
public void Test4103341() {
    TimeZone saveZone = TimeZone.getDefault();
    try {
        // {sfb} changed from adoptDefault to setDefault
        TimeZone.setDefault(TimeZone.getTimeZone("CST"));
        SimpleDateFormat simple = new SimpleDateFormat("MM/dd/yyyy HH:mm");
        TimeZone temp = TimeZone.getDefault();
        if (!simple.getTimeZone().equals(temp))
            errln("Fail: SimpleDateFormat not using default zone");
    } finally {
        TimeZone.setDefault(saveZone);
    }
}
 
Example 9
Source File: DateFormatRoundTripTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestDateFormatRoundTrip() {
    dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss.SSS zzz yyyy G");
    getFieldCal = Calendar.getInstance();
    ran = createRandom(); // use test framework's random seed

    final Locale[] avail = DateFormat.getAvailableLocales();
    int locCount = avail.length;
    logln("DateFormat available locales: " + locCount);
    if (quick) {
        if (locCount > 5)
            locCount = 5;
        logln("Quick mode: only testing first 5 Locales");
    }
    TimeZone tz = TimeZone.getDefault();
    logln("Default TimeZone:             " + tz.getID());

    if (INFINITE) {
        // Special infinite loop test mode for finding hard to reproduce errors
        Locale loc = Locale.getDefault();
        logln("ENTERING INFINITE TEST LOOP FOR Locale: " + loc.getDisplayName());
        for (;;) {
            _test(loc);
        }
    } else {
        _test(Locale.getDefault());
        for (int i = 0; i < locCount; ++i) {
            _test(avail[i]);
        }
    }
}
 
Example 10
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * CANNOT REPRODUDE
 *
 * Yet another _alleged_ bug in TimeZone.getOffset(), a method that never
 * should have been made public.  It's simply too hard to use correctly.
 *
 * The original test code failed to do the following:
 * (1) Call Calendar.setTime() before getting the fields!
 * (2) Use the right millis (as usual) for getOffset(); they were passing
 *     in the MILLIS field, instead of the STANDARD MILLIS IN DAY.
 * When you fix these two problems, the test passes, as expected.
 */
@Test
public void Test4126678() {
// Note: this test depends on the PST time zone.
TimeZone initialZone = TimeZone.getDefault();
    Calendar cal = Calendar.getInstance();
    TimeZone tz = TimeZone.getTimeZone("PST");
TimeZone.setDefault(tz);
    cal.setTimeZone(tz);

    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    tempcal.set(1998, Calendar.APRIL, 5, 10, 0);
    Date dt = tempcal.getTime();
// the dt value is local time in PST.
    if (!tz.inDaylightTime(dt))
        errln("We're not in Daylight Savings Time and we should be.\n");

    cal.setTime(dt);
    int era = cal.get(Calendar.ERA);
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DATE);
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    int millis = cal.get(Calendar.MILLISECOND) +
        (cal.get(Calendar.SECOND) +
         (cal.get(Calendar.MINUTE) +
          (cal.get(Calendar.HOUR) * 60) * 60) * 1000) -
        cal.get(Calendar.DST_OFFSET);

    long offset = tz.getOffset(era, year, month, day, dayOfWeek, millis);
    long raw_offset = tz.getRawOffset();
    if (offset == raw_offset)
        errln("Offsets should not match when in DST");

// restore the initial time zone so that this test case
// doesn't affect the others.
TimeZone.setDefault(initialZone);
}
 
Example 11
Source File: TimeZoneTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestFreezable() {
    // Test zones - initially thawed
    TimeZone[] ZA1 = {
        TimeZone.getDefault(),
        TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_ICU),
        TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_JDK),
        new SimpleTimeZone(0, "stz"),
        new RuleBasedTimeZone("rbtz", new InitialTimeZoneRule("rbtz0", 0, 0)),
        VTimeZone.create("America/New_York"),
    };

    checkThawed(ZA1, "ZA1");
    // freeze
    for (int i = 0; i < ZA1.length; i++) {
        ZA1[i].freeze();
    }
    checkFrozen(ZA1, "ZA1(frozen)");

    // Test zones - initially frozen
    final TimeZone[] ZA2 = {
        TimeZone.GMT_ZONE,
        TimeZone.UNKNOWN_ZONE,
        TimeZone.getFrozenTimeZone("America/Los_Angeles"),
        new SimpleTimeZone(3600000, "frz_stz").freeze(),
        new RuleBasedTimeZone("frz_rbtz", new InitialTimeZoneRule("frz_rbtz0", 3600000, 0)).freeze(),
        VTimeZone.create("Asia/Tokyo").freeze(),
    };

    checkFrozen(ZA2, "ZA2");
    TimeZone[] ZA2_thawed = new TimeZone[ZA2.length];
    // create thawed clone
    for (int i = 0; i < ZA2_thawed.length; i++) {
        ZA2_thawed[i] = ZA2[i].cloneAsThawed();
    }
    checkThawed(ZA2_thawed, "ZA2(thawed)");

}
 
Example 12
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void Test4083167() {
    TimeZone saveZone = TimeZone.getDefault();
    try {
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
        Date firstDate = new Date();
        Calendar cal = new GregorianCalendar();
        cal.setTime(firstDate);
        long firstMillisInDay = cal.get(Calendar.HOUR_OF_DAY) * 3600000L +
            cal.get(Calendar.MINUTE) * 60000L +
            cal.get(Calendar.SECOND) * 1000L +
            cal.get(Calendar.MILLISECOND);
        
        logln("Current time: " + firstDate.toString());

        for (int validity=0; validity<30; validity++) {
            Date lastDate = new Date(firstDate.getTime() +
                                     (long)validity*1000*24*60*60);
            cal.setTime(lastDate);
            long millisInDay = cal.get(Calendar.HOUR_OF_DAY) * 3600000L +
                cal.get(Calendar.MINUTE) * 60000L +
                cal.get(Calendar.SECOND) * 1000L +
                cal.get(Calendar.MILLISECOND);
            if (firstMillisInDay != millisInDay) 
                errln("Day has shifted " + lastDate);
        }
    }
    finally {
        TimeZone.setDefault(saveZone);
    }
}
 
Example 13
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * DateFormat class mistakes date style and time style as follows: -
 * DateFormat.getDateTimeInstance takes date style as time style, and time
 * style as date style - If a Calendar is passed to
 * DateFormat.getDateInstance, it returns time instance - If a Calendar is
 * passed to DateFormat.getTimeInstance, it returns date instance
 */
@Test
public void TestDateFormatFactoryJ26() {
    TimeZone zone = TimeZone.getDefault();
    try {
        Locale loc = Locale.US;
        TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
        java.util.Calendar tempcal = java.util.Calendar.getInstance();
        tempcal.set(2001, Calendar.APRIL, 5, 17, 43, 53);
        Date date = tempcal.getTime();
        Calendar cal = Calendar.getInstance(loc);
        Object[] DATA = {
            DateFormat.getDateInstance(DateFormat.SHORT, loc),
            "DateFormat.getDateInstance(DateFormat.SHORT, loc)",
            "4/5/01",

            DateFormat.getTimeInstance(DateFormat.SHORT, loc),
            "DateFormat.getTimeInstance(DateFormat.SHORT, loc)",
            "5:43 PM",

            DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, loc),
            "DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, loc)",
            "Thursday, April 5, 2001 at 5:43 PM",

            DateFormat.getDateInstance(cal, DateFormat.SHORT, loc),
            "DateFormat.getDateInstance(cal, DateFormat.SHORT, loc)",
            "4/5/01",

            DateFormat.getTimeInstance(cal, DateFormat.SHORT, loc),
            "DateFormat.getTimeInstance(cal, DateFormat.SHORT, loc)",
            "5:43 PM",

            DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.SHORT, loc),
            "DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.SHORT, loc)",
            "Thursday, April 5, 2001 at 5:43 PM",
        
            cal.getDateTimeFormat(DateFormat.SHORT, DateFormat.FULL, loc),
            "cal.getDateTimeFormat(DateFormat.SHORT, DateFormat.FULL, loc)",
            "4/5/01, 5:43:53 PM Pacific Daylight Time",

            cal.getDateTimeFormat(DateFormat.FULL, DateFormat.SHORT, loc),
            "cal.getDateTimeFormat(DateFormat.FULL, DateFormat.SHORT, loc)",
            "Thursday, April 5, 2001 at 5:43 PM",
        };
        for (int i=0; i<DATA.length; i+=3) {
            DateFormat df = (DateFormat) DATA[i];
            String desc = (String) DATA[i+1];
            String exp = (String) DATA[i+2];
            String got = df.format(date);
            if (got.equals(exp)) {
                logln("Ok: " + desc + " => " + got);
            } else {
                errln("FAIL: " + desc + " => " + got + ", expected " + exp);
            }
        }
    } finally {
        TimeZone.setDefault(zone);
    }
}
 
Example 14
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * SimpleTimeZone.getOffset accepts illegal arguments.
 */
@Test
public void Test4154650() {
    final int GOOD=1, BAD=0;
    final int GOOD_ERA=GregorianCalendar.AD, GOOD_YEAR=1998, GOOD_MONTH=Calendar.AUGUST;
    final int GOOD_DAY=2, GOOD_DOW=Calendar.SUNDAY, GOOD_TIME=16*3600000;
    int[] DATA = {
        GOOD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, GOOD_TIME,

        GOOD, GregorianCalendar.BC, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, GOOD_TIME,
        GOOD, GregorianCalendar.AD, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, GOOD_TIME,
        BAD,  GregorianCalendar.BC-1, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, GOOD_TIME,
        BAD,  GregorianCalendar.AD+1, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, GOOD_TIME,

        GOOD, GOOD_ERA, GOOD_YEAR, Calendar.JANUARY, GOOD_DAY, GOOD_DOW, GOOD_TIME,
        GOOD, GOOD_ERA, GOOD_YEAR, Calendar.DECEMBER, GOOD_DAY, GOOD_DOW, GOOD_TIME,
        BAD,  GOOD_ERA, GOOD_YEAR, Calendar.JANUARY-1, GOOD_DAY, GOOD_DOW, GOOD_TIME,
        BAD,  GOOD_ERA, GOOD_YEAR, Calendar.DECEMBER+1, GOOD_DAY, GOOD_DOW, GOOD_TIME,

        GOOD, GOOD_ERA, GOOD_YEAR, Calendar.JANUARY, 1, GOOD_DOW, GOOD_TIME,
        GOOD, GOOD_ERA, GOOD_YEAR, Calendar.JANUARY, 31, GOOD_DOW, GOOD_TIME,
        BAD,  GOOD_ERA, GOOD_YEAR, Calendar.JANUARY, 0, GOOD_DOW, GOOD_TIME,
        BAD,  GOOD_ERA, GOOD_YEAR, Calendar.JANUARY, 32, GOOD_DOW, GOOD_TIME,

        GOOD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, Calendar.SUNDAY, GOOD_TIME,
        GOOD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, Calendar.SATURDAY, GOOD_TIME,
        BAD,  GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, Calendar.SUNDAY-1, GOOD_TIME,
        BAD,  GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, Calendar.SATURDAY+1, GOOD_TIME,

        GOOD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, 0,
        GOOD, GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, 24*3600000-1,
        BAD,  GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, -1,
        BAD,  GOOD_ERA, GOOD_YEAR, GOOD_MONTH, GOOD_DAY, GOOD_DOW, 24*3600000,
    };

    TimeZone tz = TimeZone.getDefault();
    for (int i=0; i<DATA.length; i+=7) {
        boolean good = DATA[i] == GOOD;
        IllegalArgumentException e = null;
        try {
            /*int offset =*/ tz.getOffset(DATA[i+1], DATA[i+2], DATA[i+3],
                                      DATA[i+4], DATA[i+5], DATA[i+6]);
            //offset = 0;
       } catch (IllegalArgumentException ex) {
            e = ex;
        }
        if (good != (e == null)) {
            errln("Fail: getOffset(" +
                  DATA[i+1] + ", " + DATA[i+2] + ", " + DATA[i+3] + ", " +
                  DATA[i+4] + ", " + DATA[i+5] + ", " + DATA[i+6] +
                  (good ? (") threw " + e) : ") accepts invalid args"));
        }
    }
}
 
Example 15
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * TimeZone broken at midnight.  The TimeZone code fails to handle
 * transitions at midnight correctly.
 */
@Test
public void Test4162593() {
    SimpleDateFormat fmt = new SimpleDateFormat("z", Locale.US);
    final int ONE_HOUR = 60*60*1000;
    final float H = (float) ONE_HOUR;
    TimeZone initialZone = TimeZone.getDefault();
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy HH:mm z");

    SimpleTimeZone asuncion = new SimpleTimeZone(-4*ONE_HOUR, "America/Asuncion" /*PY%sT*/,
        Calendar.OCTOBER, 1, 0 /*DOM*/, 0*ONE_HOUR,
        Calendar.MARCH, 1, 0 /*DOM*/, 0*ONE_HOUR, 1*ONE_HOUR);

    /* Zone
     * Starting time
     * Transition expected between start+1H and start+2H
     */
    Object[] DATA = {
        new SimpleTimeZone(2*ONE_HOUR, "Asia/Damascus" /*EE%sT*/,
            Calendar.APRIL, 1, 0 /*DOM*/, 0*ONE_HOUR,
            Calendar.OCTOBER, 1, 0 /*DOM*/, 0*ONE_HOUR, 1*ONE_HOUR),
        new int[] {1998, Calendar.SEPTEMBER, 30, 22, 0},
        Boolean.TRUE,

        asuncion,
        new int[] {2000, Calendar.FEBRUARY, 28, 22, 0},
        Boolean.FALSE,

        asuncion,
        new int[] {2000, Calendar.FEBRUARY, 29, 22, 0},
        Boolean.TRUE,
    };

    String[] zone = new String[4];

    for (int j=0; j<DATA.length; j+=3) {
        TimeZone tz = (TimeZone)DATA[j];
        TimeZone.setDefault(tz);
        fmt.setTimeZone(tz);
        sdf.setTimeZone(tz);

        // Must construct the Date object AFTER setting the default zone
        int[] p = (int[])DATA[j+1];
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(p[0], p[1], p[2], p[3], p[4]);
        long start = cal.getTime().getTime();
        boolean transitionExpected = ((Boolean)DATA[j+2]).booleanValue();

        logln(tz.getID() + ":");
        for (int i=0; i<4; ++i) {
            Date d = new Date(start + i*ONE_HOUR);
            zone[i] = fmt.format(d);
            logln("" + i + ": " + sdf.format(d) + " => " + zone[i] +
                  " (" + d.getTime()/H + ")");
        }
        cal.set(p[0], p[1], p[2], 0, 0);
        for (int i=0; i<4; ++i) {
            int h = 22+i;
            int dom = p[2]+(h>=24?1:0);
            h %= 24;
            int ms = h*ONE_HOUR;
            cal.clear();
            cal.set(p[0], p[1], dom, 0, 0);
            int off = tz.getOffset(GregorianCalendar.AD,
                                   cal.get(Calendar.YEAR),
                                   cal.get(Calendar.MONTH),
                                   cal.get(Calendar.DATE),
                                   cal.get(Calendar.DAY_OF_WEEK),
                                   ms);
            cal.add(Calendar.HOUR, h);
            int dstOffset = cal.get(Calendar.DST_OFFSET);
            logln("h=" + h + "; dom=" + dom +
                  "; ZONE_OFFSET=" + cal.get(Calendar.ZONE_OFFSET)/H +
                  "; DST_OFFSET=" + dstOffset/H +
                  "; getOffset()=" + off/H +
                  " (" + cal.getTime().getTime()/H + ")");
        }
        if (zone[0].equals(zone[1]) &&
            (zone[1].equals(zone[2]) != transitionExpected) &&
            zone[2].equals(zone[3])) {
            logln("Ok: transition " + transitionExpected);
        } else {
            errln("FAIL: expected " +
                  (transitionExpected?"transition":"no transition"));
        }
    }

// restore the initial time zone so that this test case
// doesn't affect the others.
TimeZone.setDefault(initialZone);
}
 
Example 16
Source File: JapaneseTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void Test5345calendar() {
    logln("** testIncompleteCalendar()");
    // Test calendar with incomplete information
    JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault());
    logln("test clear");
    c.clear();
 
    // Showa 45 = Gregorian 1970
    int expected0[] = {   Calendar.ERA, 234,
                          Calendar.YEAR, 45 };
    checkExpected(c, expected0);

    logln("test setting era");
    c.clear();
    c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
    
    
    int expectedA[] = {   Calendar.ERA, JapaneseCalendar.MEIJI };
    checkExpected(c, expectedA);


    logln("test setting era and year and month and date");
    c.clear();
    c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
    c.set(Calendar.YEAR, 1);
    c.set(Calendar.MONTH, Calendar.JANUARY);
    c.set(Calendar.DATE, 1);


    int expectedC[] = {   Calendar.ERA, JapaneseCalendar.MEIJI -1};
    checkExpected(c, expectedC);


    logln("test setting  year and month and date THEN era");
    c.clear();
    c.set(Calendar.YEAR, 1);
    c.set(Calendar.MONTH, Calendar.JANUARY);
    c.set(Calendar.DATE, 1);
    c.set(Calendar.ERA, JapaneseCalendar.MEIJI);
    
    
    checkExpected(c, expectedC);
    
    
    logln("test setting era and year");
    c.clear();
    c.set(Calendar.YEAR, 1);
    c.set(Calendar.ERA, JapaneseCalendar.MEIJI);


    int expectedB[] = { Calendar.ERA, JapaneseCalendar.MEIJI,
                        Calendar.YEAR, 1 };
    checkExpected(c, expectedB);

}
 
Example 17
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * @bug 4065240
 */
@Test
public void Test4065240() {
    Date curDate;
    DateFormat shortdate, fulldate;
    String strShortDate, strFullDate;
    Locale saveLocale = Locale.getDefault();
    TimeZone saveZone = TimeZone.getDefault();

    try {
        Locale curLocale = new Locale("de", "DE");
        Locale.setDefault(curLocale);
        // {sfb} adoptDefault instead of setDefault
        //TimeZone.setDefault(TimeZone.createTimeZone("EST"));
        TimeZone.setDefault(TimeZone.getTimeZone("EST"));
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(98 + 1900, 0, 1);
        curDate = cal.getTime();
        shortdate = DateFormat.getDateInstance(DateFormat.SHORT);
        fulldate = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
        strShortDate = "The current date (short form) is ";
        String temp;
        temp = shortdate.format(curDate);
        strShortDate += temp;
        strFullDate = "The current date (long form) is ";
        String temp2 = fulldate.format(curDate);
        strFullDate += temp2;

        logln(strShortDate);
        logln(strFullDate);

        // {sfb} What to do with resource bundle stuff?????

        // Check to see if the resource is present; if not, we can't test
        //ResourceBundle bundle = //The variable is never used
        //    ICULocaleData.getBundle("DateFormatZoneData", curLocale); 

        // {sfb} API change to ResourceBundle -- add getLocale()
        /*if (bundle.getLocale().getLanguage().equals("de")) {
            // UPDATE THIS AS ZONE NAME RESOURCE FOR <EST> in de_DE is updated
            if (!strFullDate.endsWith("GMT-05:00"))
                errln("Fail: Want GMT-05:00");
        } else {
            logln("*** TEST COULD NOT BE COMPLETED BECAUSE DateFormatZoneData ***");
            logln("*** FOR LOCALE de OR de_DE IS MISSING ***");
        }*/
    } catch (Exception e) {
        logln(e.getMessage());
    } finally {
        Locale.setDefault(saveLocale);
        TimeZone.setDefault(saveZone);
    }

}
 
Example 18
Source File: JapaneseTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void Test5345parse() {
    // Test parse with incomplete information
    DateFormat fmt2= DateFormat.getDateInstance(); //DateFormat.LONG, Locale.US);
    JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault(), new ULocale("en_US"));
    SimpleDateFormat fmt = (SimpleDateFormat)c.getDateTimeFormat(1,1,new ULocale("en_US@calendar=japanese"));
    fmt.applyPattern("G y");
    logln("fmt's locale = " + fmt.getLocale(ULocale.ACTUAL_LOCALE));
    //SimpleDateFormat fmt = new SimpleDateFormat("G y", new Locale("en_US@calendar=japanese"));
    long aDateLong = -3197117222000L; // 1868-09-08 00:00 Pacific Time (GMT-07:52:58)
    if (TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK) {
        // Java time zone implementation does not support LMTs
        aDateLong = -3197116800000L; // 1868-09-08 00:00 Pacific Time (GMT-08:00)
    }
    Date aDate = new Date(aDateLong);
    logln("aDate: " + aDate.toString() +", from " + aDateLong);
    String str;
    str = fmt2.format(aDate);
    logln("Test Date: " + str);
    str = fmt.format(aDate);
    logln("as Japanese Calendar: " + str);
    String expected = "Meiji 1";
    if(!str.equals(expected)) {
        errln("FAIL: Expected " + expected + " but got " + str);
    }
    Date otherDate;
    try {
        otherDate = fmt.parse(expected);
        if(!otherDate.equals(aDate)) { 
            String str3;
//            ParsePosition pp;
            Date dd = fmt.parse(expected);
            str3 = fmt.format(otherDate);
            long oLong = otherDate.getTime();
            long aLong = otherDate.getTime();
            
            errln("FAIL: Parse incorrect of " + expected + ":  wanted " + aDate + " ("+aLong+"), but got " +  " " +
                otherDate + " ("+oLong+") = " + str3 + " not " + dd.toString() );


        } else {
            logln("Parsed OK: " + expected);
        }
    } catch(java.text.ParseException pe) {
        errln("FAIL: ParseException: " + pe.toString());
        pe.printStackTrace();
    }
}
 
Example 19
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * @bug 4106807
 */
@Test
public void Test4106807() {
    Date dt;
    DateFormat df = DateFormat.getDateTimeInstance();

    SimpleDateFormat sdfs[] = {
            new SimpleDateFormat("yyyyMMddHHmmss"), 
            new SimpleDateFormat("yyyyMMddHHmmss'Z'"), 
            new SimpleDateFormat("yyyyMMddHHmmss''"), 
            new SimpleDateFormat("yyyyMMddHHmmss'a''a'"), 
            new SimpleDateFormat("yyyyMMddHHmmss %")}; 
    String strings[] = {
            "19980211140000", 
            "19980211140000", 
            "19980211140000", 
            "19980211140000a", 
            "19980211140000 "}; 
    GregorianCalendar gc = new GregorianCalendar();
    TimeZone timeZone = TimeZone.getDefault();
    TimeZone gmt = (TimeZone) timeZone.clone();
    gmt.setRawOffset(0);
    for (int i = 0; i < 5; i++) {
        SimpleDateFormat format = sdfs[i];
        String dateString = strings[i];
        try {
            format.setTimeZone(gmt);
            dt = format.parse(dateString);
            // {sfb} some of these parses will fail purposely

            StringBuffer fmtd = new StringBuffer("");
            FieldPosition pos = new FieldPosition(0);
            fmtd = df.format(dt, fmtd, pos);
            logln(fmtd.toString());
            //logln(df.format(dt)); 
            gc.setTime(dt);
            logln("" + gc.get(Calendar.ZONE_OFFSET));
            StringBuffer s = new StringBuffer("");
            s = format.format(dt, s, pos);
            logln(s.toString());
        } catch (ParseException e) {
            logln("No way Jose");
        }
    }
}
 
Example 20
Source File: ChineseDateFormat.java    From j2objc with Apache License 2.0 2 votes vote down vote up
/**
 * Construct a ChineseDateFormat from a date format pattern, numbering system override and locale
 * @param pattern the pattern
 * @param override The override string.  A numbering system override string can take one of the following forms:
 *     1). If just a numbering system name is specified, it applies to all numeric fields in the date format pattern.
 *     2). To specify an alternate numbering system on a field by field basis, use the field letters from the pattern
 *         followed by an = sign, followed by the numbering system name.  For example, to specify that just the year
 *         be formatted using Hebrew digits, use the override "y=hebr".  Multiple overrides can be specified in a single
 *         string by separating them with a semi-colon. For example, the override string "m=thai;y=deva" would format using
 *         Thai digits for the month and Devanagari digits for the year.
 * @param locale the locale
 * @deprecated ICU 50
 */
@Deprecated
public ChineseDateFormat(String pattern, String override, ULocale locale) {
   super(pattern, new ChineseDateFormatSymbols(locale),
           new ChineseCalendar(TimeZone.getDefault(), locale), locale, true, override);
}