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

作者:FrankYou 时间:2021-07-15 15:29:23 

本文实例讲述了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使用DateFormatter格式化日期时间的方法示例

注: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,DateFormatter,日期时间
0
投稿

猜你喜欢

  • springboot集成mybatisplus的方法

    2022-08-02 16:35:12
  • Java常用数字工具类 数字转汉字(1)

    2022-06-25 11:01:45
  • eclipse如何搭建Springboot项目详解

    2023-11-29 04:52:36
  • 详解spring boot引入外部jar包的坑

    2021-07-29 10:49:49
  • MyBatis注解式开发映射语句详解

    2023-06-07 20:31:23
  • Java中的interrupted()和isInterrupted()

    2023-06-17 22:16:31
  • c语言10个经典小程序

    2023-11-03 01:11:35
  • Java中io流解析及代码实例

    2023-08-22 16:46:48
  • Flutter ListView 上拉加载更多下拉刷新功能实现方法

    2023-06-25 19:51:57
  • asp.net之生成验证码的方法集锦(一)

    2022-09-07 22:37:13
  • springboot+vue制作后台管理系统项目

    2022-02-26 06:08:37
  • 详解SpringCloud mysql实现配置中心

    2023-11-02 05:12:57
  • java中文转全拼工具类分享

    2022-02-07 09:50:57
  • C语言字符串操作总结大全(超详细)

    2023-07-06 15:14:56
  • SpringBoot集成MyBatis的分页插件PageHelper实例代码

    2023-09-11 21:54:33
  • IntelliJ IDEA中properties文件显示乱码问题的解决办法

    2021-09-08 04:59:03
  • 一文教会你使用jmap和MAT进行堆内存溢出分析

    2023-11-06 08:01:57
  • Java 处理超大数类型之BigInteger案例详解

    2021-06-20 15:36:38
  • Android 动画之TranslateAnimation应用详解

    2023-06-27 06:17:59
  • flutter日期选择器 flutter时间选择器

    2023-09-22 04:50:15
  • asp之家 软件编程 m.aspxhome.com