[JAVA date class detailed explanation | Date\Calendar\LocalDate, LocalTime, LocalDateTime]

first generation date

  1. Date: Accurate to milliseconds, representing a specific moment

  2. SimpleDateFormat: A class for formatting and parsing dates

    For formatting: Date -> Text

    Parsing: text -> date

    normalization

  3. Application example Date_.java

Many methods of the first generation Date are outdated

properties is to look at the get{} and set{} in the method of the class

image-20211202135029040

image-20211202135926469

//使用java.util.Date;
//实现了比较、可串行化、克隆
//默认输出格式是国外的,因此通常需要格式转换
Date d1 = new Date();//获取当前系统时间
Date d2 = new Date(9234567);//通过指定毫秒数得到时间
System.out.println(d1.getTime());//获取某个时间对应的毫秒数//1638424963461
System.out.println("d1 = " + d1);//d1 = Thu Dec 02 14:02:43 CST 2021
System.out.println("d2 = " + d2);//d2 = Thu Jan 01 10:33:54 CST 1970

//这里的字母是规定好的
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
String format = sdf.format(d1);
System.out.println(format);//2021年12月02日 02:02:43 周四

//可以把一个格式化的字符串转成对应的Date
String s = "1996年01月01日 10:20:30 星期一";
//得到的Date还是国外的格式,需要指定格式输出
//再把String -> Date时,String格式需要和你指定的一样,否则会抛出转换异常
Date parse = sdf.parse(s);
System.out.println("parse = " + sdf.format(parse));//parse = 1996年01月01日 10:20:30 周一

The second generation date class

Calendar calendar, also fromjava.util.Calendar

image-20211202141051650

It consists of two private constructors, all passed through getInstanceto get the instance.

//1. Calendar时一个抽象类,并且构造器是private
//2. 通过getInstance()获取实例
//3. 提供了大量的方法和字段给程序员
Calendar c = Calendar.getInstance();//创建日历对象,比较简单
//打印出了大量的字段
System.out.println("c = " + c);

//获取日历对象的某个日历字段
System.out.println("YEAR = " + c.get(Calendar.YEAR));
//因为Calendar在返回月的时候是按照0开始编号的,所以要+1
System.out.println("MONTH = " + (c.get(Calendar.MONTH) + 1));
System.out.println("DAY = " + c.get(Calendar.DAY_OF_MONTH));
//如果要用24小时进制,使用 Calendar.HOUR_OF_DAY
System.out.println("HOUR = " + c.get(Calendar.HOUR));//默认12小时进制
System.out.println("MINUTE = " + c.get(Calendar.MINUTE));
System.out.println("SECOND = " + c.get(Calendar.SECOND));
System.out.println("AM = " + c.get((Calendar.AM_PM)));

//Calendar没有专门的格式化方法,所以需要程序员自己组合
System.out.println(c.get(Calendar.YEAR) + "年" + (c.get(Calendar.MONTH) + 1) + "月" +
        c.get(Calendar.DAY_OF_MONTH) + "日 " + ((Calendar.AM == c.get(Calendar.AM_PM))?"上午":"下午") +
        c.get(Calendar.HOUR) + ":" +  c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND));

The third generation date class

LocalDate、LocalTime、LocalDateTime

image-20211202144244614

Therefore, JDK8 has added LocalDate (date), LocalTime (time), LocalDateTime (date time)

  • LocalDate only has the year, month, and day, and the date field can be obtained

  • LocalTime only has hours, minutes and seconds, and the time field can be obtained

  • LocalDateTime has both, and can get date and time fields

//第三代日期

//1. 使用now()返回表示当前日期时间的 对象
LocalDateTime ldt = LocalDateTime.now();// LocalDate.now // LocalTime.now()
System.out.println(ldt);//2021-12-02T14:48:19.001017
System.out.println(ldt.getYear());//2021
System.out.println(ldt.getMonth());//DECEMBER
System.out.println(ldt.getMonthValue());//12
System.out.println(ldt.getDayOfMonth());//2
System.out.println(ldt.getHour());//14
System.out.println(ldt.getMinute());//51
System.out.println(ldt.getSecond());//10

LocalDate now = LocalDate.now();//获取年月日
LocalTime now1 = LocalTime.now();//获取时分秒

DateTimeFormatter

//1. 使用now()返回表示当前日期时间的 对象
        LocalDateTime ldt = LocalDateTime.now();// LocalDate.now // LocalTime.now()
        System.out.println(ldt);//2021-12-02T14:48:19.001017

        //2. 使用DateTimeFormatter 对象来格式化
        //创建DateTimeFormatter对象
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH小时mm分钟ss秒");
        String format = dtf.format(ldt);
        System.out.println(format);

Instant timestamp

//1. 通过静态方法now() 获取表示当前时间戳的对象
Instant now = Instant.now();
System.out.println("now = " + now);//now = 2021-12-02T07:08:58.978759600Z
//2. 通过 from 可以把 Instance 转换成 Date
Date date = Date.from(now);
System.out.println("date = " + date);//date = Thu Dec 02 15:08:58 CST 2021
//3. 通过 date 的 toInstant() 可以把 date 转换成 Instant对象的
Instant instant = date.toInstant();
System.out.println("instant = " + instant);//instant = 2021-12-02T07:08:58.978Z

Guess you like

Origin blog.csdn.net/weixin_46421722/article/details/122797877