Java 8 Format - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Format class.

This code example shows how to use the following methods:setMaximumFractionDigits, setMultiplier, format, format
        } 
    } 
 
    public void testBigDecimalICUConsistency() { 
        DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
        df.setMaximumFractionDigits(2);
        df.setMultiplier(2);
        assertEquals(df.format(BigDecimal.valueOf(0.16)),
        df.format(BigDecimal.valueOf(0.16).doubleValue()));
        assertEquals(df.format(BigDecimal.valueOf(0.0293)),
        df.format(BigDecimal.valueOf(0.0293).doubleValue()));
        assertEquals(df.format(BigDecimal.valueOf(0.006)),
        df.format(BigDecimal.valueOf(0.006).doubleValue()));
        assertEquals(df.format(BigDecimal.valueOf(0.00283)),
        df.format(BigDecimal.valueOf(0.00283).doubleValue()));
        assertEquals(df.format(BigDecimal.valueOf(1.60)),
        df.format(BigDecimal.valueOf(1.60).doubleValue()));
        assertEquals(df.format(BigDecimal.valueOf(15)),
        df.format(BigDecimal.valueOf(15).doubleValue()));
        assertEquals(df.format(BigDecimal.valueOf(170)),

8
CodeRank
This code example shows how to use the following methods:parseObject, format, getMaximumFractionDigits, parse, isParseIntegerOnly
Locale arLocale = new Locale("ar", "AE");
        if (Support_Locale.isLocaleAvailable(arLocale)) {
            format = (DecimalFormat) NumberFormat.getIntegerInstance(arLocale);
            assertEquals("#,##0;#,##0-", format.toPattern());
            assertEquals("\u0666-", format.format(-6));
            assertEquals(new Long(-36), format.parse("36-"));
            assertEquals(new Long(-36), format.parseObject("36-"));
            assertEquals(0, format.getMaximumFractionDigits());
            assertTrue(format.isParseIntegerOnly());
        } 
    } 
 
    public void test_setMaximumIntegerDigits() { 
        NumberFormat format = NumberFormat.getInstance();
        format.setMaximumIntegerDigits(2);
        assertEquals("Wrong result: case 1", "23", format.format(123));
 
        format.setMaximumIntegerDigits(Integer.MIN_VALUE);
        assertEquals("Wrong result: case 2", "0", format.format(123));
7
CodeRank
This code example shows how to use the following methods:format
        } 
 
        if (args == null) {
            return formatString;
        } else { 
            MessageFormat formatter = new MessageFormat(formatString);
            return formatter.format(args);
        } 
    } 
 
    private static String getExceptionMessage(RhinoException ex)
    { 
        String msg;
        if (ex instanceof JavaScriptException) {
            msg = getMessage("msg.uncaughtJSException", ex.details());
        } else if (ex instanceof EcmaError) {
            msg = getMessage("msg.uncaughtEcmaError", ex.details());
        } else if (ex instanceof EvaluatorException) {
            msg = ex.details();
        } else { 
7
CodeRank
This code example shows how to use the following methods:format, parseObject
    Assert.assertEquals("31st", formatter.format(new NSTimestamp(cal.getTime())));
  } 
  public void testFormatThenParse() throws ParseException { 
    Format dateFormatter = new ERXOrdinalDateFormatter("d'th 'MMM' 'yyyy' 'HH':'mm':'ss");
    NSTimestamp timestamp = new NSTimestamp(new GregorianCalendar(2000, 0, 1).getTime());
    String formatted = dateFormatter.format(timestamp);
    NSTimestamp parsed = (NSTimestamp) dateFormatter.parseObject(formatted);
    String formatted2 = dateFormatter.format(parsed);
    assertEquals(formatted, formatted2);
  } 
} 
7
CodeRank
This code example shows how to use the following methods:parseObject, format, getMaximumFractionDigits, parse, isParseIntegerOnly
Locale arLocale = new Locale("ar", "AE");
        if (Support_Locale.isLocaleAvailable(arLocale)) {
            format = (DecimalFormat) NumberFormat.getIntegerInstance(arLocale);
            assertEquals("#,##0;#,##0-", format.toPattern());
            assertEquals("\u0666-", format.format(-6));
            assertEquals(new Long(-36), format.parse("36-"));
            assertEquals(new Long(-36), format.parseObject("36-"));
            assertEquals(0, format.getMaximumFractionDigits());
            assertTrue(format.isParseIntegerOnly());
        } 
    } 
 
    public void test_setMaximumIntegerDigits() { 
        NumberFormat format = NumberFormat.getInstance();
        format.setMaximumIntegerDigits(2);
        assertEquals("Wrong result: case 1", "23", format.format(123));
 
        format.setMaximumIntegerDigits(Integer.MIN_VALUE);
        assertEquals("Wrong result: case 2", "0", format.format(123));
7
CodeRank
This code example shows how to use the following methods:format, applyPattern, setLocale
    } 
 
    public static String getLangueMessageFormatter ( String key,  Object[] messageArguments ) {    	
    	String str = Utilities.getLangueMessage( key ) ; 
    	 
        MessageFormat formatter = new MessageFormat("");      
        formatter.setLocale(Utilities.currentLocale);
 
        formatter.applyPattern(str);
        return formatter.format(messageArguments);
    } 
     
    public static String retourLigne()
    { 
        return "\r\n"; 
    } 
} 
7
CodeRank
This code example shows how to use the following methods:toPattern, parseObject, format, getMaximumFractionDigits, parse
 
        // try with a locale that has a different integer pattern 
        format = (DecimalFormat) NumberFormat.getIntegerInstance(arLocale);
        assertEquals(
                "Test7: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).toPattern() returned wrong pattern", 
                "#,##0;#,##0-", format.toPattern());
        assertEquals(
                "Test8: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).format(-35.76) returned wrong value", 
                "\u0666-", format.format(-6));
        assertEquals(
                "Test9: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).parse(\"-36-\") returned wrong number", 
                new Long(-36), format.parse("36-"));
        assertEquals(
                "Test10: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).parseObject(\"36-\") returned wrong number", 
                new Long(-36), format.parseObject("36-"));
 
        assertEquals(
                "Test11: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).getMaximumFractionDigits() returned wrong value", 
                0, format.getMaximumFractionDigits());
        assertTrue(

7
CodeRank
This code example shows how to use the following methods:format
        Locale.setDefault(Locale.CANADA);
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
        String pat = "text here {0,date,yyyyyyyyy} and here";
        Calendar c = Calendar.getInstance();
        String etalon = "text here 00000" + c.get(Calendar.YEAR) + " and here";
        MessageFormat obj = new MessageFormat(pat);
        assertEquals(etalon, obj.format(new Object[] { new Date() }));
    } 
 
    public void test_parseLjava_lang_StringLjava_text_ParsePosition() { 
        ParsePosition pos = new ParsePosition(2);
 
        MessageFormat mf = new MessageFormat("{0}; {0}; {0}");
        String parse = "a; b; c";
        try { 
            mf.parse(parse, null);
            fail("NullPointerException was not thrown.");
        } catch(NullPointerException npe) {
            //expected 
        } 
7
CodeRank
This code example shows how to use the following methods:applyPattern, setGroupingUsed, setMaximumFractionDigits, setMinimumFractionDigits, format
This code example shows how to use the following methods:format, applyPattern, setLocale

Related Java 8 Format Questions & Answers:

See Code Examples for Java 8 Format Methods: