[ Back | Previous | Next ]

How to use Locale(s) with dateformatting

Package:
java.util.*
Product:
JDK
Release:
1.1.x
Related Links:
Calendar
EventObject
Hashtable
Locale
Properties
PropertyResourceBundle
TimeZone
Comment:
Let's say you want to create a function that prints the days of the week with the date/month/year in the different locales. We have to remember that most of the java.util.Local(s) are present.

Here's an example:

	private static Locale[] localeList = {
		new Locale("NL", "NL", ""),
		new Locale("EN", "US", ""),
		new Locale("DA", "DK", ""),
		new Locale("EN", "GB", ""),
		new Locale("EN", "CA", ""),
		new Locale("FR", "FR", ""),
		new Locale("FR", "CA", ""),
		new Locale("DE", "DE", ""),
		new Locale("IT", "IT", ""),
	new Locale("JA", "JP", ""),
	};

/**
 * This method was created in VisualAge.
 * @return java.lang.String
 * @param day int
 */
public String getDate(int day, String frmat) {
	cal = new GregorianCalendar();
	int wkOfYear = cal.get(Calendar.DAY_OF_YEAR);
	cal.clear();
	cal.set(Calendar.YEAR, 1999);
	cal.set(Calendar.DAY_OF_YEAR, wkOfYear);
	Date tmpDate = cal.getTime();
	// set time a few days back
	fmt = new SimpleDateFormat(frmat, localeList[getLanguage()]);
	fmt.setCalendar(cal);
	cal.add(Calendar.DATE, getDaysBefore());
	cal.add(Calendar.DATE, day);
	tmpDate = cal.getTime();
	fmt.setCalendar(cal);
	return fmt.format(cal.getTime());
}