本文实例讲述了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)); }}运行效果如下图:
注:java.time为JDK1.8版本开始引入,另外本例测试环境使用的eclipse为4.4版本以上(本站下载地址:https:///softs/390522.html),否则版本过低会提示错误!
PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:
在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在线日期计算器/相差天数计算器:
http://tools.jb51.net/jisuanqi/datecalc
在线日期天数差计算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime
希望本文所述对大家java程序设计有所帮助。