菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻

Java 日期格式类

原创
05/13 14:22 更新

Java格式 - Java日期格式类

Java 8有新的Date-Time API来处理日期和时间。 我们应该使用新的Java 8 Date-Time API来格式化和解析日期时间值。

如果我们正在编写与日期和时间相关的新代码,我们应该使用新的Date-Time API。

此部分适用于使用旧日期和日历类的旧代码。

Java库提供了两个类来格式化日期:

  • java.text.DateFormat
  • java.text.SimpleDateFormat

DateFormat 类是一个抽象类并且我们可以使用 DateFormat 类以预定义的格式来格式化日期。

因为它是抽象的,所以我们不能创建一个 DateFormat 类的实例使用 new 运算符。

我们必须使用它的一个 getXxxInstance()方法来创建新的实例。Xxx可以是日期,日期时间或时间。

要格式化日期时间值,我们使用 format()方法 DateFormat 类。

DateFormat类的格式化文本取决于两件事:

  • 样式
  • 语言环境

格式的样式决定了包括多少日期时间信息在格式化的文本

语言环境确定要使用的语言环境。

格式样式

Date Format 类将五个样式定义为常量:

  • DateFormat.DEFAULT
  • DateFormat.SHORT
  • DateFormat.MEDIUM
  • DateFormat.LONG
  • DateFormat.FULL

DEFAULT 格式与 MEDIUM 相同。getInstance()使用 SHORT

下表显示了对于美国区域设置以不同样式格式化的相同日期。

样式 格式化日期
DEFAULT Mar 27, 2014
SHORT 3/27/14
MEDIUM Mar 26, 2014
LONG March 26, 2014
FULL Sunday, November 2, 2014

例子

以下代码显示如何以简体中文格式显示语言环境的默认日期,法国和德国。

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
  public static void main(String[] args) {
    Date today = new Date();

    // Print date in the default locale format
    Locale defaultLocale = Locale.getDefault();
    printLocaleDetails(defaultLocale);
    printDate(defaultLocale, today);

    // Print date in French format
    printLocaleDetails(Locale.FRANCE);
    printDate(Locale.FRANCE, today);

    // Print date in German format. We could also use Locale.GERMANY
    // instead of new Locale ("de", "DE").
    Locale germanLocale = new Locale("de", "DE");
    printLocaleDetails(germanLocale);
    printDate(germanLocale, today);
  }

  public static void printLocaleDetails(Locale locale) {
    String languageCode = locale.getLanguage();
    String languageName = locale.getDisplayLanguage();
    String countryCode = locale.getCountry();
    String countryName = locale.getDisplayCountry();
    // Print the locale info
    System.out.println("Language: " + languageName + "(" + languageCode + "); "
        + "Country: " + countryName + "(" + countryCode + ")");
  }

  public static void printDate(Locale locale, Date date) {
    DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    String formattedDate = formatter.format(date);
    System.out.println("SHORT: " + formattedDate);

    formatter = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
    formattedDate = formatter.format(date);
    System.out.println("MEDIUM: " + formattedDate+"\n");
  }
}

上面的代码生成以下结果。



运行结果
PC端 手机端 平板端

Language: English(en); Country: United States(US)
SHORT: 11/16/23
MEDIUM: Nov 16, 2023

Language: French(fr); Country: France(FR)
SHORT: 16/11/23
MEDIUM: 16 nov. 2023

Language: German(de); Country: Germany(DE)
SHORT: 16.11.23
MEDIUM: 16.11.2023

java.util.Locale 类包含常见语言环境的常量。

我们可以使用 Locale.getDefault()方法获取系统的默认区域设置。

SimpleDateFormat类

要创建自定义日期格式,我们可以使用 SimpleDateFormat 类。

SimpleDateFormat类是对语言环境敏感的。

它的默认构造函数创建一个格式化程序,默认日期格式为默认语言环境。

SimpleDateFormat 类中的 format()方法执行日期格式。

例2

要更改后续格式化的日期格式,可以通过将新日期格式作为参数传递来使用applyPattern()方法。

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
  public static void main(String[] args) {
    SimpleDateFormat simpleFormatter = new SimpleDateFormat("dd/MM/yyyy");
    Date today = new Date();
    String formattedDate = simpleFormatter.format(today);
    System.out.println("Today is (dd/MM/yyyy):  " + formattedDate);

    simpleFormatter.applyPattern("MMMM dd, yyyy");
    formattedDate = simpleFormatter.format(today);
    System.out.println("Today is  (MMMM dd, yyyy): " + formattedDate);
  }
}

上面的代码生成以下结果。

Today is (dd/MM/yyyy):  16/11/2023
Today is  (MMMM dd, yyyy): November 16, 2023
综合评分:9.9 评分 请对本文进行纠错,及学习过程中有困难疑惑可在此进行讨论