Java SimpleDateFormat – Java日期格式

Java SimpleDateFormat and DateFormat classes are used for date formatting. It is mostly used where we need to display or utilize the date and time functionality of Java. Both of these classes are present in com.text package.

Java SimpleDateFormat和DateFormat类用于日期格式化。 它通常用于需要显示或利用Java的日期和时间功能的地方。 这两个类都存在于com.text包中。

  • DateFormat is used for formatting a date into String based on specific locale that is provided as input.

    DateFormat用于根据作为输入提供的特定语言环境将日期格式化为String。
  • The locale is used for specifying the region and language for making the code more locale to the user.

    语言环境用于指定区域和语言,以使代码对用户更具语言环境。
  • The way of writing date is different in different regions of the world. For example, 31st Dec 2017 will be written in India as 31-12-2017 but the same thing will be written in US as 12-31-2017.

    日期的书写方式在世界不同地区是不同的。 例如,2017年12月31日将在印度写为2017年12月31日,而同一件事将在美国写为2017年12月31日。
  • Date Format classes are not synchronized, it’s recommended to create separate instance for each thread.

    日期格式类未同步,建议为每个线程创建单独的实例。

创建DateFormat实例 (Creating DateFormat instance)

DateFormat object can be created using the getDateInstance() and getTimeInstance() method of the DateFormat class.

可以使用DateFormat类的getDateInstance()getTimeInstance()方法创建DateFormat对象。

Locale loc = new Locale("en", "US");
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, loc);

As shown in the example above, the getDateInstance method of DateFormat needs two input parameters, the first parameter specifies the DateFormat to use and the second parameter is the locale.

如上面的示例所示,DateFormat的getDateInstance方法需要两个输入参数,第一个参数指定要使用的DateFormat,第二个参数是语言环境。

Java日期格式示例 (Java Date Format Example)

DateFormat class has a format method which is responsible for formatting.

DateFormat类具有负责格式化的格式化方法。

Locale locale = new Locale("fr", "FR");
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
String date = dateFormat.format(new Date());
System.out.print(date);

Output: 3 janv. 2018

输出: 3 janv. 2018 3 janv. 2018

In the above example, for creating a DateFormat instance we are using getDateInstance() method.

在上面的示例中,为了创建DateFormat实例,我们使用getDateInstance()方法。

For performing a time format, we need an instance of time. We will be using getTimeInstance() method for getting an instance of time.

为了执行时间格式,我们需要一个时间实例。 我们将使用getTimeInstance()方法获取时间实例。

Locale locale = new Locale("fr", "FR");
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale);
String date = dateFormat.format(new Date());
System.out.print(date);

Output: 11:03:01

输出: 11:03:0111:03:0111:03:01

Java SimpleDateFormat (Java SimpleDateFormat)

SimpleDateFormat is very much like DateFormat, the only major difference between them is that SimpleDateFormat can be used for formatting (Date to String conversion) and for parsing (String to Date conversion) with locale support, whereas DateFormat don’t have locale support.

SimpleDateFormat非常类似于DateFormat,它们之间的唯一主要区别在于,SimpleDateFormat可用于具有语言环境支持的格式设置(日期到字符串的转换)和用于解析( 字符串到日期的转换 ),而DateFormat不具有语言环境的支持。

Also, DateFormat is an abstract class that provides base support for date formatting and parsing, SimpleDateFormat is the concrete class that extends DateFormat class.

另外,DateFormat是一个抽象类,为日期格式和解析提供基本支持,SimpleDateFormat是扩展DateFormat类的具体类。

创建SimpleDateFormat实例 (Creating SimpleDateFormat instance)

SimpleDateFormat can be created using the SimpleDateFormat constructor, the constructor is a parametrised constructor and needs a String pattern as the parameter.

可以使用SimpleDateFormat构造函数创建SimpleDateFormat,该构造函数是参数化的构造函数,并且需要String模式作为参数。

String pattern = "MM-dd-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

In the example above the String pattern is the pattern which will be used to format a date and the output will be generated in that pattern as “MM-dd-yyyy”.

在上面的示例中,“字符串”模式是将用于格式化日期的模式,并且将在该模式中将输出生成为“ MM-dd-yyyy”。

Java SimpleDateFormat示例 (Java SimpleDateFormat Example)

In order to parse a date we need to create an instance of SimpleDateFormat using the constructor and then use format() method.

为了解析日期,我们需要使用构造函数创建SimpleDateFormat的实例,然后使用format()方法。

Let us look at an example for formatting date using SimpleDateFormat.

让我们看一个使用SimpleDateFormat格式化日期的示例。

String pattern = "MM-dd-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);

Output: 01-02-2018

输出: 01-02-2018

In the example above, the date is 2nd January 2018.

在上面的示例中,日期为2018年1月2日。

For parsing time, we have to change the pattern while creating SimpleDateFormat instance.

为了解析时间,我们必须在创建SimpleDateFormat实例时更改模式。

String pattern = " HH:mm:ss.SSSZ";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);

Output: 13:03:15.454+0530

输出: 13:03:15.454+0530

In the example above the patters is a time pattern and the formatting for the current time is done based on the pattern.

在上面的示例中,模式是时间模式,当前时间的格式化是基于该模式进行的。

使用SimpleDateFormat解析 (Parsing Using SimpleDateFormat)

Parsing is conversion of String into a java.util.Date instance. We can parse a string to a date instance using parse() method of the SimpleDateFormat class.

解析是将String转换为java.util.Date实例。 我们可以使用SimpleDateFormat类的parse()方法将字符串解析为日期实例。

For parsing a String to Date we need an instance of the SimpleDateFormat class and a string pattern as input for the constructor of the class.

