java 中获取当前时间的方法有:date.gettime() 毫秒数;instant.now() 瞬时点;system.currenttimemillis() 毫秒数;calendar.gettime() date 对象。可使用 simpledateformat 或 datetimeformatter 格式化时间。
如何获取 Java 中的当前时间
在 Java 中,有几种方法可以获取当前时间:
- java.util.Date 类
java.util.Date 类表示特定时刻,可以调用其 getTime() 方法返回当前时间的毫秒数(自 1970 年 1 月 1 日午夜以来的毫秒数)。
立即学习“Java免费学习笔记(深入)”;
Date date = new Date();
long milliseconds = date.getTime();登录后复制 - java.time.Instant 类
java.time.Instant 类表示时间线上的一个瞬时点,其 now() 方法返回当前时刻。
Instant instant = Instant.now();登录后复制 - System.currentTimeMillis() 方法
System.currentTimeMillis() 方法返回当前时间的毫秒数(自 1970 年 1 月 1 日午夜以来的毫秒数)。
long milliseconds = System.currentTimeMillis();登录后复制 - java.util.Calendar 类
java.util.Calendar 类表示特定日历系统的日期和时间。可以调用其 getTime() 方法返回当前时间的 Date 对象。
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();登录后复制
如何格式化时间
获取当前时间后,可以使用以下类对时间进行格式化,例如: 
java.text.SimpleDateFormat
java.time.format.DateTimeFormatter
具体格式化方式取决于所需的格式。以上就是java当前时间怎么写的详细内容,更多请关注php中文网其它相关文章!

        
                            
Anya142Sa8 天前
发表在:ASUS华硕A8N-SLI Deluxe主板BIOS 10110Hello friends! I c...
91资源网站长-冰晨20 天前
发表在:广告合作123
FrankFAT20 天前
发表在:10日14日,星期二,在这里每天60秒读懂世界!Big cocks of blacks ...
RichardGlymn26 天前
发表在:Java webservice多个参数怎么调用https://t.me/win_1_c...
Thomasstolo28 天前
发表在:Java webservice多个参数怎么调用https://t.me/s/Casin...
Thomasstolo28 天前
发表在:Java webservice多个参数怎么调用https://t.me/s/Offic...
Thomasstolo29 天前
发表在:Java webservice多个参数怎么调用https://t.me/s/Offic...
Thomasstolo29 天前
发表在:Java webservice多个参数怎么调用https://t.me/s/Offic...
Charlesbeise1 个月前
发表在:10日02日,星期四,在这里每天60秒读懂世界!Get free Blockchain ...
WalterSnula1 个月前
发表在:Java webservice多个参数怎么调用https://t.me/s/Reyti...