Time Date Date Calendar time string SimpleDateFormat

1.Date

/ ** 
 * java.util.Date 
 each instance of a Date * is used to represent a precise time (millisecond accuracy) 
 * internal maintains a long value, since January 1, 1970 the value represented by 00:00:00 Date represents the time between the current elapsed milliseconds. 
 * Design flaw time zone, Date, and therefore most of the developed methods are declared obsolete in the future and then should no longer be used. 
 * / 
Public  class DateDemo {
     public  static  void main (String [] args) { 
        Date DATE = new new Date (); 
        System.out.println (DATE); 
        / * 
         * Long the getTime () 
         * Date Gets the milliseconds maintained inside 
         * / 
        Long Time = date.getTime (); 
        System.out.println (Time);     
        date.setTime ( 0  );
        System.out.println (DATE);
    }
}

2.SimpleDateFormat

/ ** 
 * the java.text.SimpleDateFormat 
 * may be between the class String and Date are mutually converted according to the specified format. 
 * / 
Public  class SimpleDateFormatDemo1 {
     public  static  void main (String [] args) { 
        a Date now = new new a Date (); 
        System.out.println (now);     
        / * 
         * 2019-08-21 10:35:22 
         * YYYY- dd HH-the mM: mm: SS 
         * / 
        the SimpleDateFormat SDF = new new the SimpleDateFormat ( "the mM-dd-YYYY HH: mm: SS E a" );
         / * 
         * the format String (DATE Date) 
         * given Date object according to the current SDF specified date into a string format 
         * /
        String line = sdf.format(now);
        System.out.println(line);
    }
}
/ ** 
 * parse a string Date object 
 * / 
public  class SimpleDateFormatDemo2 {
     public  static  void main (String [] args) throws a ParseException { 
        String STR = "2008-08-08 20:08:08" ;     
        the SimpleDateFormat SDF = new new the SimpleDateFormat ( "the mM-dd-YYYY HH: mm: SS"     );         
        a Date DATE = sdf.parse (STR); 
        System.out.println (DATE);         
    } 
}
/ ** 
 * When the program starts, requires users to enter their date of birth in the format: yyyy-MM-dd 
 * then undergo a process of computing output to date a total of how much living 
 * days. And then outputs born 10,000 days of the anniversary of the day is the format of the output of the above. 
 * / 
Public  class the Test {
     public  static  void main (String [] args) throws Exception { 
        Scanner Scanner = new new Scanner (System.in); 
        System.out.println ( "Please enter your birthday:" ); 
        String birthStr = Scanner .nextLine ();         
        the SimpleDateFormat SDF = new new the SimpleDateFormat ( "the MM-dd-YYYY" ); 
        a Date Birth = sdf.parse (birthStr);
        Now DATE = new new DATE ();        
         Long Time = now.getTime () - birth.getTime (); 
        Time = Time / 1000/60/60/24- ; 
        System.out.println ( "Congratulations, you've lived" + time + "Heaven, keep it!" );         
        Time = birth.getTime () + 1000 * 60 * 60 * * 24-10000L ;                 
        a Date DATE = new new a Date (Time);         
        String Line = sdf.format (DATE); 
        System.out .println ( "You were born 10,000 days of the anniversary is:" + 
        Line);             
    } 
}

3.Calendar

/ ** 
 * Calendar category the java.util.Calendar 
 * Calendar is an abstract class, the method provides a series of calendar-based operation time. 
 * It provides static methods can be used: getInstance to get a system where the region currently applicable implementation class, get back in most areas 
 * is GregorianCalendar, namely: Gregorian implementation class 
 * / 
public  class CalendarDemo1 {
     public  static  void main (String [ ] args) { 
        Calendar Calendar = Calendar.getInstance (); 
        System.out.println (Calendar);         
        / * 
         methods * Calendar provided: 
         * date the getTime () 
         * Calendar date of the current may be represented by a return to form date instance. 
         * / 
        A Date DATE = calendar.getTime (); 
        System.out.println (DATE);     
        / *
         The setTime void * (DATE Date) 
         * Calendar adjusting the current so as to show a given date Date indicated 
         * / 
        calendar.setTime (DATE);         
    } 
}
/ ** 
 * GET int (int Field) 
 * Get the current value of the specified time Calendar date components corresponding represented. 
 * Component represents a different time, without having to remember a different integers, Calendar provides a number of corresponding constants 
 * / 
