一、LocalDate,LocalTime,LocalDateTime
LocalDateTime(合并了LocalDate,LocalTime
import java.time.LocalDate;
public class Test1_LocalDate {
public static void main(String[] args) {
//1.获取本地日期对象(不可变对象)
LocalDate ld = LocalDate.now ();
System.out.println (ld);//2023-09-12
//2.获取日期对象中的信息
int year = ld.getYear ();//年
int month = ld.getMonthValue ();//月(1-12)
int day = ld.getDayOfMonth ();//日
int dayOfYear = ld.getDayOfYear ();//一年中的第几天
int dayOfWeek = ld.getDayOfWeek ().getValue ();//星期几
System.out.println (year);//2023
System.out.println (day);//12
System.out.println (dayOfWeek);//2
/**
* 3.直接修改某个信息:withYear、withMonth,withDayOfMonth,withDayOfYear
*/
LocalDate ld2 = ld.withYear ( 2099 );
System.out.println (ld2);//2099-09-12
System.out.println (ld);//2023-09-12
/**
* 4.把某个信息加多少 plusYears,plusMonths,plusDays,plusWeeks
*/
LocalDate ld3 = ld.plusYears ( 22 );//2045-09-12
LocalDate ld4 = ld.plusDays ( 20 );//2023-10-02
System.out.println (ld3);
System.out.println (ld4);
/**
* 把某个信息减去多少 minusYears(),ld.minusMonths(),minusDays(),minusWeeks()
*/
LocalDate ld11 = ld.minusYears ( 1 );
LocalDate ld12 = ld.minusMonths ( 1 );
LocalDate ld13 = ld.minusDays ( 1 );
LocalDate ld14 = ld.minusWeeks ( 1 );
/**
* 5.获取指定日期的LocalDate对象,public static LocalDate.of(int year,int month,int dayOfMonth)
*/
LocalDate ld7=LocalDate.of ( 2666,12,15 );
LocalDate ld8=LocalDate.of ( 2666,12,16 );
System.out.println (ld7);//2666-12-15
/**
* 6.判断两个日期对象是否相等 在前还是在后 equals isBefore isAfter
*
*/
System.out.println (ld7.equals ( ld8 ));//false
System.out.println (ld7.isBefore ( ld8 ));//true
System.out.println (ld8.isAfter ( ld7 ));//true
}
}
import java.time.LocalTime;
public class LocalTimeTest {
public static void main(String[] args) {
/**
* 1.获取本地时间对象
*/
LocalTime lt = LocalTime.now ();
System.out.println (lt);//20:12:37.607
/**
* 2.获取时间中的信息
*/
int hour = lt.getHour ();//时
int minute = lt.getMinute ();//分
int second = lt.getSecond ();//秒
int nano = lt.getNano ();//纳秒
System.out.println (hour);
System.out.println (minute);
System.out.println (second);
System.out.println (nano);
/**
* 3.修改时间 withHour,withMinute,withSecond,withNano
*/
LocalTime lt3 = lt.withHour ( 22 );
LocalTime lt4 = lt.withMinute ( 23 );
LocalTime lt5 = lt.withSecond ( 50 );
LocalTime lt6 = lt.withNano ( 444498415 );
System.out.println (lt3);
System.out.println (lt4);
System.out.println (lt5);
System.out.println (lt6);
/**
* 4.加多少:plusHour,plusMinutes,plusSeconds,plusNanos
*/
LocalTime lt7 = lt.plusHours ( 1 );
LocalTime lt8 = lt.plusMinutes ( 20 );
LocalTime lt9 = lt.plusSeconds ( 30 );
LocalTime lt10 = lt.plusNanos ( 64646 );
System.out.println (lt7);
System.out.println (lt8);
System.out.println (lt9);
System.out.println (lt10);
/**
* 5.减多少 minusHours(),minusMinutes(),minusSeconds(),minusNanos()
*/
LocalTime lt11 = lt.minusHours ( 1 );
LocalTime lt12 = lt.minusMinutes ( 12 );
LocalTime lt13 = lt.minusSeconds ( 13 );
LocalTime lt14 = lt.minusNanos ( 5665 );
/**
* 6.获取指定时间的LocalTime 对象 :public static localTime.of(int hour,int minute,int second)
*/
LocalTime lt15 = LocalTime.of ( 12, 12, 12 );
LocalTime lt16 = LocalTime.of ( 13, 13, 13 );
/**
* 7.判断两个日期对象是否相等 在前还是在后 equals isBefore isAfter
*/
System.out.println (lt15.equals ( lt16 ));
System.out.println (lt16.isAfter ( lt15 ));
System.out.println (lt15.isBefore ( lt16 ));
}
}
二、ZoneId(时区)
import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Set;
public class ZoneIdTest {
public static void main(String[] args) {
//1.获取系统默认的时区 public static ZonId systemDefault ()
ZoneId z1 = ZoneId.systemDefault ();
System.out.println ( z1.getId () );//Asia/Shanghai
System.out.println ( z1 );Asia/Shanghai
//2.获取java支持的全部时区ID public static Set<String> getAvailableZoneIds ();
System.out.println ( ZoneId.getAvailableZoneIds () );//显示全部时区
//3.把某个时区Id 封装成ZoneId对象 public static ZonId.of(String zoneId)
ZoneId zoneId = ZoneId.of ( "Asia/Shanghai" );
//4.ZoneDateTime:带时区的时间
//public static ZoneDateTime now(ZoneId zone):获取某个时区的ZoneDateTime对象
ZonedDateTime now = ZonedDateTime.now (zoneId); //获取 亚洲上海的当前时间
System.out.println (now);
//拿到世界标准时间
ZonedDateTime now1 = ZonedDateTime.now ( Clock.systemUTC () );
System.out.println ( now1 );
//获取系统莫仍时区的ZoneDateTime对象
ZonedDateTime now2 = ZonedDateTime.now ();
System.out.println ( now2 );
}
}
三、Instant
import java.time.Instant;
public class InstantTest {
public static void main(String[] args) {
//1.创建Instant对象,获取此刻时间信息
Instant now = Instant.now ();//不可变对象
//2.获取总秒数
long second = now.getEpochSecond ();
System.out.println ( second );
//3.不够1秒的纳秒数
int nano = now.getNano ();
System.out.println (nano);
//4.plus
Instant instant = now.plusNanos ( 111 );
//5.Instant对象的作用 乐意用来记录代码的执行时间,或用于记录用户操作某个时间的时间点
}
}
四、SimpleDateFormatter(线程安全)
代替SimpleDateFormat(线程不安全)
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatterTest {
public static void main(String[] args) {
//1.构建一个日期格式化对象出来
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "yyyy年MM月dd日 HH:mm:ss" );
//2.对时间进行格式化
LocalDateTime now = LocalDateTime.now ();
System.out.println (now);
String rs = formatter.format ( now );
System.out.println (rs);//正向格式化
String rs2 = now.format ( formatter );//反向格式化
System.out.println (rs2);
//3.解析时间 解析时间一般使用LocalDateTime提供的解析方法来解析
String dateStr="2029年12月12日 12:12:11";
LocalDateTime ldt = LocalDateTime.parse ( dateStr, formatter );
System.out.println ( ldt );
}
}
五、Period
import java.time.LocalDate;
import java.time.Period;
public class PeriodTest {
public static void main(String[] args) {
LocalDate start = LocalDate.of ( 2029, 8, 10 );
LocalDate end = LocalDate.of ( 2029, 8, 15 );
//1.创建Period对象,封装两个日期对象
Period period = Period.between ( start, end );
//2通过Period对象,获取两个日期对象相差的信息 只能计算 年 月 日 的间隔
System.out.println ( period.getYears () );//0
System.out.println ( period.getMonths () );//0
System.out.println ( period.getDays () );//5
}
}
六、Duration(持续时间)
import java.time.Duration;
import java.time.LocalDateTime;
public class DurationTest {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of ( 2025, 11, 11, 10, 10 );
LocalDateTime end = LocalDateTime.of ( 2025, 11, 11, 11, 11 );
//1.得到Duration对象
Duration d1 = Duration.between ( start, end );
System.out.println ( d1.toDays () );//间隔多少天
System.out.println ( d1.toHours () );//间隔多少小时
System.out.println ( d1.toMinutes () );//间隔多少分钟
System.out.println ( d1.toMillis () );//间隔多少毫秒
System.out.println ( d1.toNanos () );//间隔多少纳秒
}
}