1543105352845
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
2018-11-25 at 01:22:12 CET
Date date = new Date(); // this object contains the current date value
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
System.out.println(formatter.format(date));
Calendar calendar = Calendar.getInstance(); // get current instance of the calendar
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
System.out.println(formatter.format(calendar.getTime()));
25-11-2018 00:43:39
Java 8提供了一个全新的API,用以替换java.util.Date和java.util.Calendar。Date / Time API提供了多个类,帮助我们来完成工作,包括:
LocalDate
LocalTime
LocalDateTime
ZonedDateTime
LocalDate
LocalDate date = LocalDate.now(); // get the current date
我们可以format它
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
System.out.println(date.format(formatter));
得到的结果只有年月日,例如:
25-11-2018
LocalTime time = LocalTime.now(); // get the current time
可以按如下方式format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
System.out.println(time.format(formatter));
得到的结果类似如下:
00:55:58
LocalDateTime dateTime = LocalDateTime.now(); // get the current date and time
format的方式也一样
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
System.out.println(dateTime.format(formatter));
得到的日期结果类似于:
25-11-2018 00:57:20
Warning: count(): Parameter must be an array or an object that implements Countable in /www/wwwroot/javajgs.com/wp-content/plugins/wechat-social-login/templates/share/share-bar.php on line 7
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有