日期类
Java中的日期设计有点鸡肋,有很多方法都不能直接返回想要的结果。
但是通过一系列转换还是可以得到需要的格式的,只能说不尽人意。
这里介绍一下常用的三个日期类。
- Date
- SimpleDateFormat
- Calendar
# 1、Date类
即 java.util.Date
在Date的概念中,任何一个时间,都能表示成一组时间戳
如:1970-01-01 12:04:46
的时间戳是14686531
Date date1 = new Date(14686531L);
System.out.println(date1);
Date date2 = new Date(); //当前时间
System.out.println(date2);
输出:
Thu Jan 01 12:04:46 CST 1970
Tue Jan 26 10:01:29 CST 2021
但是,一般不会使用带参数的时间戳进行转换,也不会使用这种这么鸡肋的日期类型进行输出。
返回时间戳:
date2.getTime(); // 1611628686840
# 2、SimpleDateFormat类
即 java.text.SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 将date转化为字符串
Date date2 = new Date();
String str2 = sdf.format(date2);
System.out.println(str2);
输出:
2021-01-26 10:28:29
也可以格式化一串时间戳:
sdf.format(1611628662034L); //2021-01-26 10:37:42
SimpleDateFormat
可以定义一个日期格式,进行转换,格式表示有:
y : 年
M : 年中的月份
D : 年中的天数
d : 月中的天数
w : 年中的周数
W : 月中的周数
a : 上下/下午
H : 一天中的小时数(0-23)
h : 一天中的小时数(0-12)
m : 小时中的分钟
s : 分钟中的秒数
S : 毫秒数
eg:
SimpleDateFormat sdf = new SimpleDateFormat("现在是北京时间:y年M月d日 hh时m分s秒");
System.out.println(sdf.format(new Date()));
输出:
现在是北京时间:2021年1月26日 11时16分17秒
# 3、Calendar类
Calendar
出现的目的是为了替换Date
类。
Calendar
类一般用来计算某天是一个月的哪一天、星期几、某一个月第一天和最后一天 这种,或者 两个时间差。
用法:
Calendar c = Calendar.getInstance();
如果没有setTime()
,默认就是当前时间,也可以设置当前时间:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
calendar.setTime(simpleDateFormat.parse("2021-02-28")); //设置当前时间
# 1、获取当月月份,当年年份,当前日期
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
String year = String.valueOf(calendar.get(Calendar.YEAR)); //现在是哪一年
String month = String.valueOf(calendar.get(Calendar.MONTH) + 1); //现在是几月份 ,月份从 0 开始
String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)); //现在是月份的第几天
String week = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK) - 1); //现在是星期几 ,星期从 0 开始
System.out.println("现在时间是:" + year + "年" + month + "月" + day + "日,星期" + week);
输出:
现在时间是:2021年1月26日,星期2
嗯。。。。它比Date的好处起码可以知道今天是星期几了
# 2、加减日期
Calendar
类中提供了add方法。后推一天是 +1 ,向前是 -1
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
calendar.setTime(simpleDateFormat.parse("2021-02-28")); //设置当前时间
calendar.add(Calendar.DAY_OF_MONTH, +1);
Date date = calendar.getTime();
System.out.println(new SimpleDateFormat("yyyy-MM-dd ").format(date));
输出:
2021-03-01
Calendar
类 还用很多API,但是大部分好像都用不上~
计算时间差的demo:
class TestTimeDiff {
public static void main(String[] args) {
System.out.println(getTimeDeffer("2015-10-21 10:48:22", "2015-10-27 22:48:23"));
}
public static String getTimeDeffer(String time1, String time2) {
String result = "";
if (StringUtils.isNotBlank(time1) && StringUtils.isNotBlank(time2)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
long dt1 = sdf.parse(time1).getTime();
long dt2 = sdf.parse(time2).getTime();
long dc = Math.abs(dt2 - dt1);
long seconds = dc / 1000;
long date = seconds / (24 * 60 * 60); //相差的天数
long hour = (seconds - date * 24 * 60 * 60) / (60 * 60);//相差的小时数
long minute = (seconds - date * 24 * 60 * 60 - hour * 60 * 60) / (60);//相差的分钟数
long second = (seconds - date * 24 * 60 * 60 - hour * 60 * 60 - minute * 60);//相差的秒数
return (date == 0 ? "" : (date + "天")) + (hour == 0 ? "" : (hour + "小时")) + (minute == 0 ? "" : (minute + "分")) + (second == 0 ? "" : (second + "秒"));
} catch (ParseException e) {
e.printStackTrace();
}
}
return result;
}
}
输出:
6天12小时1秒
上次更新: 2025-02-21 06:04:57