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
", c); f.format(Locale.ITALY, "Locale.ITALY: %tc
", 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
", c); f.format(Locale.ITALY, "Locale.ITALY: %tc
", c); f.format(Locale.FRENCH, "Locale.FRENCH: %tc
", c); f.format(Locale.GERMANY, "Locale.GERMANY: %tc
", 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
Advertisement