java.time.format.DateTimeFormatter
Java的日期与时间
DateTimeFormatter类是Java 8中日期时间功能里,用于解析和格式化日期时间的类,位于java.time.format包下。
1.预定义的DateTimeFormatter实例
DateTimeFormatter类包含一系列预定义(常量)的实例,可以解析和格式化一些标准时间格式。这将让你免除麻烦的时间格式定义,类中包含如下预定义的实例:
BASIC_ISO_DATE ISO_LOCAL_DATE
ISO_LOCAL_TIME
ISO_LOCAL_DATE_TIME ISO_OFFSET_DATE
ISO_OFFSET_TIME
ISO_OFFSET_DATE_TIME ISO_ZONED_DATE_TIME ISO_INSTANT ISO_DATE
ISO_TIME
ISO_DATE_TIME ISO_ORDINAL_TIME
ISO_WEEK_DATE RFC_1123_DATE_TIME
每个预定义的DateTimeFormatter实例都有不同的日期格式,我就不解释全部的了。具体的可以查阅Java官方文档,但我在这篇的后续中会解释其中几个,以方便理解。
2.格式化日期
当你获取一个DateTimeFormatter实例后,就可以用format()方便来将一个日期格式化为某种字符串,例如:
DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
String formattedDate = formatter.format(LocalDate.now());
System.out.println(formattedDate);
这个样例把LocalDate对象格式化了,并输出20150703,这个输出表示现在2015年,7月3日。
再举一个关于ZonedDateTime的例子:
DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
String formattedZonedDate = formatter.format(ZonedDateTime.now());
System.out.println("formattedZonedDate = " + formattedZonedDate);
这个例子会输出:20150703+0200
表示今年2015年,7月3日,位于UTC+2时区。
参考:http://blog.csdn.net/tjgykhulj/article/details/69053949
java.time.format.DateTimeFormatter的更多相关文章
- java.time.format.DateTimeParseException: Text '2019-10-11 12:30:30' could not be parsed at index 10
java.time.format.DateTimeParseException: Text '2019-10-11 12:30:30' could not be parsed at index 10 ...
- fastjson反序列化LocalDateTime失败的问题java.time.format.DateTimeParseException: Text '2019-05-24 13:52:11' could not be parsed at index 10
本地java类 import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime; ...
- JAVA Stirng.format 使用理解
JAVA Stirng.format 使用理解前言:项目中需要对一些字符串处理发现format方法的神奇之处一.api才是王道第一种二参使用①public static String format(S ...
- JAVA String.format 方法使用介绍
1.对整数进行格式化:%[index$][标识][最小宽度]转换方式 我们可以看到,格式化字符串由4部分组成,其中%[index$]的含义我们上面已经讲过,[最小宽度]的含义也很好理解, ...
- java String.Format详解
JDK1.5中,String类新增了一个很有用的静态方法String.format(): format(Locale l, String format, Object... args) 使用指定的语言 ...
- .Net转Java.08.format
%[index$][标识][最小宽度]转换方式 [index$]可以用于表示对第index个参数进行格式化, // Java代码 String s1=String.format("%3$s, ...
- JAVA String.format 方法使用介绍<转>
在JDK1.5中,String类增加了一个非常有用的静态函数format(String format, Objece... argues),可以将各类数据格式化为字符串并输出.其中format参数 ...
- java MessageFormat.format 用法
FormatElement: { ArgumentIndex }:是从0开始的入参位置索引. { ArgumentIndex , FormatType } { ArgumentIndex , Form ...
- Java String.Format() 方法及参数说明
转自:https://blueram.iteye.com/blog/441683 JDK1.5中,String类新增了一个很有用的静态方法String.format():format(Locale l ...
随机推荐
- Mysql和SqlServer互相转换
环境: Windows XP sp2 MS SQL Server 2OOO sp1 MySql 5.0.41 1:MSSQLServer数据库导入到MySql数据库 步骤: 1.安装mysql数据库的 ...
- Laravel利用pusher推送消息
一.注册pusher 1.注册https://pusher.com/ 2.获取key,密匙,app_id等 二.配置pusher 1.安装pusher composer require pusher/ ...
- Android实现炫酷SVG动画效果
svg是眼下十分流行的图像文件格式了,svg严格来说应该是一种开放标准的矢量图形语言,使用svg格式我们能够直接用代码来描画图像,能够用不论什么文字处理工具打开svg图像.通过改变部分代码来使图像具有 ...
- 获取bundle文件下的资源
NSBundle* bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle].resourcePath stringByAppendingPat ...
- sql2000实现row_number
一.以PersonID,classid,dt_ClassData为条件进行分组,每个小组加序号,row_number在sql2005中不可用 方法一.sql2000及以上版本--以PersonID,c ...
- WordPress函数:get_bloginfo()用法详解
描述 返回你博客的信息,这些信息可以用在任何地方的 PHP 代码中.这个函数,和 bloginfo() 一样,可以用来在模板文件的任何地方显示你博客的信息. 用法 <?php $bloginfo ...
- ASP.NET CORE RAZOR :在 ASP.NET Core 中开始使用 Razor Pages
来自:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/razor-pages/razor-pages-start 系统必备安装以下组件:. ...
- 企业级Nginx服务基础到架构优化详解
1.隐藏nginx header版本号 2.更改源码隐藏软件名称 3.更改nginx默认用户及用户组 4.配置nginx worker进程个数 5.根据CPU核数进行nginx进程优化 6.nginx ...
- Ubuntu 16.04.5下FFmpeg编译与开发环境搭建
PC环境: Ubuntu 18.04 上面只要安装下面的提示安装即可,基本上不必再下载依赖库的源代码进行编译和安装 编译步骤: 1, 安装相关工具: sudo apt install -y auto ...
- mongodb中批量将时间戳转变通用日期格式
1,官网提供的mongodb遍历脚本: 官方文档地址:https://docs.mongodb.org/manual/tutorial/remove-documents/ >var arr = ...