📄 dateformattest.java
字号:
import java.text.*;
import java.util.*;
public class DateFormatTest
{
//显示日期
public void showDate(Date date, int datestyle, Locale locale)
{
DateFormat currentDateFormat =
DateFormat.getDateInstance(datestyle,locale);
//创建DateFormat对象currentDateFormat,用来分析传入参数locale的日期格式
String dateString = currentDateFormat.format(date);
System.out.println("The date is: " + dateString);
}
//显示时间
public void showTime(Date date, int timestyle, Locale locale)
{
DateFormat currentTimeFormat =
DateFormat.getTimeInstance(timestyle,locale);
String timeString = currentTimeFormat.format(date);
System.out.println("The Time is: " + timeString);
}
public static void main(String[] args)
{
int styles[] = {DateFormat.DEFAULT,
DateFormat.FULL,
DateFormat.LONG,
DateFormat.SHORT};
//获得当前日期和时间
Date date = new Date();
DateFormatTest test = new DateFormatTest();
//打印中国日期和时间
System.out.println("中国:");
test.showDate(date, styles[0], Locale.CHINA);
test.showTime(date, styles[0],Locale.CHINA);
//打印美国日期和时间
System.out.println("美国:");
test.showDate(date, styles[0], Locale.US);
test.showTime(date, styles[0],Locale.US);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -