android.icu.text.DateFormat Java Examples

The following examples show how to use android.icu.text.DateFormat. 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: VibratorService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public String toString() {
    return new StringBuilder()
            .append("startTime: ")
            .append(DateFormat.getDateTimeInstance().format(new Date(mStartTimeDebug)))
            .append(", effect: ")
            .append(mEffect)
            .append(", originalEffect: ")
            .append(mOriginalEffect)
            .append(", usageHint: ")
            .append(mUsageHint)
            .append(", uid: ")
            .append(mUid)
            .append(", opPkg: ")
            .append(mOpPkg)
            .toString();
}
 
Example #2
Source File: DatePickerCalendarDelegate.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
protected void onLocaleChanged(Locale locale) {
    final TextView headerYear = mHeaderYear;
    if (headerYear == null) {
        // Abort, we haven't initialized yet. This method will get called
        // again later after everything has been set up.
        return;
    }

    // Update the date formatter.
    mMonthDayFormat = DateFormat.getInstanceForSkeleton("EMMMd", locale);
    mMonthDayFormat.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE);
    mYearFormat = DateFormat.getInstanceForSkeleton("y", locale);

    // Update the header text.
    onCurrentDateChanged(false);
}
 
Example #3
Source File: TestCLDRVsICU.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Ignore
@Test
public void TestFiles() throws SAXException, IOException {
    // only get ICU's locales
    Set s = new TreeSet();
    addLocales(NumberFormat.getAvailableULocales(), s);
    addLocales(DateFormat.getAvailableULocales(), s);

    // johnvu: Collator was originally disabled
    // addLocales(Collator.getAvailableULocales(), s);

    // filter, to make tracking down bugs easier
    for (Iterator it = s.iterator(); it.hasNext();) {
        String locale = (String) it.next();
        if (!LOCALE_MATCH.reset(locale).matches())
            continue;
        _test(locale);
    }
}
 
Example #4
Source File: GlobalizationPreferences.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * This function can be overridden by subclasses to use different heuristics.
 * <b>It MUST return a 'safe' value,
 * one whose modification will not affect this object.</b>
 *
 * @param dateStyle
 * @param timeStyle
 * @hide draft / provisional / internal are hidden on Android
 */
protected DateFormat guessDateFormat(int dateStyle, int timeStyle) {
    DateFormat result;
    ULocale dfLocale = getAvailableLocale(TYPE_DATEFORMAT);
    if (dfLocale == null) {
        dfLocale = ULocale.ROOT;
    }
    if (timeStyle == DF_NONE) {
        result = DateFormat.getDateInstance(getCalendar(), dateStyle, dfLocale);
    } else if (dateStyle == DF_NONE) {
        result = DateFormat.getTimeInstance(getCalendar(), timeStyle, dfLocale);
    } else {
        result = DateFormat.getDateTimeInstance(getCalendar(), dateStyle, timeStyle, dfLocale);
    }
    return result;
}
 
Example #5
Source File: IndianTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestYear() {
    // Gregorian Calendar
    Calendar gCal= new GregorianCalendar();
    Date gToday=gCal.getTime();
    gCal.add(GregorianCalendar.MONTH,2);
    Date gFuture=gCal.getTime();
    DateFormat gDF = DateFormat.getDateInstance(gCal,DateFormat.FULL);
    logln("gregorian calendar: " + gDF.format(gToday) +
          " + 2 months = " + gDF.format(gFuture));

    // Indian Calendar
    IndianCalendar iCal= new IndianCalendar();
    Date iToday=iCal.getTime();
    iCal.add(IndianCalendar.MONTH,2);
    Date iFuture=iCal.getTime();
    DateFormat iDF = DateFormat.getDateInstance(iCal,DateFormat.FULL);
    logln("Indian calendar: " + iDF.format(iToday) +
          " + 2 months = " + iDF.format(iFuture));

}
 
Example #6
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * @bug 4073003
 */
@Test
public void Test4073003() {
    try {
        DateFormat fmt = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
        String tests[] = {"12/25/61", "12/25/1961", "4/3/2010", "4/3/10"};
        for (int i = 0; i < 4; i += 2) {
            Date d = fmt.parse(tests[i]);
            Date dd = fmt.parse(tests[i + 1]);
            String s;
            s = fmt.format(d);
            String ss;
            ss = fmt.format(dd);
            if (d.getTime() != dd.getTime())
                errln("Fail: " + d + " != " + dd);
            if (!s.equals(ss))
                errln("Fail: " + s + " != " + ss);
            logln("Ok: " + s + " " + d);
        }
    } catch (ParseException e) {
        errln("Fail: " + e);
        e.printStackTrace();
    }    
}
 
