首页 > 编程 > Java > 正文

Java使用DateFormatter格式化日期时间的方法示例

2019-11-26 12:41:59
字体:
来源:转载
供稿:网友

本文实例讲述了Java使用DateFormatter格式化日期时间的方法。分享给大家供大家参考,具体如下:

Java版本:1.8开始

import java.time.LocalDate;import java.time.LocalDateTime;import java.time.LocalTime;import java.time.ZonedDateTime;import java.time.format.DateTimeFormatter;/** * Created by Frank */public class CurrentDatetime { public static void main(String[] args) { LocalDate dNow = LocalDate.now(); System.out.println(dNow); LocalTime tNow = LocalTime.now(); System.out.println(tNow); LocalDateTime now = LocalDateTime.now(); System.out.println(now); DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy/MM/dd"); System.out.println(df.format(LocalDateTime.now())); System.out.println(LocalDate.parse("2016/11/28", df)); DateTimeFormatter nTZ = DateTimeFormatter.ofPattern("d MMMM, yyyy h:mm a"); System.out.println(ZonedDateTime.now().format(nTZ));/*输出:2017-04-0216:43:29.9882017-04-02T16:43:29.9882017/04/022016-11-282 四月, 2017 4:43 下午*/ }}

运行效果如下图:

注:java.time为JDK1.8版本开始引入,另外本例测试环境使用的eclipse为4.4版本以上(本站下载地址://www.VeVB.COm/softs/390522.html),否则版本过低会提示错误!

PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:

在线日期/天数计算器:
http://tools.VeVB.COm/jisuanqi/date_jisuanqi

在线日期计算器/相差天数计算器:
http://tools.VeVB.COm/jisuanqi/datecalc

在线日期天数差计算器:
http://tools.VeVB.COm/jisuanqi/onlinedatejsq

Unix时间戳(timestamp)转换工具:
http://tools.VeVB.COm/code/unixtime

希望本文所述对大家java程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表