为了将String解析为Date,我们需要SimpleDateFormat类的一个实例和一个字符串模式作为该类的构造函数的输入。

String pattern = "MM-dd-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = simpleDateFormat.parse("12-01-2018");
System.out.println(date);

Output: Sat Dec 01 00:00:00 IST 2018

输出: Sat Dec 01 00:00:00 IST 2018

Now let’s look at SimpleDateFormat example to parse time.

现在让我们看一下SimpleDateFormat示例来解析时间。

String pattern = "HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = simpleDateFormat.parse("22:00:03");
System.out.println(date);

Output: Thu Jan 01 22:00:03 IST 1970

输出: Thu Jan 01 22:00:03 IST 1970

In the example above, because we have not specified any date the program considered epoch as the date i.e 01-Jan-1970.

在上面的示例中,由于我们尚未指定任何日期,因此程序将epoch视为日期,即1970年1月1日。

带有语言环境的Java SimpleDateFormat (Java SimpleDateFormat with Locale)

We have worked with Locale as part of the DateFormat and we have seen that locales are used based on regions. Let us consider we want to use SimpleDateFormat in French, how to accomplish this?

我们已经将Locale作为DateFormat的一部分进行了工作,并且已经看到基于区域使用语言环境。 让我们考虑一下我们要使用法语使用SimpleDateFormat,该如何实现?

String pattern = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ";
SimpleDateFormat simpleDateFormat =new SimpleDateFormat(pattern, new Locale("fr", "FR"));
String date = simpleDateFormat.format(new Date());
System.out.println(date);

Output: mardi janvier 2018 14:51:02.354+0530

输出: mardi janvier 2018 14:51:02.354+0530

In the above example, the month and day are named in French based on the locale provided as input.

在上面的示例中,根据提供的语言环境以法语命名了月份和日期。

We have seen different ways of formatting and parsing. Let us have a look at the pattern syntax that should be used for the formatting pattern.

我们已经看到了不同的格式化和解析方式。 让我们看看应该用于格式化模式的模式语法。

Letter for PatternDate or Time componentExamples
GEra designatorAD
yYear2018(yyyy),18(yy)
MMonth in yearJuly(MMMM), Jul(MMM), 07(MM)
wResults in week in year16
WResults in week in month3
DGives the day count in the year266
dDay of the month09(dd), 9(d)
FDay of the week in month4
EDay name in the weekTuesday, Tue
uDay number of week where 1 represents Monday, 2 represents Tuesday and so on2
aAM or PM markerAM
HHour in the day (0-23)12
kHour in the day (1-24)23
KHour in am/pm for 12 hour format (0-11)0
hHour in am/pm for 12 hour format (1-12)12
mMinute in the hour59
sSecond in the minute35
SMillisecond in the minute978
zTimezonePacific Standard Time; PST; GMT-08:00
ZTimezone offset in hours (RFC pattern)-0800
XTimezone offset in ISO format-08; -0800; -08:00
图案字母 日期或时间部分 例子
G 时代代号 广告
ÿ 2018(yyyy),18(yy)
中号 一年中的月份 七月(MMMM),七月(MMM),07(MM)
w 一年中第几周的结果 16
w ^ 每月第几周的结果 3
d 给出一年中的天数 266
d 一个月中的某天 09(dd),9(d)
F 每月的星期几 4
Ë 星期几 星期二,星期二
ü 星期几,其中1代表星期一,2代表星期二,依此类推 2
一个 AM或PM标记 上午
H 一天中的小时(0-23) 12
ķ 一天中的小时(1-24) 23
ķ 上午/下午以12小时格式显示的小时(0-11) 0
H 以am / pm表示的小时,表示12小时格式(1-12) 12
一小时内 59
s 一分钟第二 35
小号 每分钟毫秒 978
ž 时区 太平洋标准时间; 太平洋标准时间; 格林尼治标准时间-08:00
ž 以小时为单位的时区偏移量(RFC模式) -0800
X ISO格式的时区偏移 -08; -0800; -08:00

Note: In the patterns above some letters should be used in different number for different results like for month July(MMMM), Jul(MMM), 07(MM) results differently.

注意:在上述模式中,对于不同的结果,应使用不同数量的字母,例如7月(MMMM),7月(MMM),07(MM)的结果将有所不同。

Java日期时间格式示例 (Java Date Time Format Example)

We discussed about various aspects of DateFormat and SimpleDateFormat. Let us now look at some examples for different formats of date and time.

我们讨论了DateFormat和SimpleDateFormat的各个方面。 现在让我们看一些日期和时间格式不同的示例。

PatternResult
MM/dd/yyyy01/02/2018
dd-M-yyyy hh:mm:ss02-1-2018 06:07:59
dd MMMM yyyy02 January 2018
dd MMMM yyyy zzzz02 January 2018 India Standard Time
E, dd MMM yyyy HH:mm:ss zTue, 02 Jan 2018 18:07:59 IST
模式 结果
MM / dd / yyyy 01/02/2018
dd-M-yyyy hh:mm:ss 02-1-2018 06:07:59
dd MMMM yyyy 2018年1月2日
dd MMMM yyyy zzzz 2018年1月2日印度标准时间
E,dd MMM yyyy HH:mm:ss z 星期二,2018年1月2日18:07:59 IST

That’s all for java SimpleDateFormat example for date formatting and parsing string to date in java programs.

这就是java SimpleDateFormat示例的全部内容,这些示例用于在Java程序中进行日期格式设置和将字符串解析为日期。

Reference: SimpleDateFormat API Doc, DateFormat API Doc

参考: SimpleDateFormat API DocDateFormat API Doc

翻译自: https://www.journaldev.com/17899/java-simpledateformat-java-date-format

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值