API test date and time

API Type Date time before 1.jdk 8

①System class with currentTimeMillis (); 
②java.util.Date and subclasses a java.sql.Date
③SimpleDateFormat
④Calendar

2.SimpleDateFormat use

SimpleDateFormat use, SimpleDateFormat to date Date format and parsing of class 
1. format, date ---> string
2. parsing, formatting the inverse process, string ---> Date
@Test 
   public void testSimpleDateFormat () throws a ParseException { 
     // instantiate the SimpleDateFormat 
      the SimpleDateFormat the SimpleDateFormat SDF new new = (); 

      // --- formatted date> string 
      a Date = new new DATE a Date (); 
      System.out.println (DATE) ; // Thu Jul 18 21:41:57 CST 2019 
      String format = sdf.format (DATE); 
      System.out.println (format); // 19-7-18 9:41 pm 
       
      // parse 
       String str = " 19-10-18 4:17 pm "; 
       a Date date1 = sdf.parse (str); 
       System.out.println (date1); // Fri Oct 18 16:17:00 CST 2019 

       // ****** ******* formatting and parsing manner specified: it invokes the parameterized constructor ********  
       // the SimpleDateFormat the SimpleDateFormat new new SDF-I = ( "GGG yyyyy.MMMMM.dd HH: mm AAA" );
       the SimpleDateFormat the SimpleDateFormat new new SDF-I = ( "the mM-dd-YYYY HH: mm: SS");
       Sdf1.format format1 = String (DATE); 
       System.out.println (format1); 2019-07-18 09:41:57 // 

       // parse: string must conform to the format requirement SimpleDateFormat identified (by construction parameters reflect), otherwise it will throw an exception 
       a Date that represented by DATE2 = sdf1.parse ( "2019-07-18 04:30:05"); 
       System.out.println (that represented by DATE2); // Thu Jul 18 04:30:05 CST 2019 

   }

3. Exercise 1: string "2019-07-18" into java.sql.Date

@Test
public void test1() throws ParseException {
     String birth="2019-07-18";
     SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
     Date date=sdf1.parse(birth);
     System.out.println(date); //Thu Jul 18 00:00:00 CST 2019
        java.sql.Date birthDate = new java.sql.Date(date.getTime());
        System.out.println(birthDate); //2019-07-18
 }  

4. Exercise Two: fisherman, "three days of fishing two days of drying" 1990-01-01 From the beginning, in a certain period after he was still fishing nets drying? For example 2020-09-08

Ideas: conversion of a few milliseconds ( one day = 24 hour = 24 * 60 minute = Measured 24 * 60 * 60 second = 24 * 60 * 60 * 1000 ms )

Specific code implementation:
@Test
 public void test2() throws ParseException {
     SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy-MM-dd");
     Date date1 =sdf1.parse("1990-01-01");
     //System.out.println(date1.getTime());  //631123200000
     SimpleDateFormat sdf2 =new SimpleDateFormat("yyyy-MM-dd");
     Date date2 =sdf1.parse("2020-09-08");
     //System.out.println(date2.getTime());  //1599494400000
     //计算总天数
     long date= (date2.getTime() - date1.getTime())/(1000 * 60 * 60 *24);
     //System.out.println(date);  //11208

     long l=date%5;
     //System.out.println(l);  //3
     if(l==1 || l==2 ||l==3){
         System.out.println("渔夫打渔");
     }else {
         System.out.println ( "Fisherman drying net"); 
     } 
 }

Use 5.Calendar class calendar

Note that when you get in January, January is 0, February is 1, and so, on December 11, when acquiring the week, Sunday is 1, Monday is 2, and so on, Saturday is 7

