Java Code Examples for android.icu.text.DateFormat#getDateInstance()

The following examples show how to use android.icu.text.DateFormat#getDateInstance() . 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: 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 2
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 3
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 4
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 5
Source File: CopticTest.java    From j2objc with Apache License 2.0 5 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));

    // Coptic Calendar
    CopticCalendar cCal= new CopticCalendar();
    Date cToday=cCal.getTime();
    cCal.add(CopticCalendar.MONTH,2);
    Date cFuture=cCal.getTime();
    DateFormat cDF = DateFormat.getDateInstance(cCal,DateFormat.FULL);
    logln("coptic calendar: " + cDF.format(cToday) +
          " + 2 months = " + cDF.format(cFuture));

    // EthiopicCalendar
    EthiopicCalendar eCal= new EthiopicCalendar();
    Date eToday=eCal.getTime();
    eCal.add(EthiopicCalendar.MONTH,2); // add 2 months
    eCal.setAmeteAlemEra(false);
    Date eFuture=eCal.getTime();
    DateFormat eDF = DateFormat.getDateInstance(eCal,DateFormat.FULL);
    logln("ethiopic calendar: " + eDF.format(eToday) +
          " + 2 months = " + eDF.format(eFuture));
}
 
Example 6
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 7
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 8
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestDangiFormat() {
    DateFormat fmt = DateFormat.getDateInstance(DateFormat.MEDIUM, new ULocale("en@calendar=dangi"));
    String calType = fmt.getCalendar().getType();
    assertEquals("Incorrect calendar type used by the date format instance", "dangi", calType);

    GregorianCalendar gcal = new GregorianCalendar();
    gcal.set(2013, Calendar.MARCH, 1, 0, 0, 0);
    Date d = gcal.getTime();

    String dangiDateStr = fmt.format(d);
    assertEquals("Bad date format", "Mo1 20, 2013", dangiDateStr);
}
 
Example 9
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 10
Source File: DangiTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Make sure IS_LEAP_MONTH participates in field resolution.
 */
@Test
public void TestResolution() {
    Calendar cal = Calendar.getInstance(new ULocale("ko_KR@calendar=dangi"));
    DateFormat fmt = DateFormat.getDateInstance(cal, DateFormat.DEFAULT);

    // May 22 4334 = y4334 m4 d30 doy119
    // May 23 4334 = y4334 m4* d1 doy120

    final int THE_YEAR = 4334;
    final int END = -1;

    int[] DATA = {
        // Format:
        // (field, value)+, END, exp.month, exp.isLeapMonth, exp.DOM
        // Note: exp.month is ONE-BASED

        // If we set DAY_OF_YEAR only, that should be used
        Calendar.DAY_OF_YEAR, 1,
        END,
        1,0,1, // Expect 1-1
        
        // If we set MONTH only, that should be used
        Calendar.IS_LEAP_MONTH, 1,
        Calendar.DAY_OF_MONTH, 1,
        Calendar.MONTH, 3,
        END,
        4,1,1, // Expect 4*-1
        
        // If we set the DOY last, that should take precedence
        Calendar.MONTH, 1, // Should ignore
        Calendar.IS_LEAP_MONTH, 1, // Should ignore
        Calendar.DAY_OF_MONTH, 1, // Should ignore
        Calendar.DAY_OF_YEAR, 121,
        END,
        4,1,2, // Expect 4*-2
        
        // If we set IS_LEAP_MONTH last, that should take precedence
        Calendar.MONTH, 3,
        Calendar.DAY_OF_MONTH, 1,
        Calendar.DAY_OF_YEAR, 5, // Should ignore
        Calendar.IS_LEAP_MONTH, 1,
        END,
        4,1,1, // Expect 4*-1
    };

    StringBuilder buf = new StringBuilder();
    for (int i=0; i<DATA.length; ) {
        cal.clear();
        cal.set(Calendar.EXTENDED_YEAR, THE_YEAR);
        buf.setLength(0);
        buf.append("EXTENDED_YEAR=" + THE_YEAR);
        while (DATA[i] != END) {
            cal.set(DATA[i++], DATA[i++]);
            buf.append(" " + fieldName(DATA[i-2]) + "=" + DATA[i-1]);
        }
        ++i; // Skip over END mark
        int expMonth = DATA[i++]-1;
        int expIsLeapMonth = DATA[i++];
        int expDOM = DATA[i++];
        int month = cal.get(Calendar.MONTH);
        int isLeapMonth = cal.get(Calendar.IS_LEAP_MONTH);
        int dom = cal.get(Calendar.DAY_OF_MONTH);
        if (expMonth == month && expIsLeapMonth == isLeapMonth &&
            dom == expDOM) {
            logln("OK: " + buf + " => " + fmt.format(cal.getTime()));
        } else {
            String s = fmt.format(cal.getTime());
            cal.clear();
            cal.set(Calendar.EXTENDED_YEAR, THE_YEAR);
            cal.set(Calendar.MONTH, expMonth);
            cal.set(Calendar.IS_LEAP_MONTH, expIsLeapMonth);
            cal.set(Calendar.DAY_OF_MONTH, expDOM);
            errln("Fail: " + buf + " => " + s +
                  "=" + (month+1) + "," + isLeapMonth + "," + dom +
                  ", expected " + fmt.format(cal.getTime()) +
                  "=" + (expMonth+1) + "," + expIsLeapMonth + "," + expDOM);
        }
    }
}
 
