Completion time of the object and conversion of strings (DateFormat), (Usage and calendar Calendar)

DateFormat  和 SimpleDateFormat

Example (writing time format)

Package cn.date; 

Import the java.text.DateFormat;
 Import java.text.ParseException;
 Import the java.text.SimpleDateFormat;
 Import java.util.Date; 

public  class Test02 {
     public  static  void main (String [] args) { 
        DateFormat D = new new SimpleDateFormat ( "yyyy-year -MM month -dd day HH: mm: SS"); // set the time format 
        a date S = new new a date ();                                  // current time 
        String str = d.format (S);                             // the time of the object in accordance with format string, the string is converted to
        System.out.println (STR); 
        
        String str2 = "1998-5-20" ; 
        DateFormat dd = new new the SimpleDateFormat ( "the MM-dd-YYYY" );
         the try { 
            a Date SS = dd.parse (str2);                     // The string parsing a string having a fixed format, and give it to the Date object. Note: The format to be consistent 
            System.out.println (SS);    
        } the catch (a ParseException E) {
             // the TODO Auto-Generated Block the catch 
            e.printStackTrace (); 
        }     
        
        
    } 
}

 result

 

 

Calendar (calendar) (date-based) (the date of addition)

Examples

Package cn.date; 

Import the java.util.Calendar;
 Import java.util.Date;
 Import a java.util.GregorianCalendar; 

public  class TestClendar {
     public  static  void main (String [] args) { 
        Calendar C = new new the GregorianCalendar ();     / / Calendar is an abstract class, the object can not be built, to use his subclasses. 
        c.set (2019, 7,15,18,07,36);             // Set the date 
        a Date c.getTime D = ();                         // get the set date, no set value, the return to current time.
//         c.setTime (new new a Date ()); 
        System.out.println (D); 
        
//        System.out.println(c.get(Calendar.YEAR));
        
        //加法
        c.add(Calendar.YEAR, 30);
        Date m=c.getTime();        
        System.out.println(m);
        
    }
}

result

 

Visualization of the calendar (quite interesting)                

 Code Example:

Package cn.date; 

Import the java.text.DateFormat;
 Import java.text.ParseException;
 Import the java.text.SimpleDateFormat;
 Import the java.util.Calendar;
 Import java.util.Date;
 Import a java.util.GregorianCalendar;
 Import Java. util.Scanner; 

public  class the Visual {
     public  static  void main (String [] args) { 
        System.out.println ( "Please enter the date (in the format: 2019-5-6):" ); 
        Scanner Scanner = new new Scanner (the System .in);   // enter the date, the input stream according to the format ------ System.in
        Temp = scanner.nextLine String ();            // return result input to the variable temp.
//         String TEMP = "2019-2-15"; it is hardcoded 
        DateFormat DateFormat = new new the SimpleDateFormat ( "the MM-dd-YYYY" );
         the try { 
            a Date D = dateformat.parse (TEMP);       // string parsing a string having a fixed format, and give it to the Date object. 
            = Calendar Calendar new new the GregorianCalendar (); 
            calendar.setTime (D); 
            int Day = Calendar.get (Calendar.DATE);    // Get the current date 15; 
            
//             System.out.println (Day);            // Print
            calendar.set (Calendar.DATE, 1);     // the date set to 2019-2-1, then looks at his first few days in order to determine its position in the calendar
 //             System.out.println (Calendar.get (the Calendar.DAY_OF_WEEK));    // Print View February 1 is the first few days of this week. 6 shows Friday, Friday is the No. 1 February day
 //             System.out.println (calendar.getActualMaximum (Calendar.DATE)); // maximum number of days of the month 
            int MaxDay = calendar.getActualMaximum (Calendar.DATE);         // the extracted number of days out 
            
            System.out.println ( "day \ t a \ t two \ t three \ t four \ t five \ t six" );
             for ( int I =. 1; I <Calendar.get (Calendar. DAY_OF_WEEK); I ++) {     // print the front space 6 in front of the words to be printed on five spaces
                Of System.out.print ( "\ T" ); 
                
            } 
            // visualization calendar 
            for ( int I =. 1; I <= MaxDay; I ++ ) {
                 IF (I == Day) { 
                    of System.out.print ( "*") ;               // if printing to print the day stars "*" 
                } 
                of System.out.print (I + "\ T" );
                 int W = Calendar.get (the Calendar.DAY_OF_WEEK); // get 2019-2-2 is the first few days of this week 
                IF (w == Calendar.SATURDAY) {                // if this is the seventh day of the week, ie Saturday, then wrap. 
                    System.out.print ( "\ the n-" );
                } 
                Calendar.add (Calendar.DATE, . 1);        // date plus one, becomes 2019-2-2; 
            } 
            
        } the catch (a ParseException E) {
             // the TODO Auto-Generated Block the catch 
            e.printStackTrace (); 
        } 
        
    } 
}

Print results

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/ssxblog/p/11209948.html