java 日期格式常量_Java 日期格式类

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。

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

DEFAULTMar 27, 2014

SHORT3/27/14

MEDIUMMar 26, 2014

LONGMarch 26, 2014

FULLSunday, 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");

}

}

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

1f218e9ca217269720972d85f3717e44.png

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);

}

}

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

cc644f1029ab920146c1746940e26028.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值