package dateformat;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class TestDateFormat {
public static void
main(String[] args) {
Locale[] locales = {
Locale.CANADA,
Locale.FRANCE,
Locale.GERMANY,
Locale.US,
Locale.JAPAN,
Locale.ITALY
};
Date today = new Date();
for (Locale locale : locales) {
StringBuilder sb = new StringBuilder();
sb.append(locale.getDisplayCountry());
sb.append("\n--------------------------------");
//
// Gets a
DateFormat instance for the specified locale
// and
format a date object by calling the format
//
method.
//
DateFormat
df = DateFormat.getDateInstance(
DateFormat.DEFAULT, locale);
String
date = df.format(today);
sb.append("\n Default date format: ").append(date);
//
// Gets a
DateFormat instance for the specified locale
// and
format a time information by calling the format
//
method.
//
DateFormat
tf = DateFormat.getTimeInstance(
DateFormat.DEFAULT, locale);
String
time = tf.format(today.getTime());
sb.append("\n Default time format: ").append(time)
.append("\n");
String
time2 = formatShortTime(today, locale);
sb.append("\n Short time format: ").append(time2)
.append("\n");
System.out.println(sb.toString());
}
//
// Gets date and time formatted value for Italy
locale using
// To display a date and time in the same
String, create the
// formatter with the getDateTimeInstance
method.
// The first parameter is the date style, and
the second is
// the time style. The third parameter is the
Locale
//
DateFormat dtf =
DateFormat.getDateTimeInstance(
DateFormat.DEFAULT,
DateFormat.DEFAULT,
Locale.ITALY);
String datetime = dtf.format(today);
System.out.println("date time format in "
+
Locale.ITALY.getDisplayCountry() + ": " + datetime);
DateFormat tf3 =
DateFormat.getTimeInstance(
DateFormat.SHORT, Locale.ITALY);
String timeStr = "12.03";
Date date = null;
try {
date = tf3.parse(timeStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("The parsing result of " +
timeStr + " is: " + date);
for (Locale locale2 : getAllLocales()) {
System.out.println("Locale " + locale2 + ":
<"
+ formatShortTime(today, locale2) + ">, Time
Separator: <"
+ getTimeSeparator(locale2) + ">");
}
}
public static String formatShortTime(Date today, Locale
locale) {
DateFormat tf2 = DateFormat.getTimeInstance(
DateFormat.SHORT, locale);
String time2 = tf2.format(today.getTime());
return time2;
}
public static String getTimeSeparator(Locale locale) {
String sepValue =
":";
DateFormat dateFormatter
= DateFormat.getTimeInstance(DateFormat.SHORT,
locale);
DateFormatSymbols dfs =
new DateFormatSymbols(locale);
Date now = new
Date();
String[] amPmStrings =
dfs.getAmPmStrings();
String localeTime =
dateFormatter.format(now);
//remove the am pm string
if they exist
for (int i = 0; i
< amPmStrings.length; i++) {
localeTime =
localeTime.replace(dfs.getAmPmStrings()[i], "");
}
localeTime =
localeTime.replaceAll(" ", "");
// search for the
character that isn't a digit.
for (int currentIndex =
0; currentIndex < localeTime.length();
currentIndex++) {
if
(!(Character.isDigit(localeTime.charAt(currentIndex))))
{
sepValue = localeTime.substring(currentIndex,
currentIndex + 1);
break;
}
}
return sepValue;
}
public static Locale[] getAllLocales(){
List locales = new
ArrayList<Locale>();
locales.add(Locale.CANADA);
locales.add(Locale.CANADA_FRENCH);
locales.add(Locale.CHINA);
locales.add(Locale.CHINESE);
locales.add(Locale.ENGLISH);
locales.add(Locale.FRANCE);
locales.add(Locale.FRENCH);
locales.add(Locale.GERMAN);
locales.add(Locale.GERMANY);
locales.add(Locale.ITALIAN);
locales.add(Locale.ITALY);
locales.add(Locale.JAPAN);
locales.add(Locale.JAPANESE);
locales.add(Locale.KOREA);
locales.add(Locale.KOREAN);
locales.add(Locale.PRC);
locales.add(Locale.ROOT);
locales.add(Locale.SIMPLIFIED_CHINESE);
locales.add(Locale.TAIWAN);
locales.add(Locale.TRADITIONAL_CHINESE);
locales.add(Locale.UK);
locales.add(Locale.US);
return (Locale[]) locales.toArray(new Locale[0]);
}
}