Category: 10. Java Internationalization

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRcUEH_0xlWFYgGpeNRUmn5taquEq7H2pEiaw&s

  • Unicode Conversion from/to Reader/Writer

    Reader and Writer classes are character oriented stream classes. These can be used to read and convert Unicode characters.

    Conversion

    Following example will showcase conversion of a Unicode String to UTF8 byte[] and UTF8 byte[] to Unicode byte[] using Reader and Writer classes.

    Example

    Open Compiler

    importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.io.Reader;importjava.io.Writer;importjava.nio.charset.Charset;importjava.text.ParseException;publicclassI18NTester{publicstaticvoidmain(String[] args)throwsParseException,IOException{String input ="This is a sample text";InputStream inputStream =newByteArrayInputStream(input.getBytes());//get the UTF-8 dataReader reader =newInputStreamReader(inputStream,Charset.forName("UTF-8"));//convert UTF-8 to Unicodeint data = reader.read();while(data !=-1){char theChar =(char) data;System.out.print(theChar);
    
         data = reader.read();}
      reader.close();System.out.println();//Convert Unicode to UTF-8 BytesByteArrayOutputStream outputStream =newByteArrayOutputStream();Writer writer =newOutputStreamWriter(outputStream,Charset.forName("UTF-8"));
      writer.write(input);
      writer.close();String out =newString(outputStream.toByteArray());System.out.println(out);}}</code></pre>

    Output

    It will print the following result.

    This is a sample text
    This is a sample text
    
  • Unicode Conversion from/to String

    In java, text is internally stored in Unicode format. If input/output is in differnt format then conversion is required.

    Conversion

    Following example will showcase conversion of a Unicode String to UTF8 byte[] and UTF8 byte[] to Unicode byte[].

    Example

    Open Compiler

    importjava.io.UnsupportedEncodingException;importjava.nio.charset.Charset;importjava.text.ParseException;publicclassI18NTester{publicstaticvoidmain(String[] args)throwsParseException,UnsupportedEncodingException{String unicodeString ="\u00C6\u00D8\u00C5";//convert Unicode to UTF8 formatbyte[] utf8Bytes = unicodeString.getBytes(Charset.forName("UTF-8"));printBytes(utf8Bytes,"UTF 8 Bytes");//convert UTF8 format to UnicodeString converted =newString(utf8Bytes,"UTF8");byte[] unicodeBytes = converted.getBytes();printBytes(unicodeBytes,"Unicode Bytes");}publicstaticvoidprintBytes(byte[] array,String name){for(int k =0; k < array.length; k++){System.out.println(name +"["+ k +"] = "+ array[k]);}}}

    Output

    It will print the following result.

    UTF 8 Bytes[0] = -61
    UTF 8 Bytes[1] = -122
    UTF 8 Bytes[2] = -61
    UTF 8 Bytes[3] = -104
    UTF 8 Bytes[4] = -61
    UTF 8 Bytes[5] = -123
    Unicode Bytes[0] = -58
    Unicode Bytes[1] = -40
    Unicode Bytes[2] = -59
    
  • UTC

    UTC stands for Co-ordinated Universal Time. It is time standard and is commonly used across the world. All timezones are computed comparatively with UTC as offset. For example, time in Copenhagen, Denmark is UTC + 1 means UTC time plus one hour. It is independent of Day light savings and should be used to store date and time in databases.

    Conversion of Time Zones

    Following example will showcase conversion of various timezones. We’ll print hour of the day and time in milliseconds. First will vary and second will remain same.

    Example

    Open Compiler

    importjava.text.ParseException;importjava.util.Calendar;importjava.util.GregorianCalendar;importjava.util.TimeZone;publicclassI18NTester{publicstaticvoidmain(String[] args)throwsParseException{Calendar date =newGregorianCalendar();
    
    
      date.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
      date.set(Calendar.HOUR_OF_DAY,12);System.out.println("UTC: "+ date.get(Calendar.HOUR_OF_DAY));System.out.println("UTC: "+ date.getTimeInMillis());
      date.setTimeZone(TimeZone.getTimeZone("Europe/Copenhagen"));System.out.println("CPH: "+ date.get(Calendar.HOUR_OF_DAY));System.out.println("CPH: "+ date.getTimeInMillis());
      date.setTimeZone(TimeZone.getTimeZone("America/New_York"));System.out.println("NYC: "+ date.get(Calendar.HOUR_OF_DAY));System.out.println("NYC: "+ date.getTimeInMillis());}}</code></pre>

    Output

    It will print the following result.

    UTC: 12
    UTC: 1511956997540
    CPH: 13
    CPH: 1511956997540
    NYC: 7
    NYC: 1511956997540
    

    Available Time Zones

    Following example will showcase the timezones available with the system.

    Example

    Open Compiler

    importjava.text.ParseException;importjava.util.TimeZone;publicclassI18NTester{publicstaticvoidmain(String[] args)throwsParseException{String[] availableIDs =TimeZone.getAvailableIDs();for(String id : availableIDs){System.out.println("Timezone = "+ id);}}}

    Output

    It will print the following result.

    Timezone = Africa/Abidjan
    Timezone = Africa/Accra
    ...
    Timezone = VST
    
  • Date Format Patterns

    Followings is the use of characters in date formatting patterns.

    Sr.No.Class & Description
    1GTo display Era.
    2yTo display Year. Valid values yy, yyyy.
    3MTo display Month. Valid values MM, MMM or MMMMM.
    4dTo display day of month. Valid values d, dd.
    5hTo display hour of day (1-12 AM/PM). Valid value hh.
    6HTo display hour of day (0-23). Valid value HH.
    7mTo display minute of hour (0-59). Valid value mm.
    8sTo display second of minute (0-59). Valid value ss.
    9STo display milliseconds of minute (0-999). Valid value SSS.
    10ETo display Day in week (e.g Monday, Tuesday etc.)
    11DTo display Day in year (1-366).
    12FTo display Day of week in month (e.g. 1st Thursday of December).
    13wTo display Week in year (1-53).
    14WTo display Week in month (0-5)
    15aTo display AM / PM
    16kTo display Hour in day (1-24).
    17KTo display Hour in day, AM / PM (0-11).
    18zTo display Time Zone.

    Example

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

    Open Compiler

    importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;publicclassI18NTester{publicstaticvoidmain(String[] args)throwsParseException{String pattern ="dd-MM-yy";SimpleDateFormat simpleDateFormat =newSimpleDateFormat(pattern);Date date =newDate();System.out.println(simpleDateFormat.format(date));
    
    
      pattern ="MM-dd-yyyy";
      simpleDateFormat =newSimpleDateFormat(pattern);System.out.println(simpleDateFormat.format(date));
      pattern ="yyyy-MM-dd HH:mm:ss";
      simpleDateFormat =newSimpleDateFormat(pattern);System.out.println(simpleDateFormat.format(date));
      pattern ="EEEEE MMMMM yyyy HH:mm:ss.SSSZ";
      simpleDateFormat =newSimpleDateFormat(pattern);System.out.println(simpleDateFormat.format(date));}}</code></pre>

    Output

    It will print the following result.

    07-06-24
    06-07-2024
    2024-06-07 16:04:40
    Friday June 2024 16:04:40.866+0530
    
  • DateFormatSymbols 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 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