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 ...
随机推荐
- 【转】Understanding Inversion of Control, Dependency Injection and Service Locator Print
原文:https://www.dotnettricks.com/learn/dependencyinjection/understanding-inversion-of-control-depende ...
- 【转】Linux 中清空或删除大文件内容的五种方法(truncate 命令清空文件)
原文: http://www.jb51.net/article/100462.htm truncate -s 0 access.log -------------------------------- ...
- 转:深度学习斯坦福cs231n 课程笔记
http://blog.csdn.net/dinosoft/article/details/51813615 前言 对于深度学习,新手我推荐先看UFLDL,不做assignment的话,一两个晚上就可 ...
- JAVA Eclipse如何重新设置工作空间workspace
窗口-首选项-常规-启动和关闭,勾选启动时提示工作空间,然后移除现有的工作空间,最好也勾选启动时刷新工作空间 重启之后就可以设置工作空间了
- ubuntu 备份清理tomcat日志,并且删除七天之前的日志
创建文件clear_tomcat_log.sh #!/bin/bash logs_paths[0]="app_tomcat7_8080"; logs_paths[1]=" ...
- 【BIEE】07_调整BIEE柱子的显示顺序
现在有报表如下: 但是我们觉得这种显示不好看,想把非优秀员工的柱子放在前边显示,那么如何调整呢? 调整步骤: [编辑分析] 我们将此处条形图下的两个标签顺序重新调整一下 从上图可以看出,效果明显!
- Windows web服务器搭建---阿里云
前提步骤: 1)申请域名---- 阿里云.花生壳.万维网等等. 2)云主机购买-----阿里云.腾讯云.京东云等等. 3)网站备案,此步骤最长. 4)建立网站 5)部署网站 下面主要介绍如何部署网 ...
- Linux 查看某个程序所在端口的 PID
lsof -i:8085 加入显示的是2001 kill 2001 就可以杀死程序了
- 制作rpm包报错问题
在编译生成rpm包时,报错 ERROR 0001: file '***' contains a standard rpath '/usr/lib64'这是由于代码理由hardcode导致,解决问题有两 ...
- Atitit.跨语言 java c#.net php js常用的codec encode算法api 兼容性 应该内置到语言里面
Atitit.跨语言 java c#.net php js常用的codec encode算法api 兼容性 应该内置到语言里面 1. 常用算法1 1.1. 目录2 1.2. 定义和用法编辑2 1.3 ...