为不同国家设置日期格式的 Java 程序
首先,设置语言环境-
Locale[] strLocales = { US, UK, GERMANY};现在,让我们设置日期格式,即不同的常量-
int[] constants = { SHORT, MEDIUM, LONG };
String[] str = { "SHORT", "MEDIUM", "LONG" };循环并获取不同国家/地区的不同日期格式-
for (Locale country : strLocales) {
System.out.println("\n"+ country.getDisplayCountry() + ".....");
for (int i = 0; i < constants.length; i++) {
dateFormat = DateFormat.getDateInstance(constants[i], country);
System.out.println(str[i] + " constant = " + dateFormat.format(dt));
}
}下面是一个例子
示例
import static java.text.DateFormat.*;
import static java.util.Locale.*;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class Demo {
public static void main(String[] args) {
Date dt = new Date();
Locale[] strLocales = { US, UK, GERMANY};
int[] constants = { SHORT, MEDIUM, LONG };
String[] str = { "SHORT", "MEDIUM", "LONG" };
DateFormat dateFormat = null;
for (Locale country : strLocales) {
System.out.println("\n"+ country.getDisplayCountry() + ".....");
for (int i = 0; i < constants.length; i++) {
dateFormat = DateFormat.getDateInstance(constants[i], country);
System.out.println(str[i] + " constant = " + dateFormat.format(dt));
}
}
}
}输出结果United States..... SHORT constant = 11/22/18 MEDIUM constant = Nov 22, 2018 LONG constant = November 22, 2018 United Kingdom..... SHORT constant = 22/11/18 MEDIUM constant = 22-Nov-2018 LONG constant = 22 November 2018 Germany..... SHORT constant = 22.11.18 MEDIUM constant = 22.11.2018 LONG constant = 22. November 2018
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短