Example #7
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * @bug 4071441
 */
@Test
public void Test4071441() {
    DateFormat fmtA = DateFormat.getInstance();
    DateFormat fmtB = DateFormat.getInstance();

    // {sfb} Is it OK to cast away const here?
    Calendar calA = fmtA.getCalendar();
    Calendar calB = fmtB.getCalendar();
    calA.clear();
    calA.set(1900, 0 ,0);
    calB.clear();
    calB.set(1900, 0, 0);
    if (!calA.equals(calB))
        errln("Fail: Can't complete test; Calendar instances unequal");
    if (!fmtA.equals(fmtB))
        errln("Fail: DateFormat unequal when Calendars equal");
    calB.clear();
    calB.set(1961, Calendar.DECEMBER, 25);
    if (calA.equals(calB))
        errln("Fail: Can't complete test; Calendar instances equal");
    if (!fmtA.equals(fmtB))
        errln("Fail: DateFormat unequal when Calendars equivalent");
    logln("DateFormat.equals ok");
}
 
Example #8
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void Test4106136() {
    Locale saveLocale = Locale.getDefault();
    String[] names = { "Calendar", "DateFormat", "NumberFormat" };
    try {
        Locale[] locales = { Locale.CHINESE, Locale.CHINA };
        for (int i=0; i<locales.length; ++i) {
            Locale.setDefault(locales[i]);
            int[] n = {
                Calendar.getAvailableLocales().length,
                DateFormat.getAvailableLocales().length,
                NumberFormat.getAvailableLocales().length
            };
            for (int j=0; j<n.length; ++j) {
                if (n[j] == 0)
                    errln("Fail: " + names[j] + " has no locales for " + locales[i]);
            }
        }
    }
    finally {
        Locale.setDefault(saveLocale);
    }
}
 
Example #9
Source File: DateFormatRegressionTestJ.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void Test4253490() {
    Date d = new Date(1002705212231L);

    String[] ISOPattern = {
            "''yyyy-MM-dd-hh.mm.ss.S''", 
            "''yyyy-MM-dd-hh.mm.ss.SS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSSS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSSSS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSSSSS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSSSSSS''"}; 

    SimpleDateFormat aSimpleDF = (SimpleDateFormat) DateFormat.getDateTimeInstance();
    for (int i = 0; i < ISOPattern.length; i++) {
        aSimpleDF.applyPattern(ISOPattern[i]);
        logln("Pattern = " + aSimpleDF.toPattern());
        logln("Format = " + aSimpleDF.format(d));
    }
}
 
Example #10
Source File: IntlTestDateFormat.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestRoundtrip() {
    ULocale[] locales;
    if (isQuick()) {
        locales = new ULocale[] {
                new ULocale("bg_BG"),
                new ULocale("fr_CA"),
                new ULocale("zh_TW"),
        };
    } else {
        locales = DateFormat.getAvailableULocales();
    }
    long count = locales.length;
    if (locales != null  &&  count != 0) {
        for (int i=0; i<count; ++i) {
            String name = locales[i].getDisplayName();
            logln("Testing " + name + "...");
            try {
                localeTest(locales[i], name);
            }
            catch(Exception e) {
                errln("FAIL: TestMonster localeTest exception" + e);
            }
        }
    }
}
 
Example #11
Source File: DateFormatRegressionTestJ.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void Test4148168() {
        Date d = new Date(1002705212906L);
        String[] ISOPattern = {
            "''yyyy-MM-dd-hh.mm.ss.S''", "''yyyy-MM-dd-hh.mm.ss.SS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSS''", "''yyyy-MM-dd-hh.mm.ss.SSSS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSSSS''", "''yyyy-MM-dd-hh.mm.ss.SSSSSS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSSSSSS''", "''yyyy-MM-dd-hh.mm.ss.SSS000''"};
        SimpleDateFormat aSimpleDF = (SimpleDateFormat)DateFormat.getDateTimeInstance();

        for(int i = 0; i<ISOPattern.length; i++) {
            aSimpleDF.applyPattern( ISOPattern[i] );
            logln( "Pattern = " + aSimpleDF.toPattern());
            logln( "Format = " + aSimpleDF.format(d));
        }
}
 
