Date类的构造方法:
Date():获取当前系统的时间
Date(long time):获取从格林尼治时间进过time时间的日期;
Date中常用的方法有:
方法 | 返回值类型 | 说明 |
after(Date when) | boolean | 测试当前日期是否在指定时间when之后 |
before(Date when) | boolean | 测试当前日期是否在支付顶时间when之后 |
getTime() | long | 获取从1970年1月1日0时0分0秒开始到当前的毫秒数 |
setTime(long time) | void | 设置当前Date对象的日期和时间 |
当然,你来这里百分之九十是来看时间的转换
java中提供了DateFormat类来按照指定格式对日期和时间进行格式化,DateFormat中常用的方法如下:
方法 | 返回值类型 | 说明 |
format(Date date) | String | 将一个Date格式化为日期和时间字符串 |
getDateInstance() | DateFormat | 获取日期格式器,具有默认语言环境的默认格式化风格 |
getDateTimeIntance() | DateFormat | 获取日期时间格式器,具有默认语言环境的默认格式化风格 |
getInstance() | DateFormat | 获取SHORT风格(完全为数字)的日期和时间格式器 |
getTimeInstance() | DateFormat | 获取时间格式器,具有默认语言环境的默认格式化风格 |
parse() | Date | 将给定的字符串解析为日期/时间 |
DateFormat是抽象类,所以要通过包含instance的格式器静态方法获取其对象
getDateInstance()这种形式有3中构造方法:
getDateInstance() |
getDateInstance(int style) |
getDateInstance(int dateStyle, int timeStyle, Locale aLocale)//不多了解 |
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);//Fri May 06 15:11:52 CST 2022
DateFormat df = DateFormat.getDateInstance();
String format = df.format(date);
System.out.println(format);//2022-5-6
DateFormat df1 = DateFormat.getDateInstance(1);
String format1 = df1.format(date);
System.out.println(format1);//2022年5月6日
DateFormat df2 = DateFormat.getDateInstance(2);
String format2 = df2.format(date);
System.out.println(format2);//2022-5-6
DateFormat df3 = DateFormat.getDateInstance(3);
String format3 = df3.format(date);
System.out.println(format3);//22-5-6
DateFormat dtf = DateFormat.getDateTimeInstance();
String dtfFormat = dtf.format(date);
System.out.println(dtfFormat); //日期时间 2022-5-6 15:11:52
DateFormat instance = DateFormat.getInstance();
String format4 = instance.format(date);
System.out.println(format4);//日期和时间的short风格 22-5-6 下午3:11
DateFormat timeInstance = DateFormat.getTimeInstance();
String format5 = timeInstance.format(date);
System.out.println(format5); //只显示时间15:11:52
}
运行结果
Fri May 06 15:11:52 CST 2022
2022-5-6
2022年5月6日
2022-5-6
22-5-6
2022-5-6 15:11:52
22-5-6 下午3:11
15:11:52
<...了解一下Locale...>,就当涨涨见识吧
/* Locale[] availableLocales = DateFormat.getAvailableLocales();
System.out.println(Arrays.toString(availableLocales));*/
运行结果
[, ar_AE, ar_JO, ar_SY, hr_HR, fr_BE, es_PA, mt_MT, es_VE, bg, zh_TW, it, ko, uk, lv, da_DK, es_PR, vi_VN, en_US, sr_ME, sv_SE, es_BO, en_SG, ar_BH, pt, ar_SA, sk, ar_YE, hi_IN, ga, en_MT, fi_FI。。。。]
如果上面的日期时间格式器都不能满足你的要求,java提供了DateFormat类的子类来自定义日期时间的格式
Date date = new Date();
System.out.println(date);//Fri May 06 16:31:03 CST 2022
DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format = simpleDateFormat.format(date);
System.out.println(format);//2022-05-06 04:31:03
DateFormat dateInstance = DateFormat.getDateInstance();
Date parse = dateInstance.parse("2022-01-12");
System.out.println(parse);//2022-05-06 04:31:03
Date parse1 = simpleDateFormat.parse("2022-02-12");//会出现错误
System.out.println(parse1);
//Exception in thread "main" java.text.ParseException: Unparseable date: "2022-02-12"
DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
这种有一个非常容易出现的错误,就是现在设置的格式SimpleDateFormat("yyyy-MM-dd hh:mm:ss"),使用format()方法可以将Date日期转换为"yyyy-MM-dd hh:mm:ss"的形式,但是问题出现在parse(),设置的格式为"yyyy-MM-dd hh:mm:ss",所以这里切记要注意parse()方法中要转换为Date的字符串必须"yyyy-MM-dd hh:mm:ss"的形式,否则会报错:Exception in thread "main" java.text.ParseException: Unparseable date: "2022-02-12"
DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date parse1 = simpleDateFormat.parse("2022-02-12 00:00:00");
System.out.println(parse1)//Sat Feb 12 00:00:00 CST 2022
运行结果:Sat Feb 12 00:00:00 CST 2022