在 Java 中使用 DateFormat.FULL 格式化时间
使用getTimeInstance()Java中的方法获取您设置的语言环境的时间格式。DateFormat.FULL是完整样式模式的常量。
首先,我们将创建Date对象
Date dt = new Date(); DateFormat dateFormat;
让我们用DateFormat.FULL格式化不同语言环境的时间
dateFormat = DateFormat.getTimeInstance(DateFormat.FULL, Locale.CHINESE); System.out.println("Locale CHINESE = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.FULL, Locale.CANADA); System.out.println("Locale CANADA = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.FULL, Locale.ITALY); System.out.println("Locale ITALY = " + dateFormat.format(dt));
以下是一个例子-
示例
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(); DateFormat dateFormat; //以FULL常数显示时间 dateFormat = DateFormat.getTimeInstance(DateFormat.FULL, Locale.FRENCH); System.out.println("Locale FRENCH = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.FULL, Locale.GERMANY); System.out.println("Locale GERMANY = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.FULL, Locale.CHINESE); System.out.println("Locale CHINESE = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.FULL, Locale.CANADA); System.out.println("Locale CANADA = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.FULL, Locale.ITALY); System.out.println("Locale ITALY = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.FULL, Locale.TAIWAN); System.out.println("Locale TAIWAN = " + dateFormat.format(dt)); } }输出结果
Locale FRENCH = 09 h 52 UTC Locale GERMANY = 09:52 Uhr UTC Locale CHINESE = 上午09时52分46秒 UTC Locale CANADA = 9:52:46 o'clock AM UTC Locale ITALY = 9.52.46 UTC Locale TAIWAN = 上午09時52分46秒 UTC