本文主要包含的有:
- 时间戳与Date类型的相互转换
- 判断是否为同一天
- 日期格式化
- 给日期加上指定时长
- 得到指定时间节点的Date
1、时间戳转Date
注:以下的方法中很多常量和方法我都没有提取出来,正式项目中还是建议大家封装在时间处理类中,规范化操作
-
public static void main(String[] args) {
-
// 10位的秒级别的时间戳
-
long time1 =
1527767665;
-
String result1 =
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss").format(
new Date(time1 *
1000));
-
System.out.println(
"10位数的时间戳(秒)--->Date:" + result1);
-
Date date1 =
new Date(time1*
1000);
//对应的就是时间戳对应的Date
-
// 13位的秒级别的时间戳
-
double time2 =
1515730332000d;
-
String result2 =
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss").format(time2);
-
System.out.println(
"13位数的时间戳(毫秒)--->Date:" + result2);
-
}
-
10位数的时间戳(秒)--->Date:
2018-
05-
31
19:
54:
25
-
13位数的时间戳(毫秒)--->Date:
2018-
01-
12
12:
12:
12
尤其要注意上面10位的秒级别的时间戳时,不能用int来定义time1变量,否则会得到错误的结果:
-
public static void main(String[] args) {
-
// 10位的秒级别的时间戳
-
int time1 =
1527767665;
//错误做法
-
String result1 =
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss").format(
new Date(time1 *
1000));
-
System.out.println(
"10位数的时间戳(秒)--->Date:" + result1);
-
}
10位数的时间戳(秒)--->Date:1969-12-17 23:21:47
2、Date转时间戳
-
public static void main(String[] args) {
-
//获取指定时间的时间戳,除以1000说明得到的是秒级别的时间戳(10位)
-
long time = (
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss")).parse(
"2018-06-30 20:00:00",
new ParsePosition(
0)).getTime() /
1000;
-
-
//获取时间戳
-
long now1 = System.currentTimeMillis();
-
long now2 =
new Date().getTime();
-
-
System.out.println(
"获取指定时间的时间戳:" + time);
-
System.out.println(
"当前时间戳:" +now1);
-
System.out.println(
"当前时间戳:" +now2);
-
}
-
获取指定时间的时间戳:
1530360000
-
当前时间戳:
1527769494340
-
当前时间戳:
1527769494340
3、格式化Date
-
public static void main(String[] args) {
-
//使用common-lang包下面的DateFormatUtils类
-
String format1 = DateFormatUtils.format(
new Date(),
"yyyy-MM-dd HH:mm:ss");
-
//使用最原始的SimpleDateFormat类
-
String format2 =
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss").format(
new Date());
-
-
System.out.println(
"格式化时间1:" + format1);
-
System.out.println(
"格式化时间2:" + format2);
-
}
-
格式化时间
1:
2018-
05-
31
20:
26:
49
-
格式化时间
2:
2018-
05-
31
20:
26:
49
DateFormatUtils是commons.lang3.time.DateFormatUtils下的,如果你的项目中没有,maven中引入下:
-
<dependency>
-
<groupId>org.apache.commons
</groupId>
-
<artifactId>commons-lang3
</artifactId>
-
<version>3.6
</version>
-
</dependency>
4、给日期加上指定时长
比如,给现在的时间加上12个小时,这个需求也很常见,例如我做过的某个秒杀接口要返回剩余秒杀时间给前端,那么我就直接计算(比如秒杀持续时间为12小时):秒杀(倒计时)截止时间=(当前时间+12H)即可。
-
public static void main(String[] args) {
-
//将指定日期加上固定时间,DateUtils还有其它添加分钟、小时、月份之类的方法api
-
//使用到的是commons-lang包下面的DateUitls类
-
Date date = DateUtils.addDays(
new Date(),
10);
//
-
System.out.println(
"当前时间为:"+DateFormatUtils.format(
new Date(),
"yyyy-MM-dd HH:mm:ss"));
-
String format = DateFormatUtils.format(date,
"yyyy-MM-dd HH:mm:ss");
-
System.out.println(
"当前时间加上10天后:" + format);
-
}
-
当前时间为:
2018-
05-
31
20:
31:
53
-
当前时间加上
10天后:
2018-
06-
10
20:
31:
53
DateUtils也是commons.lang3.time包下的,别忘了加maven依赖。
5、得到指定时间节点的日期时间
-
public static void main(String[] args) throws ParseException {
-
//得到指定日期
-
String date =
"2018-03-03 15:20:12";
-
Date parse =
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss").parse(date);
-
System.out.println(parse);
-
}
6、判断两个时间点是否为同一天、同一年
给定两个日期,快速判断两者是否为同一天或者同一年,比如我做过的一个接口需求是:每人每天有一次抽奖机会,那么当用户当天第二次发送请求时候,我就得判断查询参与记录,并且判断最新一次参与时间和当天是否为同一天。
-
/**
-
* 判断是否为同一天:使用commons-lang包下的DateUtils类
-
*
-
* @param day1
-
* @param day2
-
* @return
-
*/
-
public boolean isSameDay(Date day1, Date day2) {
-
return DateUtils.isSameDay(day1, day2);
-
}
-
-
/**
-
* 判断是否为同一天:使用joda依赖包里的时间类,效率从一定程度上优于DateUtils.isSameDay()方法
-
*
-
* @param date1
-
* @param date2
-
* @return
-
*/
-
public static boolean isSameDay1(Date date1,Date date2){
-
if(date1==
null || date2==
null){
-
throw
new IllegalArgumentException(
"date must be not null");
-
}
-
LocalDate localDate1 =
new LocalDate(
new DateTime(date1.getTime()));
-
LocalDate localDate2 =
new LocalDate(
new DateTime(date2.getTime()));
-
return localDate1.equals(localDate2);
-
}
基本上涉及到时间的处理就这么多了,希望对你有参考帮助!
引申阅读: 使用quartz实现高级定制化定时任务(包含管理界面)
推荐阅读:elastic search搜索引擎实战demo:https://github.com/simonsfan/springboot-quartz-demo,分支:feature_es
转载来源:https://blog.youkuaiyun.com/fanrenxiang/article/details/80531649