Example 11
Source File: ChineseTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Make sure IS_LEAP_MONTH participates in field resolution.
 */
@Test
public void TestResolution() {
    ChineseCalendar cal = new ChineseCalendar();
    DateFormat fmt = DateFormat.getDateInstance(cal, DateFormat.DEFAULT);

    // May 22 2001 = y4638 m4 d30 doy119
    // May 23 2001 = y4638 m4* d1 doy120

    final int THE_YEAR = 4638;
    final int END = -1;

    int[] DATA = {
        // Format:
        // (field, value)+, END, exp.month, exp.isLeapMonth, exp.DOM
        // Note: exp.month is ONE-BASED

        // If we set DAY_OF_YEAR only, that should be used
        Calendar.DAY_OF_YEAR, 1,
        END,
        1,0,1, // Expect 1-1
        
        // If we set MONTH only, that should be used
        Calendar.IS_LEAP_MONTH, 1,
        Calendar.DAY_OF_MONTH, 1,
        Calendar.MONTH, 3,
        END,
        4,1,1, // Expect 4*-1
        
        // If we set the DOY last, that should take precedence
        Calendar.MONTH, 1, // Should ignore
        Calendar.IS_LEAP_MONTH, 1, // Should ignore
        Calendar.DAY_OF_MONTH, 1, // Should ignore
        Calendar.DAY_OF_YEAR, 121,
        END,
        4,1,2, // Expect 4*-2
        
        // I've disabled this test because it doesn't work this way,
        // not even with a GregorianCalendar!  MONTH alone isn't enough
        // to supersede DAY_OF_YEAR.  Some other month-related field is
        // also required. - Liu 11/28/00
        //! // If we set MONTH last, that should take precedence
        //! ChineseCalendar.IS_LEAP_MONTH, 1,
        //! Calendar.DAY_OF_MONTH, 1,
        //! Calendar.DAY_OF_YEAR, 5, // Should ignore
        //! Calendar.MONTH, 3,
        //! END,
        //! 4,1,1, // Expect 4*-1
        
        // If we set IS_LEAP_MONTH last, that should take precedence
        Calendar.MONTH, 3,
        Calendar.DAY_OF_MONTH, 1,
        Calendar.DAY_OF_YEAR, 5, // Should ignore
        Calendar.IS_LEAP_MONTH, 1,
        END,
        4,1,1, // Expect 4*-1
    };

    StringBuffer buf = new StringBuffer();
    for (int i=0; i<DATA.length; ) {
        cal.clear();
        cal.set(Calendar.EXTENDED_YEAR, THE_YEAR);
        buf.setLength(0);
        buf.append("EXTENDED_YEAR=" + THE_YEAR);
        while (DATA[i] != END) {
            cal.set(DATA[i++], DATA[i++]);
            buf.append(" " + fieldName(DATA[i-2]) + "=" + DATA[i-1]);
        }
        ++i; // Skip over END mark
        int expMonth = DATA[i++]-1;
        int expIsLeapMonth = DATA[i++];
        int expDOM = DATA[i++];
        int month = cal.get(Calendar.MONTH);
        int isLeapMonth = cal.get(Calendar.IS_LEAP_MONTH);
        int dom = cal.get(Calendar.DAY_OF_MONTH);
        if (expMonth == month && expIsLeapMonth == isLeapMonth &&
            dom == expDOM) {
            logln("OK: " + buf + " => " + fmt.format(cal.getTime()));
        } else {
            String s = fmt.format(cal.getTime());
            cal.clear();
            cal.set(Calendar.EXTENDED_YEAR, THE_YEAR);
            cal.set(Calendar.MONTH, expMonth);
            cal.set(Calendar.IS_LEAP_MONTH, expIsLeapMonth);
            cal.set(Calendar.DAY_OF_MONTH, expDOM);
            errln("Fail: " + buf + " => " + s +
                  "=" + (month+1) + "," + isLeapMonth + "," + dom +
                  ", expected " + fmt.format(cal.getTime()) +
                  "=" + (expMonth+1) + "," + expIsLeapMonth + "," + expDOM);
        }
    }
}
 
