本文实例讲述了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-02
16:43:29.988
2017-04-02T16:43:29.988
2017/04/02
2016-11-28
2 四月, 2017 4:43 下午
*/
}
}
运行效果如下图:
注:java.time为JDK1.8版本开始引入,另外本例测试环境使用的eclipse为4.4版本以上(本站下载地址:https://www.jb51.net/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程序设计有所帮助。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。