当前位置:网站首页>4hutool practice: dateutil format time [easy to understand]
4hutool practice: dateutil format time [easy to understand]
2022-07-01 09:52:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
hutool actual combat ( Take you to master all kinds of tools inside ) Catalog
To explore JDK The core underlying source code , That must be mastered native usage
Source code analysis :JDK Get the risks and best practices for the default time zone
java8 New date and time API(( One )JSR-310:ZoneId Time zone and offset )
java8 New date and time API(( Two )JSR-310: Common date time API)
java8 New date and time API(( 3、 ... and )JSR-310: Format and parse )
java8 New date and time API(( Four )JSR-310: Common calculation tools )
java8 New date and time API(( 5、 ... and )JSR-310: actual combat + Source code analysis )
When the rules of the time zone change , How to synchronize JDK Time zone rule for
hutool Date time series
1DateUtil( Time tools )- Current time and current timestamp
3DateUtil( Time tools )- Get various contents of the date
4DateUtil( Time tools )- Format time
5DateUtil( Time tools )- Parse the formatted time
6DateUtil( Time tools )- Time offset acquisition
7DateUtil( Time tools )- Date calculation
8ChineseDate( Lunar date tool class )
9LocalDateTimeUtil(JDK8+ Medium {@link LocalDateTime} Tool class encapsulation )
10TemporalAccessorUtil{@link TemporalAccessor} Tool class encapsulation
purpose : Format time
Use scenarios
Format the date into the corresponding date string according to different requirements
Project reference
This blog post is based on :hutool-5.6.5 Version source code
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.6.5</version>
</dependency>Method summary
Method | describe |
|---|---|
cn.hutool.core.date.DateUtil.formatLocalDateTime(java.time.LocalDateTime) | Format date time <br> Format yyyy-MM-dd HH:mm:ss |
cn.hutool.core.date.DateUtil.format(java.time.LocalDateTime, java.lang.String) | Format the date according to a specific format |
cn.hutool.core.date.DateUtil.format(java.util.Date, java.lang.String) | Format the date according to a specific format |
cn.hutool.core.date.DateUtil.format(java.util.Date, cn.hutool.core.date.format.DatePrinter) | Format the date according to a specific format |
cn.hutool.core.date.DateUtil.format(java.util.Date, java.text.DateFormat) | Format the date according to a specific format |
cn.hutool.core.date.DateUtil.format(java.util.Date, java.time.format.DateTimeFormatter) | Format the date according to a specific format |
cn.hutool.core.date.DateUtil.formatDateTime(java.util.Date) | Format date time <br> Format yyyy-MM-dd HH:mm:ss |
cn.hutool.core.date.DateUtil.formatDate(java.util.Date) | Format date section ( Not including time )<br> Format yyyy-MM-dd |
cn.hutool.core.date.DateUtil.formatTime(java.util.Date) | Format time <br> Format HH:mm:ss |
cn.hutool.core.date.DateUtil.formatHttpDate(java.util.Date) | Formatted as Http Standard date format for <br> The standard date format follows RFC 1123 standard , The format is similar to :Fri, 31 Dec 1999 23:59:59 GMT |
cn.hutool.core.date.DateUtil.formatChineseDate(java.util.Date, boolean, boolean) | Format as Chinese date format , If isUppercase by false, Returns something like :2018 year 10 month 24 Japan , Otherwise, it returns two 〇 October 24, 2018 |
Method details
Method name :cn.hutool.core.date.DateUtil.formatLocalDateTime(java.time.LocalDateTime)
Methods described
Format date time <br> Format yyyy-MM-dd HH:mm:ss
Support version and above
Parameters to describe :
Parameter name | describe |
|---|---|
LocalDateTime localDateTime | localDateTime Formatted date |
Return value :
Formatted string
Reference cases :
String strDate = "2019-12-01 17:02:30";
LocalDateTime ldt = DateUtil.parseLocalDateTime(strDate);
String strDate1 = DateUtil.formatLocalDateTime(ldt);
Assert.assertEquals(strDate, strDate1);The source code parsing :
Method details
Method name :cn.hutool.core.date.DateUtil.format(java.time.LocalDateTime, java.lang.String)
Methods described
Format the date according to a specific format
Support version and above
Parameters to describe :
Parameter name | describe |
|---|---|
LocalDateTime localDateTime | localDateTime Formatted date |
String format | format Date format , For common formats, see : {@link DatePattern} |
Return value :
Formatted string
Reference cases :
String strDate2 = "2019-12-01 17:02:30.111";
ldt = DateUtil.parseLocalDateTime(strDate2, DatePattern.NORM_DATETIME_MS_PATTERN);
strDate1 = DateUtil.format(ldt, DatePattern.NORM_DATETIME_PATTERN);
Assert.assertEquals(strDate, strDate1);The source code parsing :
Method details
Method name :cn.hutool.core.date.DateUtil.format(java.util.Date, java.lang.String)
Methods described
Format the date according to a specific format
Support version and above
Parameters to describe :
Parameter name | describe |
|---|---|
Date date | date Formatted date |
String format | format Date format , For common formats, see : {@link DatePattern} |
Return value :
Formatted string
Reference cases :
String strDate = "2021-05-16";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date localDate = sdf.parse(strDate);
Assert.assertEquals(strDate, DateUtil.format(localDate, DatePattern.NORM_DATE_PATTERN));The source code parsing :
Method details
Method name :cn.hutool.core.date.DateUtil.format(java.util.Date, cn.hutool.core.date.format.DatePrinter)
Methods described
Format the date according to a specific format
Support version and above
Parameters to describe :
Parameter name | describe |
|---|---|
Date date | date Formatted date |
DatePrinter format | format {@link DatePrinter} or {@link FastDateFormat} |
Return value :
Formatted string
Reference cases :
// Format the date according to a specific format
String str = "2021-05-16";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
Date date = DateUtil.parse(str);
String dateStr = DateUtil.format(date,sdf);
System.out.println(dateStr);
Assert.assertEquals(str, dateStr);The source code parsing :
Method details
Method name :cn.hutool.core.date.DateUtil.format(java.util.Date, java.text.DateFormat)
Methods described
Format the date according to a specific format
Support version and above
Parameters to describe :
Parameter name | describe |
|---|---|
Date date | date Formatted date |
DateFormat format | format {@link SimpleDateFormat} |
Return value :
Formatted string
Reference cases :
To be added The source code parsing :
Method details
Method name :cn.hutool.core.date.DateUtil.format(java.util.Date, java.time.format.DateTimeFormatter)
Methods described
Format the date according to a specific format
Support version and above
5.0.0
Parameters to describe :
Parameter name | describe |
|---|---|
Date date | date Formatted date |
DateTimeFormatter format | format {@link SimpleDateFormat} |
Return value :
Formatted string
Reference cases :
// Format the date according to a specific format
String str = "2021-05-16";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.US);
Date date = DateUtil.parse(str);
String dateStr = DateUtil.format(date,dtf);
System.out.println(dateStr);
Assert.assertEquals(str, dateStr);
// There is a problem with this method , Officially processed The processing version is 5.7.5 Repair
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEraThe source code parsing :
Method details
Method name :cn.hutool.core.date.DateUtil.formatDateTime(java.util.Date)
Methods described
Format date time <br> Format yyyy-MM-dd HH:mm:ss
Support version and above
Parameters to describe :
Parameter name | describe |
|---|---|
Date date | date Formatted date |
Return value :
Formatted date
Reference cases :
String dateStr = "2017-03-01";
Date date = DateUtil.parse(dateStr);
String format = DateUtil.format(date, "yyyy/MM/dd");
Assert.assertEquals("2017/03/01", format);
// Format of common formats
String formatDate = DateUtil.formatDate(date);
Assert.assertEquals("2017-03-01", formatDate);
String formatDateTime = DateUtil.formatDateTime(date);
Assert.assertEquals("2017-03-01 00:00:00", formatDateTime);
String formatTime = DateUtil.formatTime(date);
Assert.assertEquals("00:00:00", formatTime);The source code parsing :
Method details
Method name :cn.hutool.core.date.DateUtil.formatDate(java.util.Date)
Methods described
Format date section ( Not including time )<br> Format yyyy-MM-dd
Support version and above
Parameters to describe :
Parameter name | describe |
|---|---|
Date date | date Formatted date |
Return value :
Formatted string
Reference cases :
String dateStr = "2017-03-01";
Date date = DateUtil.parse(dateStr);
String format = DateUtil.format(date, "yyyy/MM/dd");
Assert.assertEquals("2017/03/01", format);
// Format of common formats
String formatDate = DateUtil.formatDate(date);
Assert.assertEquals("2017-03-01", formatDate);
String formatDateTime = DateUtil.formatDateTime(date);
Assert.assertEquals("2017-03-01 00:00:00", formatDateTime);
String formatTime = DateUtil.formatTime(date);
Assert.assertEquals("00:00:00", formatTime);The source code parsing :
Method details
Method name :cn.hutool.core.date.DateUtil.formatTime(java.util.Date)
Methods described
Format time <br> Format HH:mm:ss
Support version and above
3.0.1
Parameters to describe :
Parameter name | describe |
|---|---|
Date date | date Formatted date |
Return value :
Formatted string
Reference cases :
String dateStr = "2017-03-01";
Date date = DateUtil.parse(dateStr);
String format = DateUtil.format(date, "yyyy/MM/dd");
Assert.assertEquals("2017/03/01", format);
// Format of common formats
String formatDate = DateUtil.formatDate(date);
Assert.assertEquals("2017-03-01", formatDate);
String formatDateTime = DateUtil.formatDateTime(date);
Assert.assertEquals("2017-03-01 00:00:00", formatDateTime);
String formatTime = DateUtil.formatTime(date);
Assert.assertEquals("00:00:00", formatTime);The source code parsing :
Method details
Method name :cn.hutool.core.date.DateUtil.formatHttpDate(java.util.Date)
Methods described
Formatted as Http Standard date format for <br> The standard date format follows RFC 1123 standard , The format is similar to :Fri, 31 Dec 1999 23:59:59 GMT
Support version and above
Parameters to describe :
Parameter name | describe |
|---|---|
Date date | date Formatted date |
Return value :
HTTP Standard form date string
Reference cases :
String formatHttpDate = DateUtil.formatHttpDate(DateUtil.parse("2019-01-02 22:32:01"));
Assert.assertEquals("Wed, 02 Jan 2019 14:32:01 GMT", formatHttpDate);The source code parsing :
Method details
Method name :cn.hutool.core.date.DateUtil.formatChineseDate(java.util.Date, boolean, boolean)
Methods described
Format as Chinese date format , If isUppercase by false, Returns something like :2018 year 10 month 24 Japan , Otherwise, it returns two 〇 October 24, 2018
Support version and above
5.3.9
Parameters to describe :
Parameter name | describe |
|---|---|
Date date | date Formatted date |
boolean isUppercase | isUppercase Whether in uppercase |
boolean withTime | withTime Whether to include the time part |
Return value :
Chinese date string
Reference cases :
String formatChineseDate = DateUtil.formatChineseDate(DateUtil.parse("2018-02-24"), true, false);
Assert.assertEquals(" Two 〇 February 24, 2018 ", formatChineseDate);The source code parsing :
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/131752.html Link to the original text :https://javaforall.cn
边栏推荐
- 项目必用的全局异常处理器,你学会了吗
- 【leetcode】287. Find duplicates
- Thread Basics
- 项目采购管理
- About widthstep of images in opencv
- Using closures to implement private variables
- PHP merges multiple arrays. The same element takes the intersection of different elements to form an array
- 怎么理解JS Promise
- High precision factorial
- JS prototype chain
猜你喜欢

