I don't know what it was like when you were in college, and when I was in college, the teacher would tell us the first lesson of the course, how much knowledge would he put in the book? Where is it? Where did you get your exams? So many of the lessons I learned are just fur-based things. Cause now have to spend a lot of effort to fill the previously falling knowledge points. This date class is a very important point of knowledge, often used in the work of it, this thing is not difficult, but for my own, often forget this ghost, so I have to comb it from scratch and recorded, for later use.
First, we introduce the method of constructing the date class:
1. Date () Creates a Date object that gets the current date and time.
2. Date (long date) creates a date object of the specified parameter, where the parameter date represents the deviation from the 00:00:00 of Greenwich Mean Time of January 1, 1970. (popularly known as timestamps)
There are three more common methods for this class:
1. Before (date when) tests whether the date object is earlier than the specified date. Returns True if the earlier
2. After (Date) is the opposite of the above.
3. Equals (Object obj) compares the equality of two dates and returns true for equality
In fact, in the case of using the date class, we are generally working with the SimpleDateFormat class. The SimpleDateFormat class is primarily used to format dates and times. Note that it is an abstract class that cannot be instantiated. So we use its subclasses to format the date time. Some of these letters mean the meaning we need to remember: (case sensitive)
G-Indicates before and after A.D.
Y-year
M-month
W-The week ordinal of a year
W-The week of January
D-the day ordinal of a year
D-the day of January
F-The first few weeks of January
E-Days of the week
A-morning or afternoon 3.
H-24-hour system (00-23)
H-12-hour System (01-12)
K-12-hour system (01-11)
K-24-hour System (01-24)
M-Number of minutes in hours
S-Number of seconds in minutes
S-Number of milliseconds
Z-time Zone
There are three common methods:
1.parse (string source) interprets a string containing a date as the date type
2.format (date date) converts a date type to the specified time display format
3.toPattern () returns a pattern string representing the date format
Example:
Date newdate = new Date ();
LOG.D (TAG, "Present time:" +newdate);
SimpleDateFormat defaultsdf = new SimpleDateFormat ();
LOG.D (TAG, "The default mode is:" +defaultsdf.topattern ());
LOG.D (TAG, "The default mode datetime is:" +defaultsdf.format (newdate));
/* Format time */
String[] pattern = {"Yyyy-mm-dd HH:mm:ss. SSS "," yyyy. Mm.dd G ' at ' HH:mm:ss z ', ' ee,mmm D, ' yy ', ' h:mm a ', ' HH ' o ' clock ' a,zzzz '};
SimpleDateFormat patternsdf = null;
for (int i = 0, i < pattern.length; i++) {
PATTERNSDF = new SimpleDateFormat (Pattern[i]);
LOG.D (TAG, "The Time after formatting:" +patternsdf.format (newdate));
}
Date date = new Date (6000);
LOG.D (TAG, "the time corresponding to Greenwich Mean time is:" +date);
The result of the final output:
Current time: Mon May 23:39:30 CST 2016
The default mode is: Yy-m-d ah:mm
The default mode date time is: 16-5-2 11:39
Time after format: 2016-05-02 23:39:30.876
Time after format: 2016.05.02 A.D. at 23:39:30 CST
Time after format: Monday, May 2, ' 16
The time after formatting is: 11:39 pm
The time after format is: o ' clock PM, China Standard Time
Time corresponding to Greenwich Mean time is: Thu Jan 08:00:06 CST 1970
Well, this is enough detail, at least to meet the needs of our work.
Date class detailed