Javaweb internationalization
DateFormat: a tool class that formats dates, which itself is an abstract class;
NumberFormat: A character class that formats numbers to numeric strings, or currency strings;
MessageFormat: You can format the pattern string, pattern string: String with placeholders: "Date: {0}, Salary: {1}", you can format the pattern string through the format method.
ResourceBundle: Resource package class, the corresponding resource file needs to be included under the classpath (src): baseName.properties. where baseName is the base name;
The file name is: test_zh_CN.properties, the file is: date=/u65E5/u671F, salary=/u5DE5/u8D44
The file name is: test_en_US.properties, the file is: date=date, salary=salary
import java.text.DateFormat;import java.text.MessageFormat;import java.text.NumberFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;import java.util.ResourceBundle;import org.junit.Test;public class I18nTest { /** * ResourceBundle: Resource Package Class. * * 1. There is a corresponding resource file under the classpath: baseName.properties. where baseName is the base name. * 2. You can use the base name_language code_country code.properties to add resource files from different countries or regions. i18n_zh_CN.properties * 3. It is required that the keys of all resource files with the same base name must be exactly the same. * 4. You can use the native2ascii command to get the asc code of a Chinese character pair. Eclipse has built-in tools* 5. You can call the getBundle (base name, Locale instance) of ResourceBundle to get the ResourceBundle object* 6. You can call the getString(key) of ResourceBundle to get the value string of the resource file. * 7. Combined with DateFormat, NumberFormat, MessageFormat can achieve internationalization. * */ @Test public void testResourceBundle(){ Locale locale = Locale.CHINA; ResourceBundle resourceBundle = ResourceBundle.getBundle("test", locale); System.out.println(resourceBundle.getString("date")); System.out.println(resourceBundle.getString("salary")); String dateLabel = resourceBundle.getString("date"); String salLabel = resourceBundle.getString("salary"); String str = "{0}:{1}, {2}:{3}"; Date date = new Date(); double sal = 12345.12; DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); String dateStr = dateFormat.format(date); NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale); String salStr = numberFormat.format(sal); String result = MessageFormat.format(str, dateLabel, dateStr, salLabel, salStr); System.out.println(result); } /** * MessageFormat: You can format the pattern string* Pattern string: String with placeholders: "Date: {0}, Salary: {1}" * The pattern string can be formatted through the format method*/ @Test public void testMessageFormat(){ String str = "Date: {0}, Salary: {1}"; Locale locale = Locale.CHINA; Date date = new Date(); double sal = 12345.12; DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); String dateStr = dateFormat.format(date); NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale); String salStr = numberFormat.format(sal); String result = MessageFormat.format(str, dateStr, salStr); System.out.println(result); } /** * NumberFormat: Tool class for formatting numbers to numeric strings, or currency strings* 1. Get NumberFormat object through factory method* NumberFormat.getNumberInstance(locale); // Strings formatted as numbers* NumberFormat.getCurrencyInstance(locale); // Strings formatted as currency* * 2. Format method* 3. Parses a string into a Number type through parse method. */ @Test public void testNumberFormat() throws ParseException{ double d = 123456789.123d; Locale locale = Locale.FRANCE; // NumberFormat numberFormat = NumberFormat.getNumberInstance(locale); String str = numberFormat.format(d); System.out.println(str); NumberFormat numberFormat2 = NumberFormat.getCurrencyInstance(locale); str = numberFormat2.format(d); System.out.println(str); str = "123 456 789,123"; d = (Double) numberFormat.parse(str); System.out.println(d); str = "123 456 789,12 ; d = (Double) numberFormat2.parse(str); System.out.println(d); } /* * 7. If there is a string, how to parse it into a Date object? * I. Create a DateFormat object first: Create a subclass of DateFormat SimpleDateFormat * SimpleDateFormat(String pattern). * Where pattern is the format of date and time, for example: yyyy-MM-dd hh:mm:ss * II. Call DateFormat's parse method to parse strings to Date object. */ @Test public void testDateFormat2() throws ParseException{ String str = "1990-12-12 12:12:12"; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = dateFormat.parse(str); System.out.println(date); } /** * DateFormat: A tool class for formatting dates. * DateFormat itself is an abstract class. * * 1. If you only want to convert a Date object into a string through DateFormat, you can get the DateFormat object through DateFormat factory method* 2. You can get the DateFormat object that only formats Date: getDateInstance(int style, Locale aLocale) * 3. You can get the DateFormat object that only formats Time: getTimeInstance(int style, Locale aLocale) * 4. You can get the DateFormat object that formats both Date and Time: * getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale) * 5. The value of style can be: Constants of DateFormat: SHORT, MEDIUM, LONG, FULL. Locale is the Locale object representing the country and region* 6. Format a Date object to a string through the format method of DateFormat. */ @Test public void testDateFormat(){ Locale locale = Locale.US; Date date = new Date(); System.out.println(date); //Get DateFormat object DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM, locale); String str = dateFormat.format(date); System.out.println(str); } /** * Locale: A class that represents a country or region in Java. Many constants are provided in the JDK. * You can also create it through Locale(languageCode, countryCode) * In WEB applications, you can get it through the request.getLocale() method. */ @Test public void testLocale(){ Locale locale = Locale.CHINA; System.out.println(locale.getDisplayCountry()); System.out.println(locale.getLanguage()); locale = new Locale("en", "US"); System.out.println(locale.getDisplayCountry()); System.out.println(locale.getLanguage()); } }The above is a compilation of the internationalization of Java web. We will continue to add relevant information in the future. Thank you for your support to this site!