好高的佣金,《新程序员》合伙人计划来袭,人人皆可参与!

Swag init error: cannot find type definition: response Response

Apple amplification! It's done so well
![[fxcg] large scale job hopping may be one of the driving forces behind the soaring inflation in the United States](/img/f6/be4e1affb9568acdedb94117b9b037.jpg)
[fxcg] large scale job hopping may be one of the driving forces behind the soaring inflation in the United States

The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious

炒币,亏了1000万。

I like two men...

7-Zip boycotted? The callers have committed "three crimes": pseudo open source, unsafe, and the author is from Russia!

Hololens2 development -6-eyetracking and speech recognition

HMS core audio editing service 3D audio technology helps create an immersive auditory feast
随机推荐
云原生到底是什么?它会是未来发展的趋势吗?
遇到女司机业余开滴滴,日入500!
Sd-wan notes
我喜欢两个男人。。。
MySQL interception_ MySQL method for intercepting strings [easy to understand]
嵌入式开发用到的一些工具
HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴
华为帐号多端协同,打造美好互联生活
LVGL V8.2字符串显示在Keil MDK上需要注意的事项(以小熊派为例)
请问有没有人知道clickhouse 中 limit语句执行的逻辑,图片中,上面的SQL可以执行成功
[untitled]
Mikrotik Routeros Internet access settings
dotnet 控制台 使用 Microsoft.Maui.Graphics 配合 Skia 进行绘图入门
Who has the vision to cross the cycle?
奇怪,为什么ArrayList初始化容量大小为10?
Centos 配置discuz 提示请检查 mysql 模块是否正确加载
Unity tips for reducing the amount of code -- empty protection extension
UE small knowledge point controller possess pawn process
Configure load balancing
[fxcg] large scale job hopping may be one of the driving forces behind the soaring inflation in the United States