Java:日期时间转换与格式化
一、Java中时间类
1、Date类
Date常用的构造函数:
Date()生成一个代表当前日期时间的Date对象,相当于Date(System.currentTimeMillis())
Date(long date) 指定时间戳,默认单位ms。距1970.1.1 00:00:00的毫秒数。
Date常用方法:
boolean after(Date date) 判断是否在该时间日期之后
boolean before(Date date)
long getTime() 获取时间戳
void setTime() 设置时间戳
少用Date,尽量用Calendar代替。
2、Calendar类
Calendar类常用方法:
Calendar calendar=Calendar.getInstance(); 使用静态方法获取实例,默认为当前的时间日期
calendar.get(Calendar.YEAR) 获取指定字段的值,参数为预定义的常量,返回值均为int。月份比较特殊,0-11,0表示1月。
calendar.set(Calendar.YEAR,2020); 设置指定字段的值calendar.set(2020,1,1); 同时设置年月日
calendar.set(2020,1,1,1,1,1); 同时设置年月日时分秒
calendar.add(Calendar.YEAR,2); 增加指定字段的值
calendar.add(Calendar.YEAR,-1); 可以为负数
参数、返回值均为int型。
3、LocalDate类:代表当前时区的日期(Java8)
4、LocalTime类:代表当前时区的时间(Java8)
5、LocalDateTime:代表当前时区的日期时间(Java8)
6、获取时间戳的方法
(1)System.currentTimeMillis()
(2)new Date().getTime()
(3)Calendar.getInstance().getTimeInMillis(); //耗时较多
二、日期时间转换
1.java.sql.Date和java.util.Date相互转化:
//sql---->util
java.util.Date d=new java.sql.Date(1472025220343L);
System.out.println(d);//子类赋给父类 ,直接转换就行了!
//util--->sql
java.sql.Date d2=new java.sql.Date(new java.util.Date().getTime());
System.out.println(d2);
2.String----Date
DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
//DateFormat是抽象类 ,抽象类不可以直接创建对象,所以我们创建子类的对象
try {
java.util.Date d1=df.parse("1890-4-4 9-8-7");//这个格式必须按照上面给出的格式进行转化否则出错
} catch (ParseException e) {
e.printStackTrace();//为啥抛出异常的 不是随便的字符串都可以可以转化为日期吗的
}
3.Date---String
DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
java.util.Date da=new java.util.Date();
System.out.println("a1"+da);
System.err.println("a2"+da.toLocaleString());
String str=df.format(da);
System.out.println("a3"+str);
4.String-->Date--->Calendar
//创建Calendar对象
Calendar cal=Calendar.getInstance();
//String-->Date
java.sql.Date d=java.sql.Date.valueOf("1999-3-6");
//Date--->Calendar
cal.setTime(d);
System.out.println(cal);
三、日期时间格式化
1.DateFormat类
获取DateFormat实例:
DateFormat.getDateInstance() //只能格式化日期 2019年5月13日
DateFormat.getTimeInstance() //只能格式化时间 下午10:06:07
DateFormat.getDateTimeInstance() //格式化日期时间 2019年5月13日 下午10:06:07
以上方法均为静态方法。
以上方法均有2个重载方法:
指定显示样式,DateFormat类预定义的常量。
DateFormat.getDateInstance(int style) 指定日期显示样式
DateFormat.getTimeInstance(int style) 指定时间显示样式
DateFormat.getDateTimeInstance(int style,int style) 第一个参数指定Date显示样式,第二个参数指定Time显示样式
可用常量:
DateForm.SHORT : 2019/5/13 下午10:16
DateForm.MEDIUM : 2019年5月13日 下午10:17:06 (缺省时默认值就是DateForm.MEDIUM)
DateForm.LONG : 这个不常用
DateForm.FULL : 2019年5月13日星期一 中国标准时间 下午10:18:44
以上三个方法可再加一个参数指定国家:
DateFormat.getDateInstance(int style,Locale.CHINA) 指定日期显示样式
DateFormat.getTimeInstance(int style,Locale.CHINA) 指定时间显示样式
DateFormat.getDateTimeInstance(int style,int style,Locale.CHINA) 第一个参数指定Date显示样式,第二个参数指定Time显示样式
也可以使用其他国家。缺省第二个参数时,会使用默认值。第二个参数一般可以缺省。DateFormat实例常用的方法:
String format(Date date) 将Date对象转换为格式化的字符串,并返回字符串
package test;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
public class Test{
public static void main(String[] args) throws ParseException {
//使用静态方法获取实例。只能格式化日期
DateFormat df1=DateFormat.getDateInstance();
//只能格式化时间
DateFormat df2=DateFormat.getTimeInstance();
//格式化日期时间
DateFormat df3=DateFormat.getDateTimeInstance();
//要格式化的Date对象
Date date=new Date();
//使用format()格式化Date对象
System.out.println(df1.format(date));
System.out.println(df2.format(date));
System.out.println(df3.format(date));
}
}
2.SimpleDateFormat类
SimpleDateForm类是DateForm类的子类。SimpleDateForm比DateForm更简单,功能更强大。
package test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test{
public static void main(String[] args) throws ParseException {
//创建SimpleDateFormat对象,指定样式 2019-05-13 22:39:30
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//2019年的第133天。占位符是特定的
SimpleDateFormat sdf2=new SimpleDateFormat("y年的第D天");
//要格式化的Date对象
Date date=new Date();
//使用format()方法格式化Date对象为字符串,返回字符串
System.out.println(sdf1.format(date));
System.out.println(sdf2.format(date));
}
}
注意:关于代码中的模式字符串,如下
字母 | 日期或时间元素 | 表示 | 示例 |
G | Era 标志符 | Text | AD |
y | 年 | Year | 1996; 96 |
M | 年中的月份 | Month | July; Jul; 07 |
w | 年中的周数 | Number | 27 |
W | 月份中的周数 | Number | 2 |
D | 年中的天数 | Number | 189 |
d | 月份中的天数 | Number | 10 |
F | 月份中的星期 | Number | 2 |
E | 星期中的天数 | Text | Tuesday; Tue |
a | Am/pm 标记 | Text | PM |
H | 一天中的小时数(0-23) | Number | 0 |
k | 一天中的小时数(1-24) | Number | 24 |
K | am(中午)/pm(下午) 中的小时数(0-11) | Number | 0 |
h | am(中午)/pm(下午) 中的小时数(1-12) | Number | 12 |
m | 小时中的分钟数 | Number | 30 |
s | 分钟中的秒数 | Number | 55 |
S | 毫秒数 | Number | 978 |
3.DateTimeFormatter类(Java8)
DateTimeFormatter相当于DateFormat、SimpleDateFormat的合体,可以使用预定义的日期时间格式,也可以使用自定义的格式。但使用方式有些不同。用法:
package test;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class Test{
public static void main(String[] args) throws ParseException {
//和DateFormat不同,以下三个方法方法没有重载方法。预定义的常量为FormatStyle类的SHORT、MEDIUM、LONG、FULL
DateTimeFormatter df1=DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT); //只能格式化日期部分
DateTimeFormatter df2=DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT); //只能格式化时间部分
DateTimeFormatter df3=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT,FormatStyle.SHORT); //格式化日期时间
//使用自定义的格式
DateTimeFormatter df4=DateTimeFormatter.ofPattern("y-M-d H:m:s");
//要格式化的时间日期对象,只能用LocalDateTime。只涉及到日期也可以用LocalDate类,只涉及时间也可以用LocalTime类
LocalDateTime ldt=LocalDateTime.now();
//格式化,不能用Date类的实例作为参数
System.out.println(df1.format(ldt));
System.out.println(df2.format(ldt));
System.out.println(df3.format(ldt));
System.out.println(df4.format(ldt));
}
}
4.String.format()
如下示例:
public class Demo {
public static void main(String[] args) {
Date date=new Date();
String year=String.format("%tY",date);
String month=String.format("%tB",date);
String day=String.format("%td",date);
String date2=String.format("%tc",date);
System.out.println("完整日期是:"+date2);
System.out.println("今年是"+year+"年");
System.out.println("现在是"+month);
System.out.println("今天是"+day+"号");
}
}
关于日期时间格式化转换符,参见:Java日期时间格式化转换符