LoaclDate LocalTime LocalDateTime类
LocalDate :代表本地日期(年,月,日,星期)
LocalTime:代表本地时间(时,分,秒,纳秒)API
LocalDateTime:代表本地日期,时间
以上三个类获取对象的方法是:public static xxx.now();
import java.time.LocalDate;public class test {public static void main(String[] args) {//0:获取本地日期对象(不可变的)LocalDate ld=LocalDate.now();//年 月 日System.out.println(ld);//1:获取日期对象中的信息(get)int year =ld.getYear();//年int month=ld.getMonthValue();//月份int day=ld.getDayOfMonth();//一个月的第几天int dayofYear=ld.getDayOfYear();//一年的第几天int dayofWeek=ld.getDayOfWeek().getValue();//星期几System.out.println(dayofWeek);//星期3//2:直接修改某个信息(with)LocalDate d2=ld.withYear(2080);//会返回一个新的LocalDate对象LocalDate d3=ld.withDayOfMonth(4);LocalDate d4=ld.withMonth(5);//3:把某个信息加多少(plus)LocalDate d5=ld.plusWeeks(3);//4:把某个信息减多少(minus)LocalDate d6=ld.minusMonths(4);//5:获取指定日期的LocalDate对象LocalDate d7=LocalDate.of(2050,1,1);LocalDate d8=LocalDate.of(2050,1,1);System.out.println(d7);//2050-01-01//6:判断两个日期对象,是否相等,在前还是在后:equals isBefore isAfterSystem.out.println(d7.equals(d8));//trueSystem.out.println(d7.isAfter(ld));//trueSystem.out.println(d7.isBefore(ld));//false}
}
//7:可以直接把LocalDateTime对象变成LocalDate对象和LocalTime LocalDateTime ldt=LocalDateTime.now(); System.out.println(ldt); LocalDate d9=ldt.toLocalDate(); LocalTime t1=ldt.toLocalTime();//也可以把LocalDate和LocalTime对象变成一个LocalDateTime对象 LocalDateTime ldt2=LocalDateTime.of(d9,t1);
总结:这三个类获取对象的方法有
public static xxx now();
public static xxx of();获取指定时间的对象
ZoneId时区类:
public static ZoneId systemDefault()
Gets the system default time-zone.
获取系统默认时区
public static ZoneId of(String zoneId)
Obtains an instance of
ZoneId
from an ID ensuring that the ID is valid and available for use.获取一个指定时区
public static Set<String> getAvailableZoneIds()
Gets the set of available zone IDs.
获取java支持的所有时区
ZoneDateTime(LocalDateTime+ZoneId)带时区时间的常见方法:
public static ZonedDateTime now()
Obtains the current date-time from the system clock in the default time-zone.
获取当前时区的ZoneDateTime对象
public static ZonedDateTime now(ZoneId zone)
Obtains the current date-time from the system clock in the specified time-zone.
获取指定时区的ZoneDateTime对象
import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;public class test {public static void main(String[] args) {//1:获取系统默认时区ZoneId zoneid=ZoneId.systemDefault();System.out.println(zoneid.getId());//Asia/ShanghaiSystem.out.println(zoneid);//Asia/Shanghai//2:获取所有时区System.out.println(ZoneId.getAvailableZoneIds());//获取指定时区的ZoneId对象ZoneId zoneid2=ZoneId.of("America/New_York");System.out.println(zoneid2);//America/New_York//获取当前时区的ZoneDateTime对象ZonedDateTime z1=ZonedDateTime.now();System.out.println(z1);//2024-04-04T20:13:17.006835900+08:00[Asia/Shanghai]//获取指定时区的ZoneDateTime对象ZonedDateTime z2=ZonedDateTime.now(zoneid2);System.out.println(z2);//2024-04-04T08:14:52.356801600-04:00[America/New_York]//获取世界标准时间ZonedDateTime z3=ZonedDateTime.now(Clock.systemUTC());System.out.println(z3);//2024-04-04T12:17:47.416688700Z}
}
Instant类:时间线上的某个时刻/时间戳
通过获取Instant的对象可以拿到此刻的时间,该时间有两个部分组成:从1970-1-1 00:00:00开始到现在的总秒数和不到一秒的纳秒数
public static Instant now()
Obtains the current instant from the system clock.
获取当前时间的Instant对象
public long getEpochSecond()
Gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
public int getNano()
Gets the number of nanoseconds, later along the time-line, from the start of the second.
获取不满1s的纳秒
public class test {public static void main(String[] args) {//获取当前时间的Instant对象Instant i1=Instant.now();//不可变对象System.out.println(i1);//2024-04-04T12:29:43.772489700Z//获取总秒数long second= i1.getEpochSecond();System.out.println(second);//1712233841//获取不满1s的纳秒数int nano= i1.getNano();System.out.println(nano);//930559300}
}
DateTimeFormatter类
public static DateTimeFormatter ofPattern(String pattern)
Creates a formatter using the specified pattern.
获取格式化器对象
public String format(TemporalAccessor temporal)
Formats a date-time object using this formatter.
格式化时间
public class test {public static void main(String[] args) {DateTimeFormatter d=DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");//对时间格式化LocalDateTime ldt=LocalDateTime.now();System.out.println(ldt);//2024-04-04T20:42:16.266254400String s= d.format(ldt);System.out.println(s);//2024年04月04日 20:52:39//第二种格式化的方法,LocalDateTime自己调用format方法String rs= ldt.format(d);System.out.println(rs);//2024年04月04日 20:52:39//解析时间,使用LccalDateTime的parse方法String dateStr="2024年12月12日 20:42:16";LocalDateTime ldt2=LocalDateTime.parse(dateStr,d);System.out.println(ldt2);//2024-12-12T20:42:16}
}
Period类:计算日期间隔(年,月,日)
用于两个LocalDate对象
public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)
Obtains a
Period
consisting of the number of years, months, and days between two dates.传入两个日期对象,得到period对象
public class test {public static void main(String[] args) {//创建一个Period对象LocalDate d1=LocalDate.of(2028,3,1);LocalDate d2=LocalDate.of(2050,2,5);Period p=Period.between(d1,d2);//获取相差信息int year=p.getYears();int month=p.getMonths();int day=p.getDays();System.out.println(year);//21System.out.println(month);//11System.out.println(day);//4}
}
Duration
可以用于计算两个时间对象相差的天数,小时数,分数,秒数,纳秒数;支持LocalTime,LocalDateTime,Instant
public static Duration between(Temporal startInclusive, Temporal endExclusive)
传入两个时间对象,得到Duration对象
public class test {public static void main(String[] args) {LocalDateTime l1=LocalDateTime.of(2025,11,11,12,12,10);LocalDateTime l2=LocalDateTime.of(2024,11,23,10,10,10);//1:得到Duration对象Duration d=Duration.between(l1,l2);//2:获得差值System.out.println(d.toDays());//间隔多少天System.out.println(d.toHours());//间隔多少小时System.out.println(d.toMinutes());//间隔多少分System.out.println(d.toSeconds());//间隔多少秒System.out.println(d.toMillis());//间隔多少毫秒System.out.println(d.toNanos());//间隔多少纳秒}
}