Example #12
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestYearJump3279() {
    final long time = 1041148800000L;
    Calendar c = new GregorianCalendar();
    DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US);

    c.setTimeInMillis(time);
    int year1 = c.get(Calendar.YEAR);
    
    logln("time: " + fmt.format(new Date(c.getTimeInMillis())));

    logln("setting DOW to " + c.getFirstDayOfWeek());
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    logln("week: " + c.getTime());
    logln("week adjust: " + fmt.format(new Date(c.getTimeInMillis())));
    int year2 = c.get(Calendar.YEAR);
    
    if(year1 != year2) {
        errln("Error: adjusted day of week, and year jumped from " + year1 + " to " + year2);
    } else {
        logln("Year remained " + year2 + " - PASS.");
    }
}
 
Example #13
Source File: TestCLDRVsICU.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private SimpleDateFormat getDateFormat(ULocale locale, int dateFormat, int timeFormat) {
    if (DEBUG)
        logln("Getting date/time format for " + locale);
    if (DEBUG && "ar_EG".equals(locale.toString())) {
        logln("debug here");
    }
    DateFormat dt;
    if (dateFormat == 0) {
        dt = DateFormat.getTimeInstance(DateFormatValues[timeFormat], locale);
        if (DEBUG)
            System.out.print("getTimeInstance");
    } else if (timeFormat == 0) {
        dt = DateFormat.getDateInstance(DateFormatValues[dateFormat], locale);
        if (DEBUG)
            System.out.print("getDateInstance");
    } else {
        dt = DateFormat.getDateTimeInstance(DateFormatValues[dateFormat], DateFormatValues[timeFormat],
                locale);
        if (DEBUG)
            System.out.print("getDateTimeInstance");
    }
    if (DEBUG)
        logln("\tinput:\t" + dateFormat + ", " + timeFormat + " => " + ((SimpleDateFormat) dt).toPattern());
    return (SimpleDateFormat) dt;
}
 
Example #14
Source File: DateTimeGeneratorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestOrdering() {
    ULocale[] locales = ULocale.getAvailableLocales();
    for (int i = 0; i < locales.length; ++i) {
        for (int style1 = DateFormat.FULL; style1 <= DateFormat.SHORT; ++style1) {
            for (int style2 = DateFormat.FULL; style2 < style1; ++style2) {
                checkCompatible(style1, style2, locales[i]);
            }
        }
    }
}
 
Example #15
Source File: DateTimeStyleSet.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private int getOrNone(int which) {
    if(!isSet(which)) {
        return DateFormat.NONE;
    } else {
        return get(which);
    }
}
 
Example #16
Source File: TimeZoneFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void TestInheritedFormat() {
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    Calendar cal = Calendar.getInstance(tz);
    cal.setTimeInMillis(1459187377690L); // Mar 28, 2016

    StringBuffer sb = new StringBuffer();
    FieldPosition fp = new FieldPosition(DateFormat.Field.TIME_ZONE);

    TimeZoneFormat fmt = TimeZoneFormat.getInstance(ULocale.ENGLISH);

    // Test formatting a non-timezone related object
    try {
        fmt.format(new Object(), sb, fp);
        errln("ERROR: format non-timezone related object failed");
    } catch (IllegalArgumentException e) { /* Expected */ }

    // Test formatting a TimeZone object
    sb = new StringBuffer();
    fmt.format(tz, sb, fp);
    // When formatting a TimeZone object the formatter uses the current date.
    String fmtOutput = tz.inDaylightTime(new Date()) ? "GMT-07:00" : "GMT-08:00";
    if (!sb.toString().equals(fmtOutput)) {
        errln("ERROR: format TimerZone object failed. Expected: " + fmtOutput + ", actual: " + sb);
    }

    // Test formatting a Calendar object
    sb = new StringBuffer();
    fmt.format(cal, sb, fp);
    if (!sb.toString().equals("GMT-07:00")) {
        errln("ERROR: format Calendar object failed. Expected: GMT-07:00, actual: " + sb);
    }
}
 
Example #17
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * @bug 4101483
 */
@Test
public void Test4101483() {
    SimpleDateFormat sdf = new SimpleDateFormat("z", Locale.US);
    FieldPosition fp = new FieldPosition(DateFormat.TIMEZONE_FIELD);
    Date d = new Date(9234567890L);
    StringBuffer buf = new StringBuffer("");
    sdf.format(d, buf, fp);
    logln(sdf.format(d, buf, fp).toString());
    logln("beginIndex = " + fp.getBeginIndex());
    logln("endIndex = " + fp.getEndIndex());
    if (fp.getBeginIndex() == fp.getEndIndex())
        errln("Fail: Empty field");
}
 