@Test 
    public void testCalendar () { 
      //. 1. Instantiation, which is abstract and can not be directly instantiated 
        // a manner to create subclasses (the GregorianCalendar) object 
        // second approach, which call the static method getInstance () 
        = Calendar.getInstance Calendar Calendar (); 
        System.out.println (calendar.getClass ()); subclasses of the object is equivalent // class java.util.GregorianCalendar created 
        . @ 2 conventional method 
        // GET () 
        int day = calendar.get (Calendar.DAY_OF_MONTH); // get today is the first few days of the month 
        System.out.println (Day); // 18 
        System.out.println (Calendar.get (the Calendar.DAY_OF_WEEK)) ;. 5 // 
        // SET () 
        calendar.set (Calendar.DAY_OF_MONTH, 23 is); 
        Day = Calendar.get (Calendar.DAY_OF_MONTH);  
        System.out.println (Day); // 23 is
        // the Add ()
        calendar.add(Calendar.DAY_OF_MONTH,3);//修改的是calendar本身
        day=calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(day); //26
        calendar.add(Calendar.DAY_OF_MONTH,-3);
        day=calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(day); //23
        //getTime()  日历类 ---> Date
        Date date=calendar.getTime();
        System.out.println(date); //Tue Jul 23 21:50:21 CST 2019
        //setTime() Date --->日历类
        Date date1=new Date();
        calendar.setTime(date1);
        day=calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(day); //18
   }

6.jdk 8 new date and time API

To increase the reason for the new date and time API:

① variability, date, time of such classes should be immutable 
② there is an offset of, Date of the year from the beginning of 1900, a month starting from 0
③ formatting, formatting is only useful for Date, Calendar then No
④ not thread-safe, and so can not handle leap seconds
offset can cause incorrect code to achieve more specific see below:
 @Test
    public void testDate(){
       
        Date date1=new Date(2020,9,8);
        System.out.println(date1);  //Fri Oct 08 00:00:00 CST 3920
        Date date2=new Date(2020-1900,9,8);
        System.out.println(date2); //Thu Oct 08 00:00:00 CST 2020

    }

 jdk 8 new API --LocalDate, LocalTime, LocalDateTime etc.

LocalDate, LocalTime, LocalDateTime using the following categories:
@Test 
    public void test1 () { 
        // now (): Get the current date, time, date + time 
        the LocalDate localDate LocalDate.now = (); 
        System.out.println (localDate); // 2019-07-18 
        LocalTime = LocalTime.now localTime, (); 
        System.out.println (localTime,); //20:34:18.986 
        the LocalDateTime LocalDateTime.now LocalDateTime = (); 
        System.out.println (LocalDateTime); // 2019-07-18T20: 34 is: 18.986 

        // of (): set the specified year, month, day, hour, minutes, seconds, do not offset 
        the LocalDateTime localDateTime1 = LocalDateTime.of (2020,10,19,20,33,40); 
        the System. Out.println (localDateTime1); // 2020-10-19T20: 33 is: 40 

        // getXXX () Gets related properties  
        System.out.println (localDateTime.getDayOfMonth ()); // 18 this is the first of several month day
        System.out.println (localDateTime.getDayOfWeek ()); // THURSDAY currently several weeks week
        System.out.println (localDateTime.getMonth ()); // JULY currently few months 
        System.out.println (localDateTime.getMonthValue ()); // 7 digital form the current month 
        System.out.println (localDateTime.getHour ()); // current 20 hours 
        System.out.println (localDateTime.getMinute ()); // 39 current minute 
        System.out.println (localDateTime.getSecond ()); // 35 seconds current 

        / / reflected immutability, modify return value itself does not modify 
        // withxxx (): set attributes associated 
        the localDate localDate1 = localDate.withDayOfMonth (26 is); 
        System.out.println (localDate); // 2019-07-18 
        System.out.println (localDate1); // 2019-07-26

        Localdateti to localdatetime2 = is localdatetime.withho (4); 
        System.out.println (localdateti OMe); //2019-07-18T20:49:31.531 
        System.out.println (localdatetime2); //2019-07-18T04:49:31.531 

       //设置加
        localdateti to localdatetime3 = localdatetime.plusmonths (3); 
        System.out.println (localdateti OMe); //2019-07-18T20:53:55.526 
        System.out.println (localdatetime3); //2019-10-18T20:53:55.526 

        //设置减
        localdateti to localdatetime4 = localdatetime.minusdays (5); 
        System.out.println (localdateti OMe); //2019-07-18T20:56:12.180 
        System.out.println (localdatetime4); //2019-07-13T20:56:12.180 
    }
Date and time to be extra careful when using.

Guess you like

Origin www.cnblogs.com/gujun1998/p/11210297.html