I. INTRODUCTION
There are three types of dates commonly used in 1.java (accurate to milliseconds):
a.long--Number of milliseconds
b.date--Date
c.calendar--Calendar, abstract class
There are five ways in which the dates in 2.Java are often:
A. Date Created
B. Date formatting display
C. Conversion of dates (mainly to and from strings)
D. Date middle-aged. Month, day, time, minute, second, week, month, etc. get.
E. Date size comparison. The date is added and reduced.
Two. Creation time:
1.long:
Long now = System.currenttimemillis ();
2.Date:
Date now = new Date (); The current time is assumed by default
DateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String nowstr = Sdf.format (now);
SYSTEM.OUT.PRINTLN ("Formatted date:" + nowstr);
System.out.println ("Date type dates:" + now);
System.out.println ("Current time milliseconds:" + now.gettime ()); Returns the date type
3.Calendar:
Calendar Now1 = Calendar.getinstance (); The current time is assumed by default
Now1.settimezone (Timezone.gettimezone ("gmt+8:00")); Set Time zone GMT
Now1.set (2016, 10, 30, 15, 55, 44); Traps: The month of the calendar is 0~11
Calendar now2 = new GregorianCalendar (); The current time is assumed by default
Calendar now3 = new GregorianCalendar (2016, 10, 30, 15, 55, 44); Traps: The month of the calendar is 0~11
4. Get Time:
System.out.println ("Year:" + Now.get (calendar.year)); return int Type
System.out.println ("Month:" + (Now.get (calendar.month) + 1)); return int type, note: The month starts from zero
System.out.println ("Day:" + now.get (calendar.day_of_month)); return int Type
System.out.println ("Time:" + now.get (calendar.hour_of_day)); return int Type
System.out.println ("min:" + now.get (Calendar.minute)); return int Type
System.out.println ("seconds:" + now.get (Calendar.second)); return int Type
System.out.println ("Date type dates:" + now.gettime ()); Returns the date type
System.out.println ("Current time milliseconds:" + now.gettimeinmillis ()); Returns a long type
5. Note:
The week of A.calendar begins in Sunday with a constant value of 1.
The B.calendar month is starting in January with a constant value of 0.
Three. Format conversion
1.long Convert to date
A.new Date (long);
B.date date = new Date ();
Date.settime (long);
2.Date Convert to Calendar
Calendar cal = Calendar.getinstance ();
Cal.settime (date);
3.Date Convert to Long
Long L = date.gettime ();
4.Calendar Convert to date
Date date = Cal.gettime ();
5.Date Conversion to String
Date now = new Date ();
DateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String nowstr = Sdf.format (now);
6.String Convert to date
DateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Date date = Sdf.parse ("2016-2-17 11:20:50");
Five. Date comparison size
1.Date:
A.before.after.equals
B.date1.compareto (DATE2)///return value less than 0 means less than, equals 0 means equals, greater than 0 means greater than
2.Calendar:
A.before.after.equals
B.cal1.compareto (CAL2)///return value less than 0 means less than, equals 0 means equals, greater than 0 means greater than
3. Calculate the time difference: (Date and calendar are converted to long for comparison)
Long between= (End.gettime ()-begin.gettime ())/1000;//divided by 1000 for conversion into seconds
Long day=between/(24*3600);
Long hour=between% (24*3600)/3600;
Long minute=between%3600/60;
Long second=between%60/60;
System.out.println ("+day+" Days "+hour+" hours "+minute+" minutes "+second+" seconds ");
Six. Date plus minus
1.Calendar cal = Calendar.getinstance ();
Cal.add (Calendar.year,-1);
System.out.println ("Last year is" + cal.get (Calendar.year + "Year"));
2. Last day of the month (Actual: Actual)
Calendar cal = Calendar.getinstance ();
int Maxday=cals.getactualmaximum (calendar.day_of_month);
DateFormat formatter3=new SimpleDateFormat ("yyyy-mm-" +maxday);
System.out.println (Formatter3.format (Cal.gettime ()));
Seven. Attached
1.compareTo is a method of the Compareable interface, which is primarily used to specify the size relationship of the object to be created, the object to implement the Compareable interface, when A.compareto (b) >0, a>b when A.compareto ( b) When <0, A<b, when A.compareto (b) =0, a=b.
2. Select comparable interface or comparator?
Comparable: A class that implements the comparable interface indicates that objects of this class can be compared to each other, and that a collection of such objects can be sorted directly using the Sort method. Using comparable is simple, as long as the object that implements the comparable interface becomes a comparable object directly, but the source code needs to be modified.
Comparator:comparator can be regarded as an implementation of an algorithm, separating the algorithm from the data. The advantage of using comparator is that you do not need to modify the source code, but instead implement a comparator, and when a custom object needs to be compared, pass the comparator and object together in the past can be compared to size.
3. Delay Method:
A.thread.currentthread (). Sleep (500);
B.timer timer=new timer ();//Instantiate Timer class more precise
Timer.schedule (New TimerTask () {
public void Run () {
System.out.println ("Exit");
This.cancel ();
}
},500);
Java Date-time processing