Java I18N – Formatting Dates

Java Internationalization – Formatting Date ”; Previous Next 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. import java.text.DateFormat; import java.util.Date; public class I18NTester { public static void main(String[] args) { DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT); System.out.println(dateFormat.format(new Date())); dateFormat = DateFormat.getDateInstance(DateFormat.SHORT); System.out.println(dateFormat.format(new Date())); dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM); System.out.println(dateFormat.format(new Date())); dateFormat = DateFormat.getDateInstance(DateFormat.LONG); System.out.println(dateFormat.format(new Date())); dateFormat = DateFormat.getDateInstance(DateFormat.FULL); System.out.println(dateFormat.format(new Date())); } } Output It will print the following result. Jun 7, 2024 6/7/24 Jun 7, 2024 June 7, 2024 Friday, June 7, 2024 Print Page Previous Next Advertisements ”;

Java I18N – Formatting Time

Java Internationalization – Formatting Time ”; Previous Next 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. import java.text.DateFormat; import java.util.Date; public class I18NTester { public static void main(String[] args) { DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.DEFAULT); System.out.println(dateFormat.format(new Date())); dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT); System.out.println(dateFormat.format(new Date())); dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM); System.out.println(dateFormat.format(new Date())); dateFormat = DateFormat.getTimeInstance(DateFormat.LONG); System.out.println(dateFormat.format(new Date())); dateFormat = DateFormat.getTimeInstance(DateFormat.FULL); System.out.println(dateFormat.format(new Date())); } } 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 Print Page Previous Next Advertisements ”;

Java I18N – DecimalFormat Class

Java Internationalization – DecimalFormat Class ”; Previous Next The java.text.DecimalFormat class is used for formatting numbers as per customized format and as per locale. Example – Format Numbers In this example, we”re formatting numbers based on a given pattern. import java.text.DecimalFormat; public class I18NTester { public static void main(String[] args) { String pattern = “####,####.##”; double number = 123456789.123; DecimalFormat numberFormat = new DecimalFormat(pattern); System.out.println(number); System.out.println(numberFormat.format(number)); } } Output It will print the following result. 1.23456789123E8 1,2345,6789.12 Print Page Previous Next Advertisements ”;

Java I18N – NumberFormat Class

Java Internationalization – NumberFormat Class ”; Previous Next The java.text.NumberFormat class is used for formatting numbers and currencies as per a specific Locale. Number formats varies from country to country. For example, In Denmark fractions of a number are separated from the integer part using a comma whereas in England they use a dot as separator. Example – Format Numbers In this example, we”re formatting numbers based on US locale and Danish Locale. import java.text.NumberFormat; import java.util.Locale; public class I18NTester { public static void main(String[] args) { Locale enLocale = new Locale(“en”, “US”); Locale daLocale = new Locale(“da”, “DK”); NumberFormat numberFormat = NumberFormat.getInstance(daLocale); System.out.println(numberFormat.format(100.76)); numberFormat = NumberFormat.getInstance(enLocale); System.out.println(numberFormat.format(100.76)); } } Output It will print the following result. 100,76 100.76 Print Page Previous Next Advertisements ”;

Java I18N – SimpleDateFormat Class

Java Internationalization – SimpleDateFormat Class ”; Previous Next 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 import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class I18NTester { public static void main(String[] args) throws ParseException { String pattern = “dd-MM-yyyy”; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); Date date = new Date(); 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)); } } Output It will print the following result. Fri Jun 07 15:19:00 IST 2024 07-06-2024 29-11-2017 Print Page Previous Next Advertisements ”;

Java I18N – Set Min/Max Precision

Java Internationalization – Set Min/Max Precision ”; Previous Next Example In this example, we”re setting min and max digits for both integer as well as fractional part of a number. import java.text.NumberFormat; import java.util.Locale; public class I18NTester { public static void main(String[] args) { Locale enLocale = new Locale(“en”, “US”); NumberFormat numberFormat = NumberFormat.getInstance(enLocale); numberFormat.setMinimumIntegerDigits(2); numberFormat.setMaximumIntegerDigits(3); numberFormat.setMinimumFractionDigits(2); numberFormat.setMaximumFractionDigits(3); System.out.println(numberFormat.format(12234.763443)); } } Output It will print the following result. 234.763 Print Page Previous Next Advertisements ”;

Java I18N – Environment Setup

