ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java日期解析

2020-07-29 12:35:00  阅读:356  来源: 互联网

标签:formatter 格式化 text 日期 LocalDateTime java 解析 LocalDate LocalTime


Java 8提供的日期格式化类是java.time.format.DateTimeFormatter,DateTimeFormatter中本身没有提供日期格式化和日期解析方法,这些方法还是由LocalDate、LocalTime和LocalDateTime提供的。
01. 日期格式化
日期格式化方法是format,这三个类每一个都有String format(DateTimeFormatter formatter),参数formatter是DateTimeFormatter类型。

02. 日期解析
日期解析方法是parse,这三个类每一个都有两个版本的parse方法,具体说明如下:
static LocalDateTime parse(CharSequence text):使用默认格式,从一个文本字符串获取一个LocalDateTime实例,如2007-12-03T10:15:30。
static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter):使用指定格式化,从文本字符串获取LocalDateTime实例。
static LocalDate parse(CharSequence text):使用默认格式,从一个文本字符串获取一个LocalDate实例,如2007-12-03。
static LocalDate parse(CharSequence text, DateTimeFormatter formatter):使用指定格式化,从文本字符串获取LocalDate实例。
static LocalTime parse(CharSequence text):使用默认格式,从一个文本字符串获取一个LocalTime实例。
static LocalTime parse(CharSequence text, DateTimeFormatter formatter):使用指定的格式化,从文本字符串获取LocalTime实例。

//HelloWorld.java文件
package com.a51work6;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class HelloWorld {
public static void main(String[] args) {
//// 创建LocalDateTime对象
LocalDateTime dateTime = LocalDateTime.now();
System.out.println("dateTime格式化之前:" + dateTime);
// 设置格式化类
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String text = dateTime.format(formatter);
System.out.println("dateTime格式化之后:" + text);
// 格式化字符串"2018-08-18 08:58:18",返回LocalDateTime对象
LocalDateTime parsedDateTime = LocalDateTime.parse("2018-08-18 08:58:18", formatter);
System.out.println("LocalDateTime解析之后:" + parsedDateTime);
//// 创建LocalDate对象
LocalDate date = LocalDate.now();
System.out.println("date格式化之前:" + date);
// 重新设置格式化类
formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
text = date.format(formatter);
System.out.println("date格式化之后:" + text);
// 格式化字符串"2018-08-18",返回LocalDate对象
LocalDate parsedDate = LocalDate.parse("2018-08-18", formatter);
System.out.println("LocalDate解析之后:" + parsedDate);
//// 创建LocalTime对象
LocalTime time = LocalTime.now();
System.out.println("time格式化之前:" + time);
// 重新设置格式化类
formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); ①
text = time.format(formatter);
System.out.println("time格式化之后:" + text);
// 格式化字符串"08:58:18",返回LocalTime对象
LocalTime parsedTime = LocalTime.parse("08:58:18", formatter); ②
System.out.println("LocalTime解析之后:" + parsedTime);
}
}

标签:formatter,格式化,text,日期,LocalDateTime,java,解析,LocalDate,LocalTime
来源: https://www.cnblogs.com/yunjingx/p/13396389.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有