Java时间类记录

Java时间类记录

一、前言

  • 环境说明:

JDK:1.8

官方API文档:https://docs.oracle.com/javase/8/docs/api/index.html

二、记录

  • java.util.Date

参考:https://docs.oracle.com/javase/8/docs/api/java/util/Date.html

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
System.out.println(sdf.format(date));
  • java.util.Calendar

参考:https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html

Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DATE);
// int hour = calendar.get(Calendar.HOUR); // 12小时制
int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时制
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.println(year +"-"+ month +"-"+ day +" "+ hour +":"+ minute +":"+ second);
// or
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(calendar.getTime()));
  • java.time.LocalDatejava.time.LocalTimejava.time.LocalDateTime

JDK:1.8+

参考:

https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html

https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html

https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html

DateTimeFormatter dateDTF = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.now();
DateTimeFormatter timeDTF = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime localTime = LocalTime.now();
System.out.println(localDate.format(dateDTF) +" "+ localTime.format(timeDTF));
// or
DateTimeFormatter datetimeDTF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime.format(datetimeDTF));
  • System.currentTimeMillis()

参考:https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#currentTimeMillis–

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
System.out.println(sdf.format(date));
  • java.sql.Timestamp

参考:https://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
System.out.println(sdf.format(timestamp));
// or
timestamp = new Timestamp(System.currentTimeMillis());
date = new Date(timestamp.getTime());
System.out.println(sdf.format(date));
  • java.sql.Date

参考:https://docs.oracle.com/javase/8/docs/api/java/sql/Date.html

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
System.out.println(sdf.format(sqlDate));
// or
sqlDate = new java.sql.Date(System.currentTimeMillis());
date = new Date(sqlDate.getTime());
System.out.println(sdf.format(date));

三、其它

1.获取未来或过去24小时的时间

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date()); // 设置当前时间
for(int i=0; i< 24; i++){
    String time = sdf.format(calendar.getTime());
    System.out.println(time);
    // 如果 -1 则为是过去24小时的时间
    calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + 1);
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

趴着喝可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值