Java Internationalization – Environment Setup ”; Previous Next In this chapter, we will discuss on the different aspects of setting up a congenial environment for Java. Local Environment Setup If you are still willing to set up your environment for Java programming language, then this section guides you on how to download and set up Java on your machine. Following are the steps to set up the environment. Java SE is freely available from the link Download Java. You can download a version based on your operating system. Follow the instructions to download Java and run the .exe to install Java on your machine. Once you installed Java on your machine, you will need to set environment variables to point to correct installation directories − Setting Up the Path for Windows Assuming you have installed Java in c:Program Filesjavajdk directory − Right-click on ”My Computer” and select ”Properties”. Click the ”Environment variables” button under the ”Advanced” tab. Now, alter the ”Path” variable so that it also contains the path to the Java executable. Example, if the path is currently set to ”C:WINDOWSSYSTEM32”, then change your path to read ”C:WINDOWSSYSTEM32;c:Program Filesjavajdkbin”. Setting Up the Path for Linux, UNIX, Solaris, FreeBSD Environment variable PATH should be set to point to where the Java binaries have been installed. Refer to your shell documentation, if you have trouble doing this. Example, if you use bash as your shell, then you would add the following line to the end of your ”.bashrc: export PATH = /path/to/java:$PATH” Popular Java Editors To write your Java programs, you will need a text editor. There are even more sophisticated IDEs available in the market. But for now, you can consider one of the following − Notepad − On Windows machine, you can use any simple text editor like Notepad (Recommended for this tutorial), TextPad. Netbeans − A Java IDE that is open-source and free which can be downloaded from https://www.netbeans.org/index.html. Eclipse − A Java IDE developed by the eclipse open-source community and can be downloaded from https://www.eclipse.org/. What is Next? Next chapter will teach you how to write and run your first Java program and some of the important basic syntaxes in Java needed for developing applications. Print Page Previous Next Advertisements ”;

Java I18N – Format Currencies

Java Internationalization – Format Currencies ”; Previous Next In this example, we”re formatting currencies based on US locale and Danish Locale. Example import java.text.NumberFormat; import java.util.Locale; public class I18NTester { public static void main(String[] args) { Locale enLocale = new Locale(“en”, “US”); Locale daLocale = new Locale(“da”, “DK”); NumberFormat numberFormat = NumberFormat.getCurrencyInstance(daLocale); System.out.println(numberFormat.format(100.76)); numberFormat = NumberFormat.getCurrencyInstance(enLocale); System.out.println(numberFormat.format(100.76)); } } Output It will print the following result. kr 100,76 $100.76 Print Page Previous Next Advertisements ”;

Java I18N – Locale Details

Java Internationalization – Locale Details ”; Previous Next Example In this example, we”ll get default locale and print its details. Then create a locale for “fr” and print its details. import java.util.Locale; public class I18NTester { public static void main(String[] args) { Locale locale =Locale.getDefault(); System.out.println(“Default Locale Properties:n”); System.out.println(locale.getDisplayCountry()); System.out.println(locale.getDisplayLanguage()); System.out.println(locale.getDisplayName()); System.out.println(locale.getISO3Country()); System.out.println(locale.getISO3Language()); System.out.println(locale.getLanguage()); System.out.println(locale.getCountry()); Locale frenchLocale = new Locale(“fr”,”fr”); System.out.println(“nfr Locale Properties:n”); System.out.println(frenchLocale.getDisplayCountry()); System.out.println(frenchLocale.getDisplayLanguage()); System.out.println(frenchLocale.getDisplayName()); System.out.println(frenchLocale.getISO3Country()); System.out.println(frenchLocale.getISO3Language()); System.out.println(frenchLocale.getLanguage()); System.out.println(frenchLocale.getCountry()); } } Output It will print the following result. Default Locale Properties: United States English English (United States) USA eng en US fr Locale Properties: France French French (France) FRA fra fr FR Print Page Previous Next Advertisements ”;

Java I18N – Overview

Java Internationalization – Overview ”; Previous Next Internationalization Internationalization or I18N refers to the capability of an Application to be able to serve users in multiple and different languages. Java has in-built support for Internationalization. Java also provides formatting of numbers, currencies and adjustment of date and time accordingly. Java Internationalization helps to make a java application handle different languages, number formats, currencies, region specific time formatting. Localization Localization or L10N is the adaptability of an application that is how an application adapts itself with a specific language, number formats, date and time settings etc. A java application should be internationalized in order to be able to localize itself. Culturally Dependent Information Following information items often varies with different time zones or cultures. Messages Date Time Number Currency Measurements Phone Numbers Postal Addresses GUI labels Internationalization Classes Java has a set of built-in classes which help in internationalization of an application. These classes are following − Sr.No. Class & Description 1 Locale Represents a language along with country/region. 2 ResourceBundle Contains localized text or objects. 3 NumberFormat Use to format numbers/currencies as per the locale. 4 DecimalFormat Use to format numbers as per customized format and as per locale. 5 DateFormat Use to format dates as per locale. 6 SimpleDateFormat Use to format dates as per customized format and as per locale. Print Page Previous Next Advertisements ”;