LocalDate
里面的是年月日星期,最主要的是六方面如下
package com.API1;
import java.time.LocalDate;
public class test_LocalDate {
public static void main(String[] args) {
//获取本地日期对象(不可变对象)
LocalDate ld = LocalDate.now();//年、月、日
System.out.println("日期:"+ld);
//1.获取日期对象中的信息
int year=ld.getYear();//得到年份
System.out.println("年份:"+year);
//ld.Month得到的是Month类型,ld.getMonthValue方法返回的是int型的数据
int month=ld.getMonth().getValue();//得到月份
//int month = ld.getMonthValue();
System.out.println("月份:"+month);
//getDayOfMonth表示一个月中的第几天
int day=ld.getDayOfMonth();//得到具体几号
System.out.println(day+"号");
//一年当中的第几天
int year_inday=ld.getDayOfYear();
System.out.println("一年当中的第"+year_inday+"天");
//星期几
int dayofweek=ld.getDayOfWeek().getValue();
System.out.println("哦~今天星期几~哦~今天星期"+dayofweek);
//2.直接修改某个信息:withYear、withMonth、withDayOfMonth、withDayOfYear
//因为LocalDate里面只有年月日,因此只能修改年月日
LocalDate ld2 = ld.withYear(2099);//此方法返回的是新对象,原本日期不改变
System.out.println("修改后的日期:"+ld2);
System.out.println("系统真正的日期:"+ld);
//每次修改都是返回一个新的对象
LocalDate ld3 = ld.withMonth(1);
System.out.println("拜托回到第"+ld3+"月");
LocalDate ld4 = ld.withDayOfMonth(1);
System.out.println("拜托回到这个月的第"+ld4+"天");
LocalDate ld5 = ld.withDayOfYear(1);
System.out.println("拜托回到今年的第"+ld5+"天");
//3.把某个信息加多少:plusYears、pulsMonths、plusDays、plusWeeks
LocalDate ld6=ld.plusYears(2);
LocalDate ld7=ld.plusDays(2);
//4.把某个信息减多少;minusYears、minusDays、minusWeeks
LocalDate ld8 = ld.minusYears(9);
//5.获取指定日期的LocalDate对象:public LocalDate of(int year,int month,int dayOfMonth);
LocalDate ld9=LocalDate.of(2099,12,12);
//6.判断2个日期对象,是否相等,在前还是在后:equals isBefore isAfter
System.out.println(ld9.equals(ld));
System.out.println(ld9.isAfter(ld));
System.out.println(ld9.isBefore(ld));
}
}
LocalTime
时分秒纳秒,用法和LocalDate用法相同
ZonedDateTime:带时区时间的常见方法
package com.API1;
import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class test_Zoneld {
public static void main(String[] args) {
//获取系统默认的时区
ZoneId zoneld=ZoneId.systemDefault();
System.out.println(zoneld.getId());
System.out.println(zoneld);
//获取JAVA支持的全部时区ID
System.out.println(ZoneId.getAvailableZoneIds());
//public static ZoneId of(String zoneId),把某个时区id封装成ZoneId对象
ZoneId zoneid1=ZoneId.of("America/New_York");
//ZonedDateTime:带时区的时间
//获取某个时间的ZonedDateTime对象,即返回的是带纽约时区的时间
ZonedDateTime now= ZonedDateTime.now(zoneid1);
System.out.println(now);
//获取系统默认时区的ZonedDateTime对象,即带上海时区的时间
ZonedDateTime now2=ZonedDateTime.now();
System.out.println(now2);
//世界标准时间
ZonedDateTime now1=ZonedDateTime.now(Clock.systemDefaultZone());
System.out.println(now1);
//也是返回一个新的ZonedDateTime对象
System.out.println(now2.getMonth());
}
}