Java使用DateFormatter格式化日期时间的方法示例
本文实例讲述了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程序设计有所帮助。
栏 目:JAVA代码
下一篇:Spring Boot(三)之找回熟悉的Controller,Service
本文标题:Java使用DateFormatter格式化日期时间的方法示例
本文地址:http://www.codeinn.net/misctech/26591.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




