Java examples on java.text
- How do I convert String to Date object?
- How do I format a number?
- How do I convert Date to String?
- How do I format a date into dd/mm/yyyy?
- How do I format a date-time value?
- How do I format date using a locale based format?
- How do I format a number with leading zeros?
- How do I get a list of month names?
- How do I get a list of weekday names?
- How do I sort strings using Collator class?
- How do I format a message that contains date information?
- How do I format a number for a locale?
- How do I iterate each characters of a string?
- How do I format a message that contains number information?
- How do I parse a number for a locale?
- How do I reverse a string using CharacterIterator?
- How do I iterate a subset of a string?
- How do I change the currency symbol?
- How do I format a message that contains time information?
- How do I breaks a text or sentence into words?
- How do I change the date format symbols for a specified locale?
- How do I format a number as currency string?
- How do I breaks a paragraph into sentences?
- How do I change DecimalFormat pattern?
- How do I get default date and time format for a defined country?
- How do I get pattern string of a SimpleDateFormat?
- How do I sort an array of string data using RuleBasedCollator class?
- How do I change number format symbols?
- How do I convert string to Date in GMT timezone?
- How do I format number as percentage string?
- How do I sort strings data using CollationKey class?
- How do I get a formatted date for a specific pattern and locale?
How do I format date using a locale based format?
The code below demonstrate how to format date information for a specific Locale. In the example utilize the java.text.SimpleDateFormat class.
package org.kodejava.example.text;
import java.util.Locale;
import java.util.Date;
import java.text.SimpleDateFormat;
public class FormatDateLocale {
public static void main(String[] args) {
//
// Defines an array of Locale we are going to use for
// formatting date information.
//
Locale[] locales = new Locale[] {
Locale.JAPAN,
Locale.CHINA,
Locale.KOREA,
Locale.TAIWAN,
Locale.ITALY,
Locale.FRANCE,
Locale.GERMAN
};
// Get an instance of current date time
Date today = new Date();
//
// Iterates the entire Locale defined above and create a long
// formatted date using the SimpleDateFormat.getDateInstance()
// with the format, the Locale and the date information.
//
for (Locale locale : locales) {
System.out.println("Date format in "
+ locale.getDisplayName()
+ " = "
+ SimpleDateFormat.getDateInstance(
SimpleDateFormat.LONG, locale)
.format(today).toUpperCase());
}
}
}
The result of our code are:
Date format in Japanese (Japan) = 2009/01/04 Date format in Chinese (China) = 2009年1月4日 Date format in Korean (South Korea) = 2009년 1월 4일 (일) Date format in Chinese (Taiwan) = 2009年1月4日 Date format in Italian (Italy) = 4 GENNAIO 2009 Date format in French (France) = 4 JANVIER 2009 Date format in German = 4. JANUAR 2009