public  class CalendarDemo2 {
     public  static  void main (String [] args) { 
        Calendar Calendar = Calendar.getInstance ();        
         int = year Calendar.get (Calendar.YEAR);
         / * 
         * Note from month 0, namely: 0 for January, February 1 represents ... 
         * / 
        int month the Calendar.get = (the Calendar.MONTH) + 1'd ;
         / * 
         * and "day" time component associated with a: 
         * DAY_OF_MONTH is: month day 
         * DAY_OF_WEEK: days of the week
         * DAY_OF_YEAR:年中的天
         * DATE:月中的天,与DAY_OF_MONTH一致
         */
        int day = calendar.get(Calendar.DATE);
        System.out.println(year+"-"+month+"-"+day);        
        int h = calendar.get(Calendar.HOUR_OF_DAY);
        int m = calendar.get(Calendar.MINUTE);
        int s = calendar.get(Calendar.SECOND);
        System.out.println(h+":"+m+":"+s);    
        int days = calendar.get(Calendar.DAY_OF_YEAR);
        System.out.println("今天是今年的第"+days+"天");
        /*
         * According to the US diet, the first day of the week is Sunday 
         * / 
        Days = Calendar.get (the Calendar.DAY_OF_WEEK) -1 ; 
        String [] Data = { "day", "one", "two", "three" , "four", "five", "six" }; 
        System.out.println ( "today is Week" + Data [Days]);    
         // Get the specified maximum allowed time component 
        int D = calendar.getActualMaximum ( Calendar.DAY_OF_YEAR); 
        System.out.println ( "year, a total" + d + "days" );     
    } 
}
/ ** 
 * SET void (Field int, int value) 
 * Calendar specified time adjusted to the given current component value 
 * / 
public  class CalendarDemo3 {
     public  static  void main (String [] args) { 
        Calendar Calendar = Calendar.getInstance () ; 
        System.out.println (calendar.getTime ());     
        // 2008-08-08 20:08:08
         // adjustment for the 2008 
        calendar.set (Calendar.YEAR, 2008 );
         // adjust the August 
        calendar .set (the Calendar.MONTH, Calendar.AUGUST);
         // adjusted number. 8 
        calendar.set (Calendar.DATE,. 8 );
        calendar.set(Calendar.HOUR_OF_DAY, 20);
        calendar.set(Calendar.MINUTE, 8);
        calendar.set(Calendar.SECOND, 8);
        System.out.println(calendar.getTime());    
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
        System.out.println(calendar.getTime());
    }
}
/ ** 
 * void the Add (Field int, int AMOUNT) 
 * plus a predetermined value to a specified time component, if given a negative value is subtracted. 
 * / 
Public  class CalendarDemo4 {
     public  static  void main (String [] args) { 
        Calendar Calendar = Calendar.getInstance (); 
        System.out.println (calendar.getTime ()); 
        / * 
         * See February 3 months and 25 ? after the day of the week Wednesday is the day 
         * / 
        // add three years 
        calendar.add (Calendar.YEAR, 3 ); 
        System.out.println (calendar.getTime ());         
        // add two months 
        calendar.add (the Calendar.MONTH, 2 ); 
        System.out.println (calendar.getTime ());     
        //Plus 25 days 
        Calendar.add (Calendar.DAY_OF_YEAR, 25 ); 
        System.out.println (calendar.getTime ());         
        // adjusted to a week Wednesday 
        calendar.set (the Calendar.DAY_OF_WEEK,. 4 ); 
        the System.out. the println (calendar.getTime ()); 
    } 
}
/ ** 
 * Calculation merchandising Date 
 * After the program starts, the user to enter the date of production of goods: the MM-dd-YYYY 
 * then enter the number of days the shelf life of the product, is an integer. 
 * Then undergo a process of calculation, the output of the merchandising date, format above. 
 * Rules: 
 * Product expired on Wednesday, two weeks before the date for the promotion. 
 * / 
Public  class Test2 {
     public  static  void main (String [] args) throws Exception { 
        Scanner Scanner = new new Scanner (the System.in); 
        System.out.println ( "Please enter the date of production:" ); 
        String STR = Scanner. nextLine (); 
        System.out.println ( "Please enter the number of days of shelf life:" );
         int days =The Integer.parseInt (scanner.nextLine ());     
        the SimpleDateFormat SDF = new new the SimpleDateFormat ( "the MM-dd-YYYY" ); 
        a Date DATE = sdf.parse (STR);    
         // make the production date indicates Calendar 
        Calendar Calendar = Calendar.getInstance ( ); 
        calendar.setTime (dATE);         
        // calculate the expiration date 
        Calendar.add (Calendar.DAY_OF_YEAR, days);
         // adjust to two weeks before Wednesday 
        Calendar.add (Calendar.DAY_OF_YEAR, -14 ); 
        calendar.set ( the Calendar.DAY_OF_WEEK, . 4 );         
        DATE = calendar.getTime (); 
        STR= Sdf.format (DATE); 
        System.out.println ( "Promotion date:" + STR); 
    } 
}

Guess you like

Origin www.cnblogs.com/hello4world/p/12159219.html