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

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: 可以输出整个月份单词。

格式参数

符号    含义                        展现形式          示例
------  -------                    ------------      -------
G       era(时代)                   text              AD; Anno Domini; A
u       year                        year              2004; 04
y       year-of-era                 year              2004; 04
D       day-of-year                 number            189
M/L     month-of-year               number/text       7; 07; Jul; July; J
d       day-of-month                number            10

Q/q     quarter-of-year             number/text       3; 03; Q3; 3rd quarter
Y       week-based-year             year              1996; 96
w       week-of-week-based-year     number            27
W       week-of-month               number            4
E       day-of-week                 text              Tue; Tuesday; T
e/c     localized day-of-week       number/text       2; 02; Tue; Tuesday; T
F       week-of-month               number            3

a       am-pm-of-day                text              PM
h       clock-hour-of-am-pm (1-12)  number            12
K       hour-of-am-pm (0-11)        number            0
k       clock-hour-of-am-pm (1-24)  number            0

H       hour-of-day (0-23)          number            0
m       minute-of-hour              number            30
s       second-of-minute            number            55
S       fraction-of-second          fraction          978
A       milli-of-day                number            1234
n       nano-of-second              number            987654321
N       nano-of-day                 number            1234000000

V       time-zone ID                zone-id           America/Los_Angeles; Z; -08:30
z       time-zone name              zone-name         Pacific Standard Time; PST
O       localized zone-offset       offset-O          GMT+8; GMT+08:00; UTC-08:00;
X       zone-offset 'Z' for zero    offset-X          Z; -08; -0830; -08:30; -083015; -08:30:15;
x       zone-offset                 offset-x          +0000; -08; -0830; -08:30; -083015; -08:30:15;
Z       zone-offset                 offset-Z          +0000; -0800; -08:00;

预定义格式

预定义格式化程序在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

请先

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

社交账号快速登录