Example #18
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * j32 {JDK Bug 4210209 4209272}
 * DateFormat cannot parse Feb 29 2000 when setLenient(false)
 */
@Test
public void Test4210209() {

    String pattern = "MMM d, yyyy";
    DateFormat fmt = new SimpleDateFormat(pattern, Locale.US);
    DateFormat disp = new SimpleDateFormat("MMM dd yyyy GG", Locale.US);

    Calendar calx = fmt.getCalendar();
    calx.setLenient(false);
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(2000, Calendar.FEBRUARY, 29);
    Date d = calendar.getTime();
    String s = fmt.format(d);
    logln(disp.format(d) + " f> " + pattern + " => \"" + s + "\"");
    ParsePosition pos = new ParsePosition(0);
    d = fmt.parse(s, pos);
    logln("\"" + s + "\" p> " + pattern + " => " +
          (d!=null?disp.format(d):"null"));
    logln("Parse pos = " + pos.getIndex() + ", error pos = " + pos.getErrorIndex());
    if (pos.getErrorIndex() != -1) {
        errln("FAIL: Error index should be -1");
    }

    // The underlying bug is in GregorianCalendar.  If the following lines
    // succeed, the bug is fixed.  If the bug isn't fixed, they will throw
    // an exception.
    GregorianCalendar cal = new GregorianCalendar();
    cal.clear();
    cal.setLenient(false);
    cal.set(2000, Calendar.FEBRUARY, 29); // This should work!
    d = cal.getTime();
    logln("Attempt to set Calendar to Feb 29 2000: " + disp.format(d));
}
 
Example #19
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestT5683() {
    Locale[] aliasLocales = {
        new Locale("zh", "CN"),
        new Locale("zh", "TW"),
        new Locale("zh", "HK"),
        new Locale("zh", "SG"),
        new Locale("zh", "MO")
    };

    ULocale[] canonicalLocales = {
        new ULocale("zh_Hans_CN"),
        new ULocale("zh_Hant_TW"),
        new ULocale("zh_Hant_HK"),
        new ULocale("zh_Hans_SG"),
        new ULocale("zh_Hant_MO")
    };

    Date d = new Date(0);

    for (int i = 0; i < aliasLocales.length; i++) {
        DateFormat dfAlias = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, aliasLocales[i]);
        DateFormat dfCanonical = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, canonicalLocales[i]);

        String sAlias = dfAlias.format(d);
        String sCanonical = dfCanonical.format(d);

        if (!sAlias.equals(sCanonical)) {
            errln("Fail: The format result for locale " + aliasLocales[i] + " is different from the result for locale " + canonicalLocales[i]
                    + ": " + sAlias + "[" + aliasLocales[i] + "] / " + sCanonical + "[" + canonicalLocales[i] + "]");
        }
    }
}
 
Example #20
Source File: DateTimeGeneratorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Get the ordering from a particular date format. Best is to use
 * DateFormat.FULL to get the format with String form month (like "January")
 * and DateFormat.SHORT for the numeric format order. They may be different.
 * (Theoretically all 4 formats could be different but that never happens in
 * practice.)
 *
 * @param style
 *          DateFormat.FULL..DateFormat.SHORT
 * @param locale
 *          desired locale.
 * @return
 * @return list of ordered items DateFieldType (I
 *         didn't know what form you really wanted so this is just a
 *         stand-in.)
 */
private DateOrder getOrdering(int style, ULocale locale) {
    // and the date pattern
    String pattern = ((SimpleDateFormat) DateFormat.getDateInstance(style, locale)).toPattern();
    int count = 0;
    DateOrder result = new DateOrder();

    for (Iterator it = formatParser.set(pattern).getItems().iterator(); it.hasNext();) {
        Object item = it.next();
        if (!(item instanceof String)) {
            // the first character of the variable field determines the type,
            // according to CLDR.
            String variableField = item.toString();
            switch (variableField.charAt(0)) {
            case 'y': case 'Y': case 'u':
                result.fields[count++] = DateFieldType.YEAR;
                break;
            case 'M': case 'L':
                result.monthLength = variableField.length();
                if (result.monthLength < 2) {
                    result.monthLength = 2;
                }
                result.fields[count++] = DateFieldType.MONTH;
                break;
            case 'd': case 'D': case 'F': case 'g':
                result.fields[count++] = DateFieldType.DAY;
                break;
            }
        }
    }
    return result;
}
 
