Package com.ibm.icu.text

Examples of com.ibm.icu.text.DurationFormat


   
    /**
     * Basic test
     */
    public void TestBasics() {
        DurationFormat df;
        String expect;
        String formatted;
       
        df = DurationFormat.getInstance(new ULocale("it"));
        formatted = df.formatDurationFromNow(4096);
        expect = "fra quattro secondi";
        if(!expect.equals(formatted)) {
            errln("Expected " + expect + " but got " + formatted);
        } else {
            logln("format duration -> " + formatted);
        }
       
        formatted = df.formatDurationFromNowTo(new Date(0));
        Calendar cal = Calendar.getInstance();
        int years = cal.get(Calendar.YEAR) - 1970; // year of Date(0)
        expect = "fra " + years + " anni";
        if(!expect.equals(formatted)) {
            errln("Expected " + expect + " but got " + formatted);
        } else {
            logln("format date  -> " + formatted);
        }
       
        formatted = df.formatDurationFrom(1000*3600*24, new Date(0).getTime());
        expect = "fra un giorno";
        if(!expect.equals(formatted)) {
            errln("Expected " + expect + " but got " + formatted);
        } else {
            logln("format date from -> " + formatted);
        }

        formatted = df.format(new Long(1000*3600*24*2));
        expect = "fra due giorni";
        if(!expect.equals(formatted)) {
            errln("Expected " + expect + " but got " + formatted);
        } else {
            logln("format long obj -> " + formatted);
View Full Code Here


            errln("Error instantiating XML DatatypeFactory.");
            e.printStackTrace();
        }
       
        Duration d;
        DurationFormat df;
        String out;
        String expected;
        String expected2;
       
        // test 1
        d = factory.newDuration("PT2H46M40S");
        df = DurationFormat.getInstance(new ULocale("en"));
        expected = "2 hours, 46 minutes, and 40 seconds";
        out = df.format(d);
        if(out.equals(expected)) {
            logln("out=expected: " + expected + " from " + d);
        } else {
            errln("FAIL: got " + out + " wanted " + expected + " from " + d);
        }
       
        // test 2
        d = factory.newDuration(10000);
        df = DurationFormat.getInstance(new ULocale("en"));
        expected = "10 seconds";
        out = df.format(d);
        if(out.equals(expected)) {
            logln("out=expected: " + expected + " from " + d);
        } else {
            errln("FAIL: got " + out + " wanted " + expected + " from " + d);
        }
        // test 3
        d = factory.newDuration("P0DT0H0M10.0S");
        df = DurationFormat.getInstance(new ULocale("en"));
        expected = "10 seconds";
        out = df.format(d);
        if(out.equals(expected)) {
            logln("out=expected: " + expected + " from " + d);
        } else {
            errln("FAIL: got " + out + " wanted " + expected + " from " + d);
        }
        // test 4
        d = factory.newDuration(86400000);
        df = DurationFormat.getInstance(new ULocale("en"));
        expected = "1 day, 0 hours, 0 minutes, and 0 seconds";
        expected2 = "1 day and 0 seconds"; // This is the expected result for Windows with IBM JRE6
        out = df.format(d);
        if(out.equals(expected)) {
            logln("out=expected: " + expected + " from " + d);
        } else {
            if(out.equals(expected2)){
                logln("WARNING: got " + out + " wanted " + expected + " from " + d);
View Full Code Here

                d = factory.newDuration(Long.parseLong(from.substring(1)));
            } else {
                d = factory.newDuration(from);
            }
           
            DurationFormat df = DurationFormat.getInstance(locale);
            String output = df.format(d);
           
            if(output.equals(to)) {
                logln("SUCCESS: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +" to " + to + "= " + output);
            } else {
                logln("FAIL: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +": expected " + to + " got " + output);
View Full Code Here

//#endif


    public void TestBadObjectError() {
        Runtime r = Runtime.getRuntime();
        DurationFormat df = DurationFormat.getInstance(new ULocale("en"));
        String output = null;
        try {
            output = df.format(r);
            errln("FAIL: did NOT get IllegalArgumentException! Should have. Formatted Runtime as " + output + " ???");
        } catch (IllegalArgumentException iae) {
            logln("PASS: expected: Caught iae: " + iae.toString() );
        }
        // try a second time, because it is a different code path for java < 1.5
        try {
            output = df.format(r);
            errln("FAIL: [#2] did NOT get IllegalArgumentException! Should have. Formatted Runtime as " + output + " ???");
        } catch (IllegalArgumentException iae) {
            logln("PASS: [#2] expected: Caught iae: " + iae.toString() );
        }
    }
View Full Code Here

        }
    }

    public void TestBadLocaleError() {
        try {
            DurationFormat df = DurationFormat.getInstance(new ULocale("und"));
            df.format(new Date());
            logln("Should have thrown err.");
            errln("failed, should have thrown err.");
        } catch(MissingResourceException mre) {
            logln("PASS: caught missing resource exception on locale 'und'");
            logln(mre.toString());
View Full Code Here

            logln(mre.toString());
        }
    }

    public void TestResourceWithCalendar() {
        DurationFormat df = DurationFormat.getInstance(new ULocale("th@calendar=buddhist"));
        // should pass, but return a default formatter for th.
        if (df == null) {
            errln("FAIL: null DurationFormat returned.");
        }
    }
View Full Code Here

        // original test case
        // if we don't support milliseconds, format times less than 1 second as
        // 'less than 1 second'
        {
            ULocale ul = new ULocale("th");
            DurationFormat df = DurationFormat.getInstance(ul);
            String result = df.formatDurationFromNow(500);
            assertEquals(
                "original test case",
                "\u0E44\u0E21\u0E48\u0E16\u0E36\u0E07\u0E2D\u0E35\u0E01 1 \u0E27\u0E34\u0E19\u0E32\u0E17\u0E35",
                result);
        }
       
        // same issue, but using English and the internal APIs
        {
            PeriodFormatterService pfs = BasicPeriodFormatterService.getInstance();
            PeriodBuilder pb = pfs.newPeriodBuilderFactory()
                .setAllowMilliseconds(false)
                .getSingleUnitBuilder();
            DurationFormatter df = pfs.newDurationFormatterFactory()
                .setPeriodBuilder(pb)
                .getFormatter();
            String result = df.formatDurationFromNow(500);
            assertEquals(
                "english test case",
                "less than 1 second from now",
                result);
          
        }
       
        // if the limit is set on milliseconds, and they are not supported,
        // use an effective limit based on seconds
        {
            PeriodFormatterService pfs = BasicPeriodFormatterService.getInstance();
            PeriodBuilder pb = pfs.newPeriodBuilderFactory()
                .setMinLimit(2500)
                .setAllowMilliseconds(false)
                .getSingleUnitBuilder();
            DurationFormatter df = pfs.newDurationFormatterFactory()
                .setPeriodBuilder(pb)
                .getFormatter();
            String result = df.formatDurationFromNow(500);
            assertEquals(
                "limit test case",
                "less than 2 seconds from now",
                result);
        }
View Full Code Here

    public static class BasicDurationFormatHandler implements SerializableTest.Handler
    {
        public Object[] getTestObjects()
        {
            DurationFormat formats[] = {
                    DurationFormat.getInstance(new ULocale("en"))
                  
            };
           
            return formats;
View Full Code Here

TOP

Related Classes of com.ibm.icu.text.DurationFormat

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.