10 Date Detailed

A, Date type of initialization

1, Date (int year, int month, int date); direct writing year is not the correct result. Because the java Date from 1900 began to count, so long as the first argument in front of the fill from 1900 after how many years is the year to get what you want. May need to minus 1, the date can be directly inserted. This method is relatively small, the second method is used.

2, this method is conform to a specific format, such as yyyy-MM-dd, converted into a string data type Date. First, a definition of the object type Date Date date = null; redefinition conform to the format of a String String String dateStr = "2010-9-10"; this string split String [] dateDivide = dateStr.split ( "-"); the date taken separately, assigned Calendar, Calendar used in the getTime (); again assigned acquisition date date.

E.g:

IF (dateDivide.length ==. 3 ) { 
    int year = the Integer.parseInt (dateDivide [0] .trim ()); // strips spaces 
     int month The = the Integer.parseInt (dateDivide [. 1 ] .trim ()); 
      int Day the Integer.parseInt = (dateDivide [2 ] .trim ()); 
     calendar C = Calendar.getInstance (); // get a calendar instance 
     c.set (year, month the-. 1, Day); // set the calendar date 
     = DATE c.getTime (); 
}

 

Second, the type of comparative Date

DATE a;
DATE b;
Suppose now that you have instantiated a and b
a.after (b) returns a boolean, if a time (not equal to) after b return to true
b.before (a) returns a boolean, b returns true if the time prior to a (not including equal to)
a.equals (b) returns a boolean, time if a and b are equal returns true

Third, the usual type of manipulation functions Date

// 1. Calculate the maximum number of days of a month 
Calendar Time = Calendar.getInstance (); 
time.clear (); 
time.set (Calendar.YEAR, year); // year as int 
time.set (the Calendar.MONTH, i - 1); // Note, Calendar object defaults in January is 0 
int day = time.getActualMaximum (Calendar.DAY_OF_MONTH); // number of days this month
 // Note: before using the set method, you must first clear look, otherwise many information is inherited from the current time
 // conversion 2.Calendar and Date of
 // (. 1) into Calendar Calendar Date 
CAL = Calendar.getInstance (); 
Date DATE = cal.getTime ();
 // (2) Date into Calendar 
a Date DATE = new new a Date ();
CAL Calendar  =Calendar.getInstance (); 
cal.setTime (DATE); 
// 3. formatted output date and time (the use of more) 
a Date DATE = new new a Date (); 
the SimpleDateFormat DF = new new the SimpleDateFormat ( "YYYY-MM- HH dd: mm: SS " ); 
String Time = df.format (DATE); 
System.out.println (Time); 
// 4. calculation of weeks of the year
 // (1) is a calculated one day the first few weeks of the 
Calendar CAL = Calendar.getInstance (); 
cal.set (Calendar.YEAR, 2006 ); 
cal.set (the Calendar.MONTH, . 8 ); 
cal.set (Calendar.DAY_OF_MONTH, . 3 );
 int weekno =cal.get (Calendar.WEEK_OF_YEAR);
 // (2) weeks of the year is calculated is the date 
the SimpleDateFormat DF = new new the SimpleDateFormat ( "the MM-dd-YYYY" ); 
Calendar CAL = Calendar.getInstance (); 
CAL the .set (Calendar.YEAR, 2006 ); 
cal.set (Calendar.WEEK_OF_YEAR, 1 ); 
cal.set (the Calendar.DAY_OF_WEEK, Calendar.MONDAY,); 
System.out.println (df.format (cal.getTime ()) ); // output: 2006-01-02
 // 5.add () and roll () usage (less commonly)
 // (. 1) the Add () method 
SimpleDateFormat df = newSimpleDateFormat ( "yyyy- MM-dd" ); 
Calendar CAL = Calendar.getInstance (); 
cal.set (Calendar.YEAR, 2006);
cal.set(Calendar.MONTH, 8);
cal.set(Calendar.DAY_OF_MONTH, 3);
cal.add(Calendar.DATE, -4);
Date date = cal.getTime();
System.out.println(df.format(date));
cal.add(Calendar.DATE, 4);
date = cal.getTime();
System.out.println(df.format(date));//输出:2006-08-30 2006-09-03
//(2)roll方法
cal.set(Calendar.YEAR, 2006);
cal.set(Calendar.MONTH,8);
cal.set(Calendar.DAY_OF_MONTH,3);
cal.roll (Calendar.DATE, -4 );
DATE = cal.getTime (); 
System.out.println (df.format (DATE)); 
cal.roll (Calendar.DATE, . 4 ); 
DATE = cal.getTime () ; 
System.out.println (df.format (DATE)); // output: 2006-09-292006-09-03
 // visible, roll () method loops within this month, the general use of add () method;

It is reproduced

https://www.cnblogs.com/0717GG/p/7087663.html

 

Guess you like

Origin www.cnblogs.com/xuwangqi/p/11205005.html