Example #21
Source File: IntlTestDateFormat.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestAvailableLocales() {
    final ULocale[] locales = DateFormat.getAvailableULocales();
    long count = locales.length;
    logln("" + count + " available locales");
    if (locales != null  &&  count != 0) {
        StringBuffer all = new StringBuffer();
        for (int i=0; i<count; ++i) {
            if (i!=0) all.append(", ");
            all.append(locales[i].getDisplayName());
        }
        logln(all.toString());
    }
    else errln("********** FAIL: Zero available locales or null array pointer");
}
 
Example #22
Source File: TestMessageFormat.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void TestDateFormatHashCode() {
    DateFormat testDF = DateFormat.getDateInstance(DateFormat.DEFAULT, ULocale.GERMAN);
    NumberFormat testNF = testDF.getNumberFormat();

    int expectedResult =
            testNF.getMaximumIntegerDigits() * 37 + testNF.getMaximumFractionDigits();
    int actualHashResult = testDF.hashCode();
    assertEquals("DateFormat hashCode", expectedResult, actualHashResult);
}
 
Example #23
Source File: LocaleAliasTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void  TestDateFormat() {
    ULocale defLoc = ULocale.getDefault();
    ULocale.setDefault(_DEFAULT_LOCALE);
    for (int i=0; i<_LOCALE_NUMBER; i++) {
        ULocale oldLoc = _LOCALES[i][0];
        ULocale newLoc = _LOCALES[i][1];
        if(availableMap.get(_LOCALES[i][1])==null){
            logln(_LOCALES[i][1]+" is not available. Skipping!");
            continue;
        }
        DateFormat df1 = DateFormat.getDateInstance(DateFormat.FULL, oldLoc);
        DateFormat df2 = DateFormat.getDateInstance(DateFormat.FULL, newLoc);
        
        //Test function "getLocale"
        ULocale l1 = df1.getLocale(ULocale.VALID_LOCALE);
        ULocale l2 = df2.getLocale(ULocale.VALID_LOCALE);
        if (!newLoc.equals(l1)) {
            errln("DateFormatTest: newLoc!=l1: newLoc= "+newLoc +" l1= "+l1);
        }
        if (!l1.equals(l2)) {
            errln("DateFormatTest: l1!=l2: l1= "+l1 +" l2= "+l2);
        }
        if (!df1.equals(df2)) {
            errln("DateFormatTest: df1!=df2: newLoc= "+newLoc +" oldLoc= "+oldLoc);
        }
        TestFmwk.logln("DateFormat(getLocale) old:"+l1+"   new:"+l2);
        
        //Test function "format"
//        Date d = new Date();
//        String d1 = df1.format(d);
//        String d2 = df2.format(d);
//        if (!d1.equals(d2)) {
//            pass = false;
//        }
//        this.logln("DateFormat(format) old:"+d1+"   new:"+d2);
    }
    ULocale.setDefault(defLoc);
}
 
Example #24
Source File: DateFormatRegressionTestJ.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void Test4250359() {
    Locale.setDefault(Locale.US);
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(101 + 1900, 9, 9, 17, 53);
    Date d = cal.getTime();
    DateFormat tf = DateFormat.getTimeInstance(DateFormat.SHORT);
    String act_result = tf.format(d);
    String exp_result = "5:53 PM";
    
    if(!act_result.equals(exp_result)){
        errln("The result is not expected");
    }
}
 
Example #25
Source File: ULocaleTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestDateFormat() {
    checkService("de_CH_ZURICH", new ServiceFacade() {
        @Override
        public Object create(ULocale req) {
            return DateFormat.getDateInstance(DateFormat.DEFAULT, req);
        }
    }, new Subobject() {
        @Override
        public Object get(Object parent) {
            return ((SimpleDateFormat) parent).getDateFormatSymbols();
        }
    }, null);
}
 
Example #26
Source File: NumberFormatRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * DateFormat should call setIntegerParseOnly(TRUE) on adopted
 * NumberFormat objects.
 */
