Java中特定于语言环境的格式
对于特定于语言环境的格式,请首先导入以下软件包。
import java.util.Calendar; import java.util.Formatter; import java.util.Locale;
创建一个Formatter和Calendar对象-
Formatter f = new Formatter(); Calendar c = Calendar.getInstance();
我们正在格式化不同的语言环境-
f.format(Locale.TAIWAN, "Locale.TAIWAN: %tc\n", c); f.format(Locale.ITALY, "Locale.ITALY: %tc\n", c);
以下是一个例子-
示例
import java.util.Calendar; import java.util.Formatter; import java.util.Locale; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); Calendar c = Calendar.getInstance(); f.format(Locale.TAIWAN, "Locale.TAIWAN: %tc\n", c); f.format(Locale.ITALY, "Locale.ITALY: %tc\n", c); f.format(Locale.FRENCH, "Locale.FRENCH: %tc\n", c); f.format(Locale.GERMANY, "Locale.GERMANY: %tc\n", c); System.out.println(f); } }
输出结果
Locale.TAIWAN: 星期日十一月 25 14:36:20 UTC 2018 Locale.ITALY: dom nov 25 14:36:20 UTC 2018 Locale.FRENCH: dim. nov. 25 14:36:20 UTC 2018 Locale.GERMANY: So Nov 25 14:36:20 UTC 2018