Java基础~Java Instant类 & 日期相关

_____________________________________________________________________________________________________________________________

 日期时间 (Demo DateDemo.java)
1.  日期格式化时,传入 pattern 中表示年份统一使用小写的 y。
说明:日期格式化时,yyyy 表示当天所在的年,而大写的 YYYY 代表是 week in which year(JDK7 之后引入的概念),意思是当天所在的周属于的年份,一周从周日开始,周六结束,只要本周跨年,返回的 YYYY就是下一年。
正例:表示日期和时间的格式如下所示:
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
2.  在日期格式中分清楚大写的 M 和小写的 m,大写的 H 和小写的 h 分别指代的意义。
说明:日期格式中的这两对字母表意如下:
1) 表示月份是大写的 M;
2) 表示分钟则是小写的 m;
3) 24 小时制的是大写的 H;
4) 12 小时制的则是小写的 h。
3.  获取当前毫秒数:System.currentTimeMillis(); 而不是 new Date().getTime()。
说明:如果想获取更加精确的纳秒级时间值,使用 System.nanoTime();的方式。在 JDK8 中,针对统计时间等场景,推荐使用 Instant 类。

_____________________________________________________________________________________________________________________________

 

Java基础~Java Instant类使用 

一、介绍
Instant类由一个静态的工厂方法now()可以返回当前时间戳
时间戳是包含日期和时间的,与java.util.Date很类似,事实上Instant就是类似JDK8以前的Date
Instant和Date这两个类可以进行转换


二、实例
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println("当前时间戳是:"+instant);

Date date = Date.from(instant);
System.out.println("当前时间戳是:"+date);

instant = date.toInstant();
System.out.println("当前时间戳是:"+instant);

_____________________________________________________________________________________________________________________________

 

posted @ 2022-08-30 16:18  kelelipeng  阅读(444)  评论(0编辑  收藏  举报