@Test
public void TestJ691() {
    
    Locale loc = new Locale("fr", "CH");

    // set up the input date string & expected output
    String udt = "11.10.2000";
    String exp = "11.10.00";

    // create a Calendar for this locale
    Calendar cal = Calendar.getInstance(loc);

    // create a NumberFormat for this locale
    NumberFormat nf = NumberFormat.getInstance(loc);

    // *** Here's the key: We don't want to have to do THIS:
    //nf.setParseIntegerOnly(true);

    // create the DateFormat
    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, loc);

    df.setCalendar(cal);
    df.setNumberFormat(nf);

    // set parsing to lenient & parse
    Date ulocdat = new Date();
    df.setLenient(true);
    try {
        ulocdat = df.parse(udt);
    } catch (java.text.ParseException pe) {
        errln(pe.getMessage());
    }
    // format back to a string
    String outString = df.format(ulocdat);

    if (!outString.equals(exp)) {
        errln("FAIL: " + udt + " => " + outString);
    }
}
 
Example #27
Source File: IntlTestDateFormatAPI.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
    public void TestEquals()
    {
        // Create two objects at different system times
        DateFormat a = DateFormat.getInstance();
        Date start = Calendar.getInstance().getTime();
        while (true) {
            // changed to remove compiler warnings.
            if (!start.equals(Calendar.getInstance().getTime())) {
                break; // Wait for time to change
            }
        }
        DateFormat b = DateFormat.getInstance();

        if (!(a.equals(b)))
            errln("FAIL: DateFormat objects created at different times are unequal.");

        // Why has this test been disabled??? - aliu
//        if (b instanceof SimpleDateFormat)
//        {
//            //double ONE_YEAR = 365*24*60*60*1000.0; //The variable is never used
//            try {
//                ((SimpleDateFormat)b).setTwoDigitStartDate(start.getTime() + 50*ONE_YEAR);
//                if (a.equals(b))
//                    errln("FAIL: DateFormat objects with different two digit start dates are equal.");
//            }
//            catch (Exception e) {
//                errln("FAIL: setTwoDigitStartDate failed.");
//            }
//        }
    }
 
Example #28
Source File: DateIntervalFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetInstance_String_Locale_DateIntervalInfo() {
    DateIntervalInfo dateIntervalInfo = new DateIntervalInfo(new ULocale("ca"));
    DateIntervalFormat dateIntervalFormat = DateIntervalFormat.getInstance(
            DateFormat.YEAR_MONTH, Locale.GERMAN, dateIntervalInfo);
    Calendar from = Calendar.getInstance();
    from.set(2000, Calendar.JANUARY, 1, 12, 0);
    Calendar to = Calendar.getInstance();
    to.set(2001, Calendar.FEBRUARY, 1, 12, 0);
    DateInterval interval = new DateInterval(from.getTimeInMillis(), to.getTimeInMillis());
    dateIntervalFormat.setTimeZone(from.getTimeZone());
    // Month names are German, format is Catalan
    assertEquals("Wrong date interval",
            "Januar de 2000 – Februar de 2001", dateIntervalFormat.format(interval));
}
 
Example #29
Source File: DateIntervalFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetInstance_String_DateIntervalInfo() {
    DateIntervalInfo dateIntervalInfo = new DateIntervalInfo(new ULocale("ca"));
    DateIntervalFormat dateIntervalFormat = DateIntervalFormat.getInstance(
            DateFormat.YEAR_MONTH, Locale.ENGLISH, dateIntervalInfo);
    Calendar from = Calendar.getInstance();
    from.set(2000, Calendar.JANUARY, 1, 12, 0);
    Calendar to = Calendar.getInstance();
    to.set(2001, Calendar.FEBRUARY, 1, 12, 0);
    DateInterval interval = new DateInterval(from.getTimeInMillis(), to.getTimeInMillis());
    dateIntervalFormat.setTimeZone(from.getTimeZone());
    // Month names are default (English), format is Catalan
    assertEquals("Wrong date interval",
            "January de 2000 – February de 2001", dateIntervalFormat.format(interval));
}
 
Example #30
Source File: DateTimeStyleSet.java    From j2objc with Apache License 2.0 5 votes vote down vote up
protected void handleParseValue(FieldsSet inheritFrom, int field, String substr) {
    if(substr.startsWith(kRELATIVE_)) {
        parseValueEnum(DebugUtilitiesData.UDateFormatStyle, inheritFrom, field, substr.substring(kRELATIVE_.length()));
        if(isSet(field)) {
            set(field, get(field) | DateFormat.RELATIVE);
        }
    } else {
        parseValueEnum(DebugUtilitiesData.UDateFormatStyle, inheritFrom, field, substr);
    }
}