Common time and date of Java core

Detailed Java date processing class Date

The basics of time

  • Time zone: The entire earth is divided into twenty-four time zones, each time zone has its own local time.

For the sake of uniformity, a unified time is used, called Universal Time Coordinated (UTC).
TC and Greenwich Mean Time (GMT, Greenwich Mean Time, also translated as: Greenwich Mean Time) are almost the same as
CST (Beijing Time), Beijing Time, China Standard
Time, China Standard Time. In terms of time zone division, it belongs to the East Eight District, which is 8 hours earlier than Coordinated Universal Time and is recorded as UTC+8.
Timestamp: The total number of seconds from January 1, 1970 (08:00:00GMT) to the current time. It is also called Unix Timestamp and is widely used in intellectual property protection, contract signing, and finance aspects of accounting, electronic tender offer, stock trading and other
formats variety: 2050-10-3110: 11: 11,2050 / 10/3110 / 10:10 year, month, date, day of week, etc.

background:

  • How to express the time in the program code? What do I need to get the current time? The ava.util package provides the Date class to encapsulate the current date and time constructor
  • The ava.util package provides the Date class to encapsulate the current date and time
  • Constructor
//当前时间
Date()
//从1970年1月1日起的毫秒数作为参数
Date(long millisec)

Common method

//返回自1970年1月1日00:00:00GMT以来此Date对象表示的毫秒数。
long getTime()
//调用此方法的Date对象在指定日期之后返回true,否则返回false。
boolean after(Date date)
//调用此方法的Date对象在指定日期之前返回true,否则返回false。
boolean before(Date date)

The new version of JDK8 time and date processing class

  • Java8 further strengthens the processing of date and time by releasing the new Date-Time API (JSR310)
  • Many common apis have been added, such as date/time comparison, addition and subtraction, formatting, etc. The location of the package is java.time
  • Core class
LocalDate:不包含具体时间的日期。
LocalTime:不含日期的时间。
LocalDateTime:包含了日期及时间。

  • LocalDate common API
 LocalDate today=LocalDate.now();
 system.out.print1n("今天日期:"+today);
 
//获取年,月,日,周几
system.out.print1n("现在是哪年:"+today.getYear());
system.out.print1n("现在是哪月:"+today.getMonth());
System.out.print1n("现在是哪月(数字):"+today.getMonthValue());
System.out.print1n("现在是几号:"+today.getDayofMonth());
system.out.print1n("现在是周几:"+today.getDayofweek());
//加减年份,加后返回的对象才是修改后的,旧的依旧是旧的LocalDate changeDate=today.plusYears(1);
system.out.print1n("加后是哪年:"+changeDate.getYear());
System.out.print1n("旧的是哪年:"+today.getYear());
//日期比较
system.out.print1n("isafter:"+changeDate.isAfter(today));
//getYear()int 获取当前日期的年份
//getMonth()Month获取当前日期的月份对象
//getMonthValue()int 获取当前日期是第几月
//getDayofweek()Dayofweek 表示该对象表示的日期是星期几
//getDayofMonth()int 表示该对象表示的日期是这个月第几天
//getDayofyear()int 表示该对象表示的日期是今年第几天
//withyear(int year)LocalDate 修改当前对象的年份
//withMonth(int month)LocalDate修改当前对象的月份
//withpayofMonth(int dayofMonth)LocalDate 修改当前对象在当月的日期
//plusYears(long yearsToAdd)Localpate 当前对象增加指定的年份数
//plusMonths(1ong monthsToAdd)LocalDate 当前对象增加指定的月份数
//plusweeks(1ong weeksToAdd)LocalDate 当前对象增加指定的周数
//plusDays(1ong daysToAdd)LocalDate 当前对象增加指定的天数
//minusYears(long yearsTosubtract)LocalDate 当前对象减去指定的年数
//minusMonths(1ong months ToSubtract)LocalDate当前对象减去注定的月数
//minusWeeks(long weeksTosubtract)LocalDate 当前对象减去指定的周数
//minusDays(1ong daysTosubtract)LocalDate当前对象减去指定的天数
//compareTo(ChronoLocalDate other)int 比较当前对象和other对象在时间上的大小,返回值如果为正,则当前对象时间较晚
//isBefore(ChronoLocalDate other)boolean比较当前对象日期是否在other对象日期之前
//isAfter(ChronoLocalDate other)boolean 比较当前对象日期是否在other对象日期之后
//isEqual(ChronoLocalDate other)boolean 比较两个日期对象是否相等

Time and date formatting of the new version of JDK8

  • Why format the date and time

Program printing, or web page display time and date format, users have different needs, you need to format according to certain rules

  • Commonly used placeholders

y Four-digit year
M month d day
h hour in
m minutes
S milliseconds

  • After JDK8: the date and time of introducing thread safety
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd
HH:mm:ss");
String ldtStr = dtf.format(ldt);
System.out.println(ldtStr);

  • Get the specified date and time object LocalDate Time
    ldt =LocalDate Time.of(2020,11,11,8,20,30); System.out.println(ldt);
  • Calculate the date and time difference java.time.Duration
LocalDateTime today = LocalDateTime.now();
System.out.println(today);
LocalDateTime changeDate = LocalDateTime.of(2020,10,1,10,40,30);
System.out.println(changeDate);
Duration duration = Duration.between( today,changeDate);//第⼆二个参数减第⼀一
个参数
System.out.println(duration.toDays());//两个时间差的天数
System.out.println(duration.toHours());//两个时间差的小时数
System.out.println(duration.toMinutes());//两个时间差的分钟数
System.out.println(duration.toMillis());//两个时间差的毫秒数
System.out.println(duration.toNanos());//两个时间差的纳秒数

Guess you like

Origin blog.csdn.net/qq_43266564/article/details/114988977