本文共 9452 字,大约阅读时间需要 31 分钟。
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 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,第二个参数是语言环境。
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:01
: 11:03:01
: 11:03:01
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 () 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 can be created using the SimpleDateFormat constructor, the constructor is a parametrised constructor and needs a pattern as the parameter.
可以使用SimpleDateFormat构造函数创建SimpleDateFormat,该构造函数是参数化的构造函数,并且需要模式作为参数。
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”。
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.
在上面的示例中,模式是时间模式,当前时间的格式化是基于该模式进行的。
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日。
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 Pattern | Date or Time component | Examples |
---|---|---|
G | Era designator | AD |
y | Year | 2018(yyyy),18(yy) |
M | Month in year | July(MMMM), Jul(MMM), 07(MM) |
w | Results in week in year | 16 |
W | Results in week in month | 3 |
D | Gives the day count in the year | 266 |
d | Day of the month | 09(dd), 9(d) |
F | Day of the week in month | 4 |
E | Day name in the week | Tuesday, Tue |
u | Day number of week where 1 represents Monday, 2 represents Tuesday and so on | 2 |
a | AM or PM marker | AM |
H | Hour in the day (0-23) | 12 |
k | Hour in the day (1-24) | 23 |
K | Hour in am/pm for 12 hour format (0-11) | 0 |
h | Hour in am/pm for 12 hour format (1-12) | 12 |
m | Minute in the hour | 59 |
s | Second in the minute | 35 |
S | Millisecond in the minute | 978 |
z | Timezone | Pacific Standard Time; PST; GMT-08:00 |
Z | Timezone offset in hours (RFC pattern) | -0800 |
X | Timezone 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)的结果将有所不同。
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的各个方面。 现在让我们看一些日期和时间格式不同的示例。
Pattern | Result |
---|---|
MM/dd/yyyy | 01/02/2018 |
dd-M-yyyy hh:mm:ss | 02-1-2018 06:07:59 |
dd MMMM yyyy | 02 January 2018 |
dd MMMM yyyy zzzz | 02 January 2018 India Standard Time |
E, dd MMM yyyy HH:mm:ss z | Tue, 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: ,
参考: ,
翻译自:
转载地址:http://tumzd.baihongyu.com/