所有分类
  • 所有分类
  • 未分类

Java时间(JDK8)-DateTimeFormatter的使用

简介

说明

本文用示例介绍Java中的DateTimeFormatter的用法。

Java中的DateTimeFormatter是用来格式化JDK8的新的日期类的,比如:LocalDateTime、LocalDate、LocalTime。

线程安全

DateTimeFormatter是线程安全的。Date的格式化工具SimpleDateFormat不是线程安全的。

简单示例

package org.example.a;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Demo{
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();

        //预定义的格式进行格式化
        String date = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(now);
        System.out.println(date);

        //自定义格式进行格式化
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(formatter.format(now));
        // 或者这样写:
        // System.out.println(now.format(formatter));

        //自定义格式进行格式化(带毫秒)
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        System.out.println(formatter1.format(now));
    }
}

 执行结果

2020-07-29T23:03:03.014
2020-07-29 23:03:03
2020-07-29T23:03:03.014

示例大全

1.中文的月份

package com.example.a;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Demo {
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = null;

        formatter = DateTimeFormatter.ofPattern("dd MM yyyy");
        System.out.println(formatter.format(now));

        formatter = DateTimeFormatter.ofPattern("dd MMM yyyy");
        System.out.println(formatter.format(now));

        formatter = DateTimeFormatter.ofPattern("dd MMMM yyyy");
        System.out.println(formatter.format(now));
    }
}

结果

19 04 2023
19 四月 2023
19 四月 2023

 可以发现,大于等于三个MMM时,就能输出本地语言对应的月份。

2.英文的月份

要输出英文时,要指定语言。

package com.example.a;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Demo {
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = null;

        formatter = DateTimeFormatter.ofPattern("dd M yyyy", Locale.ENGLISH);
        System.out.println(formatter.format(now));

        formatter = DateTimeFormatter.ofPattern("dd MM yyyy", Locale.ENGLISH);
        System.out.println(formatter.format(now));

        formatter = DateTimeFormatter.ofPattern("dd MMM yyyy", Locale.ENGLISH);
        System.out.println(formatter.format(now));

        formatter = DateTimeFormatter.ofPattern("dd MMMM yyyy", Locale.ENGLISH);
        System.out.println(formatter.format(now));

        formatter = DateTimeFormatter.ofPattern("dd MMMMM yyyy", Locale.ENGLISH);
        System.out.println(formatter.format(now));
    }
}

结果

19 4 2023
19 04 2023
19 Apr 2023
19 April 2023
19 A 2023

可以发现,M的个数可以是从1到5。

MMM:可以输出三位的月份。

MMMM: 可以输出整个月份单词。

格式参数

DateTimeFomatter类的一段注释。

SymbolMeaningPresentationExamples
GeratextAD; Anno Domini; A
uyearyear2004; 04
yyear-of-erayear2004; 04
Dday-of-yearnumber189
M/Lmonth-of-yearnumber/text7; 07; Jul; July; J
dday-of-monthnumber10
Q/qquarter-of-yearnumber/text3; 03; Q3; 3rd quarter
Yweek-based-yearyear1996; 96
wweek-of-week-based-yearnumber27
Wweek-of-monthnumber4
Eday-of-weektextTue; Tuesday; T
e/clocalized day-of-weeknumber/text2; 02; Tue; Tuesday; T
Fweek-of-monthnumber3
aam-pm-of-daytextPM
hclock-hour-of-am-pm (1-12)number12
Khour-of-am-pm (0-11)number0
kclock-hour-of-am-pm (1-24)number0
Hhour-of-day (0-23)number0
mminute-of-hournumber30
ssecond-of-minutenumber55
Sfraction-of-secondfraction978
Amilli-of-daynumber1234
nnano-of-secondnumber987654321
Nnano-of-daynumber1234000000
Vtime-zone IDzone-idAmerica/Los_Angeles; Z; -08:30
ztime-zone namezone-namePacific Standard Time; PST
Olocalized zone-offsetoffset-OGMT+8; GMT+08:00; UTC-08:00;
Xzone-offset ‘Z’ for zerooffset-XZ; -08; -0830; -08:30; -083015; -08:30:15;
xzone-offsetoffset-x+0000; -08; -0830; -08:30; -083015; -08:30:15;
Zzone-offsetoffset-Z+0000; -0800; -08:00;
ppad nextpad modifier1
escape for textdelimiter 
single quoteliteral ‘ 
[optional section start  
]optional section end  
#reserved for future use  
{reserved for future use  
}reserved for future use 

预定义格式

预定义格式化程序在DateTimeFormatter类中定义为常量。

大全

常量示例
BASIC_ISO_DATE‘20111203’
ISO_LOCAL_DATE‘2011-12-03’
ISO_OFFSET_DATE‘2011-12-03+01:00’
ISO_DATE‘2011-12-03’ ‘2011-12-03+01:00’
ISO_LOCAL_TIME’10:15′ ’10:15:30′
ISO_OFFSET_TIME’10:15+01:00′  ’10:15:30+01:00′
ISO_TIME’10:15′ ’10:15:30′ ’10:15:30+01:00′
ISO_LOCAL_DATE_TIME‘2011-12-03T10:15’ ‘2011-12-03T10:15:30’
ISO_OFFSET_DATE_TIME‘2011-12-03T10:15:30.016+01:00’
ISO_ZONED_DATE_TIME‘2011-12-03T10:15:30.+01:00[Europe/Paris]’
ISO_DATE_TIME‘2011-12-03T10:15:30’, ‘2011-12-03T10:15:30+01:00’ ‘2011-12-03T10:15:30+01:00[Europe/Paris]’
ISO_ORDINAL_DATE‘2012-337’
ISO_WEEK_DATE‘2012-W48-6’
ISO_INSTANT‘2011-12-03T10:15:30Z’

注意事项

上边ISO之后都是对应的类名,比如:ISO_OFFSET_DATE_TIME是对应的OffsetDateTime类。OffsetDateTime是Java 8 中一个新的日期时间API,它表示一个日期-时间,包含时区偏移量。它比LocalDateTime就多了一个时区,其他用法都一样。

package com.example.a;

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

public class Demo {
    public static void main(String[] args) {
        OffsetDateTime now = OffsetDateTime.now();

        // 使用自带的格式
        System.out.println(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(now));
        //或者:
        System.out.println(now.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

        // 自定义格式
        System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX").format(now));
    }
}

结果

2023-09-06T14:08:23.034+08:00
2023-09-06T14:08:23.034+08:00
2023-09-06T14:08:23+08:00
0

评论0

请先

显示验证码
没有账号?注册  忘记密码?

社交账号快速登录