1 Introduction
article begins, let's look at a short code, this code can run it?
1 package simpledate;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6
7 public class Format {
8
9 public static void main(String[] args) throws ParseException {
10 String date = "2012-12-12";
11 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
12 Date end = simpleDateFormat.parse(date.trim());
13
14 }
15
16 }
answer is no, this code will be reported java.text.ParseException error, which I encountered in the project yesterday a small problem, let us read the following explanations come back to see why there is this error.
1.1 Introduction
SimpleDateFormat inherited DateFormat, essentially it is a date through a pattern to control the output style tools.
1.2 Constructor
its constructor method has four, generally we use the first two on it.
If you do not want the time specified in the constructor class style, you can also select the first constructor, but you must use it before using the specified style.
1 public static void main(String[] args) throws ParseException {
2 SimpleDateFormat format5 = new SimpleDateFormat();
3 Date date = new Date();
4 format5.applyPattern("yyyy-MM-dd");
5 System.out.println(format5.format(date));
6 }
2 used two methods: parse () and format ()
2.1 string to date with the parse method
parse () is simple to use as follows:
1) construct a SimpleDateFormat object, the constructor argument passed when 'yyyy-MM-dd' representatives in date-01-01 'The date format
2) holds a string to be transferred
3) Use the SimpleDateFormat parse () method
1 public static void main(String[] args) throws ParseException {
2 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
3 String s = "2013-01-01";
4 Date date = simpleDateFormat.parse(s);
5 }
2.2 date to a string using the format method
format () is simple to use as follows:
1) construct a SimpleDateFormat object, the constructor argument passed when 'yyyy-MM-dd' representatives in date-01-01 'The date format
2) held by a date to be transferred object
3) Use the SimpleDateFormat format () method
1 public static void main(String[] args) throws ParseException {
2 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
3 Date date = new Date();
4 String s = simpleDateFormat.format(date);
5 }
3 constructor parameters when SimpleDateFormat Pattern
Pattern of the Chinese meaning as a model, samples, where we can be understood as the date for the provisions of the template.
这样我们就很好的理解了前言部分的代码为什么会出错了,因为它在使用parse()转换的时候没按常理出牌,待转换的String对象和规定的模板完全不同,即"2012-12-12"和"yyyy-MM-dd HH:mm:ss"是不同的样式
Pattern provides? Java API gives the following specifications:
Letter | Date or Time Component | Presentation | Examples |
---|---|---|---|
G | Era designator | Text | AD |
y | Year | Year | 1996 ; 96 |
M | Month in year | Month | July ; Jul ; 07 |
w | Week in year | Number | 27 |
W | Week in month | Number | 2 |
D | Day in year | Number | 189 |
d | Day in month | Number | 10 |
F | Day of week in month | Number | 2 |
E | Day in week | Text | Tuesday ; Tue |
a | Am / pm marker | Text | PM |
H | Hour in day (0-23) | Number | 0 |
k | Hour in day (1-24) | Number | 24 |
K | Hour in am / pm (0-11) | Number | 0 |
h | Hour in am / pm (1-12) | Number | 12 |
m | Minute in hour | Number | 30 |
s | Second in minute | Number | 55 |
S | Millisecond | Number | 978 |
z | Time zone | General time zone | Pacific Standard Time ; PST ; GMT-08: 00 |
Z | Time zone | RFC 822 time zone | -0800 |
Examples of the column is the sample values (U.S. standard), Java API also gives some examples of between dates and strings, but also the U.S. standards.
Date and Time Pattern | Result |
---|---|
"yyyy.MM.dd G 'at' HH: mm: ss z" | 2001.07.04 AD at 12:08:56 PDT |
"EEE, MMM d,'' yy" | Wed, Jul 4, '01 |
"h: mm a" | 12:08 PM |
"hh 'o'' clock' a, zzzz" | 12 o'clock PM, Pacific Daylight Time |
"K: mm a, z" | 0:08 PM, PDT |
"yyyyy.MMMMM.dd GGG hh: mm aaa" | 02001.July.04 AD 12:08 PM |
"EEE, d MMM yyyy HH: mm: ss Z" | Wed, 4 Jul 2001 12:08:56 -0700 |
"yyMMddHHmmssZ" | 010704120856-0700 |
"yyyy-MM-dd'T'HH: mm: ss.SSSZ" | 2001-07-04T12 :08:56.235-0700 |
Perhaps you are more concerned about is that as China's developers some common date formats which, wait, then see below.
Date and Time Pattern | Result |
---|---|
"yyyy-MM-dd HH: mm: ss E" | 2013-07-04 14:23:55 Thursday |
"yyyy years at MM-dd HH mm minutes ss seconds when E" | 2013 年 07 月 04 日 14 时 24 分 44 秒 Thursday |
" yy / MM / dd HH: mm: ss E " | 13/07/04 14:25:33 Thursday |
"D-day of the first year, the first year w weeks in January in the first week of W, k in the day when, in the first time zone Z '< / span> | the first 185 days of the year, the year's first 27 weeks, in January the first one week, the day 14:00, in the first time zone +0800 |
sample code is as follows:
1 public static void main(String[] args) throws ParseException {
2 SimpleDateFormat format1 = new SimpleDateFormat("yyy-MM-dd HH:mm:ss E");
3 SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E");
4 SimpleDateFormat format3 = new SimpleDateFormat("yy/MM/dd HH:mm:ss E");
5 SimpleDateFormat format4 = new SimpleDateFormat("一年中的第D天,一年中的第w个星期,一月中的第W个星期,在一天中k时,在第Z时区");
6 Date date = new Date();
7 System.out.println(format1.format(date));
8 System.out.println(format2.format(date));
9 System.out.println(format3.format(date));
10 System.out.println(format4.format(date));
11 }
没有评论:
发表评论