Java 日期格式类

Java 日期格式类

Java格式 - Java日期格式类Java 8有新的Date-Time API来处理日期和时间。 我们应该使用新的Java 8 Date-Time API来格式化和解析日期时间值。如果我们正在编写与日期和时间相关的新代码,我们应该使用新的Date-Time API。使用新的Java 8日期时间API格式化日期和时间。此部分适用于使用旧日期和日历类的旧代码。类Java库提供了两个类来格式化日期:java.text.DateFormatjava.text.SimpleDateFormat DateFormat 类是一个抽象类并且我们可以使用 DateFormat 类以预定义的格式来格式化日期。因为它是抽象的,所以我们不能创建一个 DateFormat 类的实例使用 new 运算符。我们必须使用它的一个 getXxxInstance()方法来创建新的实例。Xxx可以是日期,日期时间或时间。要格式化日期时间值,我们使用 format()方法 DateFormat 类。DateFormat类的格式化文本取决于两件事:样式语言环境格式的样式决定了包括多少日期时间信息在格式化的文本语言环境确定要使用的语言环境。格式样式 Date Format 类将五个样式定义为常量:DateFormat.DEFAULTDateFormat.SHORTDateFormat.MEDIUMDateFormat.LONGDateFormat.FULL DEFAULT 格式与 MEDIUM 相同。getInstance()使用 SHORT 。下表显示了对于美国区域设置以不同样式格式化的相同日期。样式格式化日期DEFAULTMar 27, 2014SHORT3/27/14MEDIUMMar 26, 2014LONGMarch 26, 2014FULLSunday, 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");

    }

    }

    上面的代码生成以下结果。 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);

}

}

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