Author: saqibkhan

  • Locale specific Formatting Date

    Locale can be used to create locale specific formatting over a pattern in SimpleDateFormat class. See the following example of using locale specific SimpleDateFormat class.

    Example

    Open Compiler

    importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args)throwsParseException{Locale locale =newLocale("da","DK");String pattern ="EEEEE MMMMM yyyy";SimpleDateFormat simpleDateFormat =newSimpleDateFormat(pattern);Date date =newDate();System.out.println(date);System.out.println(simpleDateFormat.format(date));
    
    
      simpleDateFormat =newSimpleDateFormat(pattern,locale);System.out.println(simpleDateFormat.format(date));}}</code></pre>

    Output

    It will print the following result.

    Fri Jun 07 15:02:27 IST 2024
    Friday June 2024
    fredag juni 2024
    
  • SimpleDateFormat Class

    java.text.SimpleDateFormat class formats dates as per the given pattern. It is also used to parse dates from string where string contains date in mentioned format. See the following example of using SimpleDateFormat class.

    Example

    Open Compiler

    importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;publicclassI18NTester{publicstaticvoidmain(String[] args)throwsParseException{String pattern ="dd-MM-yyyy";SimpleDateFormat simpleDateFormat =newSimpleDateFormat(pattern);Date date =newDate();System.out.println(date);System.out.println(simpleDateFormat.format(date));String dateText ="29-11-2017";
    
    
      date = simpleDateFormat.parse(dateText);System.out.println(simpleDateFormat.format(date));}}</code></pre>

    Output

    It will print the following result.

    Fri Jun 07 15:19:00 IST 2024
    07-06-2024
    29-11-2017
    
  • Formatting Date and Time

    DateFormat class provides various formats to format the date and time together. DateFormat.getDateTimeInstance() method is to be used. See the example below.

    Example

    In following example we’ll show how to use different formats to format date and time.

    Open Compiler

    importjava.text.DateFormat;importjava.util.Date;publicclassI18NTester{publicstaticvoidmain(String[] args){DateFormat dateFormat =DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT);System.out.println(dateFormat.format(newDate()));
    
    
      dateFormat =DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);System.out.println(dateFormat.format(newDate()));
      dateFormat =DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);System.out.println(dateFormat.format(newDate()));
      dateFormat =DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);System.out.println(dateFormat.format(newDate()));
      dateFormat =DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);System.out.println(dateFormat.format(newDate()));}}</code></pre>

    Output

    It will print the following result.

    Jun 7, 2024, 2:34:35 PM
    6/7/24, 2:34 PM
    Jun 7, 2024, 2:34:35 PM
    June 7, 2024 at 2:34:35 PM IST
    Friday, June 7, 2024 at 2:34:35 PM India Standard Time
    
  • Formatting Time

    DateFormat class provides various formats to format the time. DateFormat.getTimeInstance() method is to be used. See the example below.

    Example

    In following example we’ll show how to use different formats to format time.

    Open Compiler

    importjava.text.DateFormat;importjava.util.Date;publicclassI18NTester{publicstaticvoidmain(String[] args){DateFormat dateFormat =DateFormat.getTimeInstance(DateFormat.DEFAULT);System.out.println(dateFormat.format(newDate()));
    
    
      dateFormat =DateFormat.getTimeInstance(DateFormat.SHORT);System.out.println(dateFormat.format(newDate()));
      dateFormat =DateFormat.getTimeInstance(DateFormat.MEDIUM);System.out.println(dateFormat.format(newDate()));
      dateFormat =DateFormat.getTimeInstance(DateFormat.LONG);System.out.println(dateFormat.format(newDate()));
      dateFormat =DateFormat.getTimeInstance(DateFormat.FULL);System.out.println(dateFormat.format(newDate()));}}</code></pre>

    Output

    It will print the following result.

    2:36:09 PM
    2:36 PM
    2:36:09 PM
    2:36:09 PM IST
    2:36:09 PM India Standard Time
    
  • Formatting Date

    DateFormat class provides various formats to format the date. Following is list of some of the formats.

    • DateFormat.DEFAULT
    • DateFormat.SHORT
    • DateFormat.MEDIUM
    • DateFormat.LONG
    • DateFormat.FULL

    Example

    In following example we’ll show how to use different formats.

    Open Compiler

    importjava.text.DateFormat;importjava.util.Date;publicclassI18NTester{publicstaticvoidmain(String[] args){DateFormat dateFormat =DateFormat.getDateInstance(DateFormat.DEFAULT);System.out.println(dateFormat.format(newDate()));
    
    
      dateFormat =DateFormat.getDateInstance(DateFormat.SHORT);System.out.println(dateFormat.format(newDate()));
      dateFormat =DateFormat.getDateInstance(DateFormat.MEDIUM);System.out.println(dateFormat.format(newDate()));
      dateFormat =DateFormat.getDateInstance(DateFormat.LONG);System.out.println(dateFormat.format(newDate()));
      dateFormat =DateFormat.getDateInstance(DateFormat.FULL);System.out.println(dateFormat.format(newDate()));}}</code></pre>

    Output

    It will print the following result.

    Jun 7, 2024
    6/7/24
    Jun 7, 2024
    June 7, 2024
    Friday, June 7, 2024
    
  • DateFormat Class

    java.text.DateFormat class formats dates as per the locale. As different coutries use different formats to display dates. This class is extremely useful in dealing with dates in Internationalization of application. Following example show how to create and use DateFormat Class.

    Example

    Open Compiler

    importjava.text.DateFormat;importjava.util.Date;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale locale =newLocale("da","DK");DateFormat dateFormat =DateFormat.getDateInstance();System.out.println(dateFormat.format(newDate()));
    
    
      dateFormat =DateFormat.getDateInstance(DateFormat.DEFAULT, locale);System.out.println(dateFormat.format(newDate()));}}</code></pre>

    Output

    It will print the following result.

    Nov 29, 2017
    29-11-2017
    
  • Grouping Digits

    Using setGroupingSize() method of DecimalFormat, default grouping of numbers can be changed. Following example is illustrating the same.

    Example

    Open Compiler

    importjava.text.DecimalFormat;publicclassI18NTester{publicstaticvoidmain(String[] args){double number =121223232473.4567;DecimalFormat decimalFormat =newDecimalFormat();System.out.println(number);System.out.println(decimalFormat.format(number));   
    
    
      decimalFormat.setGroupingSize(4);System.out.println(decimalFormat.format(number));}}</code></pre>

    Output

    It will print the following result.

    1.212232324734567E11
    121,223,232,473.457
    1212,2323,2473.457
    
  • DecimalFormatSymbols Class

    Using DecimalFormatSymbols class, the default separator symbols, grouping separator symbols etc. can be changed. Following example is illustrating the same.

    Example

    Open Compiler

    importjava.text.DecimalFormat;importjava.text.DecimalFormatSymbols;publicclassI18NTester{publicstaticvoidmain(String[] args){String pattern ="#,###.###";double number =126473.4567;DecimalFormat decimalFormat =newDecimalFormat(pattern);System.out.println(decimalFormat.format(number));DecimalFormatSymbols decimalFormatSymbols =newDecimalFormatSymbols();
    
      decimalFormatSymbols.setDecimalSeparator(';');
      decimalFormatSymbols.setGroupingSeparator(':');
      decimalFormat =newDecimalFormat(pattern, decimalFormatSymbols);System.out.println(decimalFormat.format(number));}}</code></pre>

    Output

    It will print the following result.

    126,473.457
    126:473;457
    
  • Locale Specific DecimalFormat

    By default, DecimalFormat object is using the JVM’s locale. We can change the default locale while creating the DecimalFormat object using NumberFormat class. In the example below, we’ll use same pattern for two different locale and you can spot the difference in the output.

    Example

    Open Compiler

    importjava.text.DecimalFormat;importjava.text.NumberFormat;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){String pattern ="###.##";double number =123.45;Locale enlocale  =newLocale("en","US");Locale dalocale  =newLocale("da","DK");DecimalFormat decimalFormat =(DecimalFormat)NumberFormat.getNumberInstance(enlocale);
    
      decimalFormat.applyPattern(pattern);System.out.println(decimalFormat.format(number));   
      decimalFormat =(DecimalFormat)NumberFormat.getNumberInstance(dalocale);
      decimalFormat.applyPattern(pattern);System.out.println(decimalFormat.format(number));}}</code></pre>

    Output

    It will print the following result.

    123.45
    123,45
    
  • Formatting Patterns

    Followings is the use of characters in formatting patterns.

    Sr.No.Class & Description
    10To display 0 if less digits are present.
    2#To display digit ommitting leading zeroes.
    3.Decimal separator.
    4,Grouping separator.
    5EMantissa and Exponent separator for exponential formats.
    6;Format separator.
    7Negative number prefix.
    8%Shows number as percentage after multiplying with 100.
    9?Shows number as mille after multiplying with 1000.
    10XTo mark character as number prefix/suffix.
    11To mark quote around special characters.

    Example

    In this example, we’re formatting numbers based on different patterns.

    Open Compiler

    importjava.text.DecimalFormat;publicclassI18NTester{publicstaticvoidmain(String[] args){String pattern ="###.###";double number =123456789.123;DecimalFormat numberFormat =newDecimalFormat(pattern);System.out.println(number);//pattern ###.###System.out.println(numberFormat.format(number));//pattern ###.#
    
      numberFormat.applyPattern("###.#");System.out.println(numberFormat.format(number));//pattern ###,###.##
      numberFormat.applyPattern("###,###.##");System.out.println(numberFormat.format(number));
      number =9.34;//pattern 000.###
      numberFormat.applyPattern("000.##");System.out.println(numberFormat.format(number));}}</code></pre>

    Output

    It will print the following result.

    1.23456789123E8
    123456789.123
    123456789.1
    123,456,789.12
    009.34