Java's commonly used class two-date time class

1. Date and time AP test before jdk8

1. currentTimeMillis () in the
system
class
2. java.util.Date and Java.sql.Date 3. SimpleDateFormat class 4.Calendar class

1.1. CurrentTimeMillis (): returns the current computer time, the time format is the current computer time and GMT time (Greenwich Mean Time), to obtain the milliseconds starting on January 1, 1970 0: 00: 00: 00 number
1.2, the string 2019-09-08 is converted to java.sql.Date
    public void testExer() throws ParseException {
        String brith =  "2019-09-08";
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf1.parse(brith);
        System.out.println(date);

        java.sql.Date brithDate =   new java.sql.Date(date.getTime());
        System.out.println(brithDate);
    }
1.3, the use of SimpleDateFormat: SimpleDateFormat class for date format analysis

① Two operations
1.1 formatting: date—> string
1.2 parsing: string—> date

② The instantiation of SimpleDateFormat

public void test() throws ParseException {
        //实例化
        SimpleDateFormat sdf = new SimpleDateFormat();
        //格式化:日期  --->字符串
        Date date =new Date();
        System.out.println(date);

        String  format = sdf.format(date);
        System.out.println(format);

//        解析:字符串--->日期

        String str = "20-3-25 下午10:02";
        Date date1 = sdf.parse(str);
        System.out.println(date1);
        System.out.println("**********************************");

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = sdf1.format(date);
        System.out.println(format1);
    }
1.4. Use of Calendar class (abstract class)

① Instantiation
// Method 1: Create a subclass (GregorianCalendar) object
// Method 2: Call its static method getInstance ();

  Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.getClass());

② Common methods

  //get() 获取当前日期的年月日
        int days = calendar.get(calendar.DAY_OF_MONTH);
        System.out.println(days);
        //set() 设置当前日期的年月日
        calendar.set(calendar.DAY_OF_MONTH,22);
        days = calendar.get(calendar.DAY_OF_MONTH);
        System.out.println(days);
        //add()给当前日期添加年月日的时间
        calendar.add(calendar.DAY_OF_MONTH,3);
        days = calendar.get(calendar.DAY_OF_MONTH);
        System.out.println(days);
        calendar.add(calendar.DAY_OF_MONTH,-3);
        days = calendar.get(calendar.DAY_OF_MONTH);
        System.out.println(days);
        //getTime()获取当前时间的时间戳 日历类-- date类
        Date date = calendar.getTime();
        System.out.println(date);
        //setTime() date --- 日历类
        Date date1 = new Date();
        calendar.setTime(date1);
        days = calendar.get(calendar.DAY_OF_MONTH);
        System.out.println(days);

2. API test of jdk8 date and time

2.1, LocalDate, LocalTime, LocalDateTime use local date and time

Note:
1. LocalDateTime is used more frequently than LocalDate and LocalTime
2. Similar to Calender

    public void testDate(){//已过期 deprecated  偏移量的问题
        Date date = new Date(2020 - 1900,9 - 1,8);
        System.out.println(date);
    }
@Test
    public  void  test(){
        //实例化
        //now():获取当前的 日期 时间  日期+时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);

        //of():设置指定的年月日时分秒,没有偏移量
//        LocalDateTime localDateTime1 = LocalDateTime.of(2020,10,26);
//        System.out.println(localDateTime1);

        //getXXX():获取属性值
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getMonth());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getMinute());

        //有返回值,不用修改原来的值,体现不可变性,修改值
        //with():设置相关属性
        LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(22);
        System.out.println(localDateTime1);

        LocalDateTime localDateTime2 = localDateTime.withHour(4);
        System.out.println(localDateTime);
        System.out.println(localDateTime2);

        //不可变性,plus添加相关时间
        LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
        System.out.println(localDateTime);
        System.out.println(localDateTime3);

        //原来的值不变,新增新的值  minus  减去相应的时间
        LocalDateTime localDateTime4 = localDateTime.minusDays(6);
        System.out.println(localDateTime);
        System.out.println(localDateTime4);
    }
2.2. Instant class: An instantaneous point in time, which may be used to record the event timestamp in the application.

1. In Java, starting from 0:00:00 on January 1, 1970, in milliseconds.
2. Similar to java.util.Date class

 @Test
    public void  test1(){
        //now():获取本初子午线对应的标准时间
        Instant instant = Instant.now();
        System.out.println(instant);

        //添加时间的偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        //获取子1970年1月1日0时0分0秒(UTC)开始的毫秒数
        //toEpochMilli()
        long mill = instant.toEpochMilli();
        System.out.println(mill);

        //通过给定的毫秒数,获取Instant实例 -->date类中的getTime()
        Instant instant1 = Instant.ofEpochMilli(1585193930875L);
        System.out.println(instant1);

    }
2.3, DateTimeFormatter: format or parse date and time similar to simpleDateFormat

Method 1: Predefined standard format

  /*
        三种方式
        1.ISO_LOCAL_DATE_TIME
        2.ISO_LOCAL_DATE
        3.ISO_LOCAL_TIME
         */
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化:日期--->字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        String str = formatter.format(localDateTime);
        System.out.println(localDateTime);
        System.out.println(str);//20-3-26 下午8:05

        //解析:字符串-->日期
        TemporalAccessor parse = formatter.parse("2020-03-26T19:58:57.398");
        System.out.println(parse);

Method 2: Localization-related formats: such as ofLocalizedDateTime (), FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT: applicable to LocalDateTime

		  DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
//        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
//        DateTimeFormatter formatter3 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
        //格式化
        String str2 = formatter1.format(localDateTime);
        System.out.println(str2);

        //本地化相关方法 如:ofLocalizedDate()
        // FormatStyle.FULL/ FormatStyle.LONG / FormatStyle.MEDIUM /FormatStyle.SHORT:适用于LocalDate
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        String format = formatter2.format(LocalDate.now());
        System.out.println(format);//2020年3月26日 星期四

Focus: Method 3: Customized format such as ofPattern ("yyyy-MM-dd hh: mm: ss")

 DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化
        String format1 = formatter3.format(LocalDateTime.now());
        System.out.println(format1);
        //解析
        TemporalAccessor parse1 = formatter3.parse("2019-03-26 08:14:19");
        System.out.println(parse1);

One of the commonly used classes in Java-String, StringBuffer, StringBuilder

https://blog.csdn.net/weixin_43244120/article/details/105187804

Three of the commonly used classes of Java-other classes (interfaces)

https://blog.csdn.net/weixin_43244120/article/details/105189069

Published 19 original articles · praised 0 · visits 489

Guess you like

Origin blog.csdn.net/weixin_43244120/article/details/105188644