Example 12
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 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: DateIntervalFormatTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private void stress(String[] data, int data_length, Locale loc, String locName) {
    String[] skeleton = {
            "EEEEdMMMMy",
            "dMMMMy",
            "dMMMM",
            "MMMMy",
            "EEEEdMMMM",
            "EEEdMMMy",
            "dMMMy",
            "dMMM",
            "MMMy",
            "EEEdMMM",
            "EEEdMy",
            "dMy",
            "dM",
            "My",
            "EEEdM",
            "d",
            "EEEd",
            "y",
            "M",
            "MMM",
            "MMMM",
            "hm",
            "hmv",
            "hmz",
            "h",
            "hv",
            "hz",
            "EEddMMyyyy", // following could be normalized
            "EddMMy", 
            "hhmm",
            "hhmmzz",
            "hms",  // following could not be normalized
            "dMMMMMy",
            "EEEEEdM",
    };

    int i = 0;

    SimpleDateFormat ref = new SimpleDateFormat(data[i++], loc);

    while (i<data_length) {
        // 'f'
        String datestr = data[i++];
        String datestr_2 = data[i++];
        Date date;
        Date date_2;
        try {
            date = ref.parse(datestr);
            date_2 = ref.parse(datestr_2);
        } catch ( ParseException e ) {
            errln("parse exception" + e);
            continue;
        }
        DateInterval dtitv = new DateInterval(date.getTime(), date_2.getTime());

        for ( int skeletonIndex = 0; 
                skeletonIndex < skeleton.length; 
                ++skeletonIndex ) {
            String oneSkeleton = skeleton[skeletonIndex];
            // need special handle of "Thai" since the default calendar
            // of "Thai" is "Budd", not "Gregorian".
            DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance(oneSkeleton, loc);
            /*
            if ( locName.equals("th") ) {
                // reset calendar to be Gregorian
                GregorianCalendar gregCal = new GregorianCalendar(loc);
                DateFormat dformat = dtitvfmt.getDateFormat();
                DateFormat newOne = (DateFormat)dformat.clone();
                newOne.setCalendar(gregCal);
                dtitvfmt.setDateFormat(newOne);
            }
             */
            dtitvfmt.format(dtitv);
        }


        // test interval format by algorithm 
        for ( int style = DateFormat.FULL; style  < 4; ++style ) {
            SimpleDateFormat dtfmt = (SimpleDateFormat) DateFormat.getDateInstance(style, loc);
            FieldPosition pos = new FieldPosition(0);
            StringBuffer str = new StringBuffer("");
            Calendar fromCalendar = (Calendar) dtfmt.getCalendar().clone();
            Calendar toCalendar = (Calendar) dtfmt.getCalendar().clone();
            fromCalendar.setTimeInMillis(dtitv.getFromDate());
            toCalendar.setTimeInMillis(dtitv.getToDate());
            dtfmt.intervalFormatByAlgorithm(fromCalendar, toCalendar, str, pos);
        } 
    } 
}
 
Example 15
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);
    }

}