dateformattest.java
来自「本java源程序包括了大量的学习程序(共27章)方便大家学习」· Java 代码 · 共 42 行
JAVA
42 行
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 + =
减小字号Ctrl + -
显示快捷键?