Tag: time; date; Java; dateformat
This afternoon, I was idle, and suddenly saw some usage of the time class. I felt that several classes were a bit complicated. I would like to summarize them:
(If something is wrong, please do not mislead beginners. Thanks) 650) This. width = 650; "src =" http://img.baidu.com/hi/jx2/j_0057.gif "alt =" j_0057.gif "/>
The specific classes mainly include:
Date calendar simpledateformat
1. Date class
Most of the methods in this class are outdated.
This can be used as follows:
Date date = new Date();System.out.println(date.toString());
You can print the current time: Mon Jun 30 12:50:09 CST 2014
But the format is not very nice. It doesn't matter. I'll wear nice clothes for them later.
One method is not outdated:
Date date = new Date();System.out.println(date.geTime());
This method returnsDateThe number of milliseconds the object represents.
It can be used to test the total number of milliseconds for a program to run, or the number of seconds.
You can also directly use:
long now = System.currentTimeMillis();
This method returns the same result as the above method.
2. Calendar abstract class
This class is very powerful, basically replacing the previous date class.Calendar
Provides a class MethodgetInstance()
To obtain a common object of this type.Calendar
OfgetInstance
Method returnsCalendar
Object whose calendar field has been initialized by the current date and time. Therefore, this class cannot be new and can only be passed through
Calendar now = Calendar.getInstance();
To instantiate an object.
Let's take a look at an example to understand how to use a class:
Import Java. util. calendar; public class testdate {public static void main (string [] ARGs) {// obtain the current date calendar = calendar ar. getinstance (); int year = calendar. get (calendar. year); system. out. println ("year =" + year); // 2014 int month = calendar. get (calendar. month); // obtain the current month, 0 .. 11 indicates jan .. decsystem. out. println ("month =" + month); // 5 // obtain the day of the month. Int day = calendar. get (calendar. day_of_month); system. out. println ("day of month =" + day); // 30 // obtain the number of days in the week, Sun = 1, MON = 2... sta = 7int day_of_week = calendar. get (calendar. day_of_week); system. out. println ("day of week =" + day_of_week); // 1 // when the current time is 12, the value is calendar. hourint hour12 = calendar. get (calendar. hour); system. out. println ("hour 12 =" + hour12); // 1 // obtain AM, PM, 0 = am, 1 = pmint ampm = calendar. get (calendar. am_pm); // 1system. out. println ("am PM =" + ampm); // 0 = am, 1 = PM // calendar is generated when the current time is 24. hour_of_dayint hour24 = calendar. get (calendar. hour_of_day); system. out. println ("hour 24 =" + hour24); // 13 complete display time: String date = calendar ar. get (calendar. year) + "-" + calendar. get (calendar. month) + "-" + calendar. get (calendar. day_of_month) + "" + calendar. get (calendar. hour_of_day) + ":" + calendar. get (calendar. minute) + ":" + calendar. get (calendar. second); system. out. println (date );}}
Well, the last "complete real time" seems a bit complicated, so I generally don't need this. There are still better methods.
There are a lot of member variables in the calendar class. The above program only lists some of them. Others can refer to Java APIs. As for the APIS, you can download them from this site, with all the resources available.
3. simpledateformat class
Let's talk about simpledateformat. Let's talk about its father:
DateFormat。
We can usegetTimeInstance
,getDateInstance
OrgetDateTimeInstance
To create a date-time formatter. Every such method can return a date/time formatter initialized in the default format.
The following uses dateformat to return a time or date format:
Returns a default time format:
DateFormat format = DateFormat.getTimeInstance();Date date = new Date();System.out.println(format.format(date)); //14:05:11
Returns a default date format:
DateFormat format = DateFormat.getDateInstance();Date date = new Date();System.out.println(format.format(date)); //2014-6-30
Returns a default date-time format:
DateFormat format = DateFormat.getDateTimeInstance();Date date = new Date();System.out.println(format.format(date)); //2014-6-30 14:08:08
Simpledateformat is used to format a date or time more freely.
SimpleDateFormat
Format and parse a date in a language environment-related manner. It allows formatting (date-> text), parsing (text-> date), and normalization.
The date and time formats are as follows:Date and Time ModesString.
The following pattern letters are defined (all other characters‘A‘
To‘Z‘
And‘a‘
To‘z‘
Are retained ):
Letter |
Date or time element |
Indicates |
Example |
G |
Era identifier |
Text |
AD |
y |
Year |
Year |
1996 ;96 |
M |
Month in Year |
Month |
July ;Jul ;07 |
w |
Week in Year |
Number |
27 |
W |
The number of weeks in the month |
Number |
2 |
D |
Days in years |
Number |
189 |
d |
Days in a month |
Number |
10 |
F |
Week in the month |
Number |
2 |
E |
Days in a week |
Text |
Tuesday ;Tue |
a |
AM/PM mark |
Text |
PM |
H |
Hours in a day (0-23) |
Number |
0 |
k |
Hours per day (1-24) |
Number |
24 |
K |
Hours in AM/PM (0-11) |
Number |
0 |
h |
Hours in AM/PM (1-12) |
Number |
12 |
m |
Minutes in an hour |
Number |
30 |
s |
Seconds in minutes |
Number |
55 |
S |
Milliseconds |
Number |
978 |
z |
Time Zone |
General Time Zone |
Pacific Standard Time ;PST ;GMT-08:00 |
Z |
Time Zone |
RFC 822 Time Zone |
-0800 |
For example:
Date date = new Date();SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");SimpleDateFormat format2 = new SimpleDateFormat("yyyy/MM/dd");System.out.println(format1.format(date)); // 2014-06-30System.out.println(format2.format(date)); // 2014/06/30
The "-" in the string is ignored during formatting, and the output is in the format specified by you.
It's much easier than the one above.
Another example:
Simpledateformat format3 = new simpledateformat ("HH: mm: SS"); // 14: 30: 32 simpledateformat format4 = new simpledateformat ("HH: mm: ss "); // 14:30:32 pm
Format the date object with format3.format (date. Make it output in the specified format.
If you want to see the format we use, there is a way to help us implement it, that is
System.out.println(format3.toPattern()); // HH:mm:ss
It's easy...
I just briefly introduced some simple usage to meet our general requirements. I will discuss it in detail later.
Hope to help you.
This article is from the "Focus on Java, Linux technology" blog, please be sure to keep this source http://wuqinglong.blog.51cto.com/9087037/1432423