java中关于日期的类的工具
1.Date日期类
主要用途为日期的比较
包:java.util
1.构造方法:
- Date() 返回此时的时间,用来获取当前时间
- Date(long date) 传入距离1970年1月1日0:0:00的毫秒数,返回Date类型,默认存在时区差,例如输入的是0,则输出的是8:00
2.常用的其他方法
//测试此日期是否在指定日期之后
boolean after (Date when )
//测试此日期是否在指定日期之前
boolean before (Date when )
//比较两个日期的顺序,在前返回值小于0,之后返回值大于0,相等返回0
int compareTo(Date another Date)
//比较两个日期的相等性
boolean equals (object obj)
//返回自1970年1月1日 00:00:00 GMT 以来此Date 对象表示的毫秒数。
long getTime()
2.Calendar 日历类
主要用途是对时间进行操作运算
星期是周日为第一天,月份也比实际大1
包:java.util
Calendar 是一个抽象类,无法直接调用,只能先实现实例化子类GregorianCalendar ,或者使用提供的静态方法getInstance获取当前地区的日历时间,需要配合getTime()或者getTimeInMillis()来显示时间
1.常见方法
//判定此Calendar 是否在指定的object之后
boolean after(object when)
//判定此Calendar 是否在指定的object之前
boolean before(object when)
//比较两个Calendar对象的时间值(从历元至现在的毫秒偏移量)
int compareTo (Calendar anotherCanlendar)
//将此Calendar与指定的object比较
boolean equals (object obj)
调整日历
set
//将给定的日历字段设置为指定值,因为存在方法重载,所以可以修改多个字段,年月日时分秒
void set(int filed, int value)
date2.set(Calendar.YEAR,1990);
add
根据日历的规则,为给定的日历字段添加或减去指定的时间量。
add(int field, int amount)
//将年份设置为去年
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -1);
roll(和 add方法类似)
//在给定的时间字段上添加或者减去单个时间单元,不更改更大的字段
roll(int field, int amount)
date2.roll(Calendar.MONTH,3);
3.总结
1.获取当前时间的方法
//1.Date方法
Date date = new Date() //返回的是时间格式 Mon Jun 29 19:50:12 CST 2020,
date.getTime(); //获取的是时间的毫秒数 1593431412849,返回的是一个long类型
//2.Calendar方法
Calendar calendar = Calendar.getInstance();
calendar.getTime(); //获取的是时间格式 返回一个date类型
calendar.getTimeInMillis(); //获取的是时间的毫秒数
2.时间的比较
Date类中的after() / before() / equals() / compareTo()
3.时间的设置运算
Calendar类中的set() 和roll() 方法
获取
计算生日以2000/01/01为例
//获取当前时间
Calendar calendar1 =Calendar.getInstance();
long time1 = calendar1.getTimeInMillis();
//先获取当前时间,然后将时间设置为2000/1/1,然后获取long 类型
Calendar calendar2 = Calendar.getInstance();
calendar2.set(2000,1,1);
long time2 = calendar2.getTimeInMillis();
System.out.println((now - time1)/1000/3600/24/365);
时间调整 add()/ roll()
Calendar calendar1 =Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
System.out.println(calendar1.getTime());
System.out.println(calendar2.getTime());
calendar1.add(Calendar.MONTH,-1);
calendar2.roll(Calendar.MONTH,-1);
System.out.println(calendar1.getTime());
System.out.println(calendar2.getTime());
4. DateFormat 日期的格式化
将日期格式显示为想要的格式
包: java.text
抽象类,调用需要使用实例化子类SimpleDateFormat
1.将Date转换成想要的字符串格式 :yyyy-MM-dd HH:mm:ss

pattern - 描述日期和时间格式的模式


具体实现:
//获取当前时间
Date date = new Date();
//设置时间的字符显示模式
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//调用format方法,返回的是String类型
String format = dateFormat.format(date);
System.out.println(format);
2.将一个字符串转换成Date类型
parse(String source)
从给定字符串的开始解析文本,以生成一个日期。
具体用法:
//先实例化SimpleDateFormat对象
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//定义想要转换的字符串
String format1 = "2020-7-15 20:00:30";
//将字符串传入parse方法
Date parse = dateFormat.parse(format1);
注意,如果是在IDEA中写入,软件会提示报错,需要配合try catch使用,防止出错
完整代码:
//先实例化SimpleDateFormat对象
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//定义想要转换的字符串
String format1 = "2020-7-15 20:00:30";
try {
Date parse = dateFormat.parse(format1);
System.out.println(parse);
} catch (ParseException e) {
e.printStackTrace();
}
本文介绍了Java中处理日期的三个主要类:Date用于日期比较,Calendar进行时间运算,DateFormat实现日期格式化。Date类包含构造方法及比较方法,Calendar提供set、add和roll等操作,DateFormat则能将Date转换为指定格式的字符串,以及反向转换。

997

被折叠的 条评论
为什么被折叠?



