目录
LocalDate/LocalTime/LocalDateTime类
ZoneDateTime和ZoneId
Instant类
DateTimeFormatter类 :解析格式化时间
LocalDate/LocalTime/LocalDateTime类
package crrc.studytest1;
import java.time.Duration;
import java.time.LocalDateTime;
public class DurationClass {
public static void main(String[] args) {
//获取两个时间对象用来比较
LocalDateTime start = LocalDateTime.of(1998,5,28,5,25,30);
LocalDateTime end = LocalDateTime.of(2024,9,19,15,01,15);
//创建一个Duration类来比较两个时间
Duration duration = Duration.between(start,end);
long days = duration.toDays();
long hours = duration.toHours();
long minutes = duration.toMinutes();
long seconds = duration.getSeconds();
long millis = duration.toMillis();
long nanos = duration.toNanos();
System.out.println(days);
System.out.println(hours);
System.out.println(minutes);
System.out.println(seconds);
System.out.println(millis);
System.out.println(nanos);
}
}
package crrc.studytest1;
import java.time.LocalDate;
import java.time.LocalTime;
public class LocalTimeClass {
public static void main(String[] args) {
//1.获取本地时间对象
LocalTime ld = LocalTime.now();//时 分 秒 纳秒
System.out.println(ld);
//2.获取日期中的信息
int hour = ld.getHour();//时
int minute = ld.getMinute();//分
int second = ld.getSecond();//秒
int nano = ld.getNano();//纳秒
System.out.println(hour);
System.out.println(minute);
System.out.println(second);
System.out.println(nano);
/*3.直接修改某个信息:withYear;withMonth.....都是以with开头,
然后会返回一个新的对象,不会改变原有时间对象*/
LocalTime ld2 = ld.withHour(15);
System.out.println(ld2);
LocalTime ld3 = ld2.withMinute(15);
System.out.println(ld3);
LocalTime ld33 = ld2.withSecond(0);
System.out.println(ld33);
//4.把某个信息加多少:plusYears;plusMonths;plusWeeks;
LocalTime ld4 = ld.plusHours(10);
System.out.println(ld4);
LocalTime ld5 = ld.plusMinutes(10);
System.out.println(ld5);
//5.把某个信息减多少:minusYears;minusMonths;minusWeeks;minusDays;
LocalTime ld6 = ld.minusHours(10);
System.out.println(ld6);
LocalTime ld7 = ld.minusSeconds(10);
System.out.println(ld7);
//6.获取指定日期的LocalDate对象:public static LocalDate of(int year,int month,int day)
LocalTime ld8 = LocalTime.of(10,5,28);
LocalTime ld9 = LocalTime.of(13,5,18);
LocalTime ld10 = LocalTime.of(13,5,18);
System.out.println(ld8);
System.out.println(ld9);
System.out.println(ld10);
//7.判断两个日期对象,是否相等,在前还是在后: equals;isBefore;isAfter;
System.out.println(ld9.equals(ld10));
System.out.println(ld9.equals(ld8));
System.out.println(ld8.isAfter(ld9));
System.out.println(ld8.isBefore(ld9));
}
}
package crrc.studytest1;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class LocalDateTimeClass {
public static void main(String[] args) {
//用法基本和前面两个类一样 只不过把他们结合起来了 在这儿不演示了
//简单用一下他们三者之间的转换
LocalTime lt = LocalTime.now();
LocalDate ld = LocalDate.now();
LocalDateTime ldt = LocalDateTime.of(ld,lt);
LocalDateTime ldt1 = LocalDateTime.now();
LocalTime lt1 = ldt1.toLocalTime();
LocalDate ld1 = ldt1.toLocalDate();
System.out.println(lt);
System.out.println(ld);
System.out.println(ldt);
System.out.println(ldt1);
System.out.println(lt1);
System.out.println(ld1);
}
}
ZoneDateTime和ZoneId
package crrc.studytest1;
import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZoneId_ZoneDateTime {
public static void main(String[] args) {
//获取当前系统的时区id并打印出来
// public static ZoneId systemDefault():获取系统默认时区
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);
System.out.println(zoneId.getId());
//获取Java支持的所有时区ID
System.out.println(ZoneId.getAvailableZoneIds());
//把某个时区ID封装成 ZoneId对象
ZoneId zoneId1 = ZoneId.of("America/New_York");
//ZoneDateTime 带时区的时间
//public static ZoneDateTime now(ZoneId zone):获取某个时区的ZoneDateTime对象
ZonedDateTime nowUSA = ZonedDateTime.now(zoneId1);
ZonedDateTime nowChina = ZonedDateTime.now(zoneId);
System.out.println(nowUSA);
System.out.println(nowChina);
//世界标准时间
ZonedDateTime now = ZonedDateTime.now(Clock.systemUTC());
System.out.println(now);
//默认系统时间
ZonedDateTime now2 = ZonedDateTime.now();
System.out.println(now2);
}
}
Instant类
package crrc.studytest1;
import java.time.Instant;
public class InstantClass {
public static void main(String[] args) {
//一般用来做代码性能分析 在代码执行前和代码执行后分别获取此刻时间戳,相减就是代码运行时间
Instant instant = Instant.now();
System.out.println(instant);
//获取总秒数
long second = instant.getEpochSecond();
//获取不够1秒的纳秒数
long nano = instant.getNano();
System.out.println(second);
System.out.println(nano);
//也有plus minus 等方法 返回一个新的Instant对象
}
}
DateTimeFormatter类 :解析格式化时间
package crrc.studytest1;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatterClass {
public static void main(String[] args) {
//用于时间格式化解析的
//1.创建一个日期时间格式化对象器出来
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//2.准备一些时间对其进行格式化
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
String strTime = dtf.format(ldt);//时间格式变成字符串(正向格式化)
System.out.println(strTime);
String strTime1 = ldt.format(dtf);//也可以用LocalDateTime类提供的format方法进行转换需要传入格式化器(反向格式化)
System.out.println(strTime1);
//3.解析时间 将字符串类型时间转换成相应时间对象
String dateStr = "1998年5月28日 18:55:36";
DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy年M月dd日 HH:mm:ss");
LocalDateTime ldt1 = LocalDateTime.parse(dateStr,dtf1);
System.out.println(ldt1);
}
}
Period类
package crrc.studytest1;
import java.time.LocalDate;
import java.time.Period;
public class PeriodClass {
public static void main(String[] args) {
//1.先准备两个时间对象用于计算二者的时间间隔
LocalDate ld1 = LocalDate.now();
LocalDate ld2 = ld1.plusMonths(13);
LocalDate ld3 = ld2.plusYears(5);
LocalDate ld4 = ld3.plusDays(20);
System.out.println(ld1);
System.out.println(ld4);
//2.创建一个Period对象
Period p = Period.between(ld1,ld4);
//3.获取两个日期相差的信息
int year = p.getYears();
int month = p.getMonths();
int day = p.getDays();
System.out.println(year);
System.out.println(month);
System.out.println(day);
}
}
Duration类
package crrc.studytest1;
import java.time.Duration;
import java.time.LocalDateTime;
public class DurationClass {
public static void main(String[] args) {
//获取两个时间对象用来比较
LocalDateTime start = LocalDateTime.of(1998,5,28,5,25,30);
LocalDateTime end = LocalDateTime.of(2024,9,19,15,01,15);
//创建一个Duration类来比较两个时间
Duration duration = Duration.between(start,end);
long days = duration.toDays();
long hours = duration.toHours();
long minutes = duration.toMinutes();
long seconds = duration.getSeconds();
long millis = duration.toMillis();
long nanos = duration.toNanos();
System.out.println(days);
System.out.println(hours);
System.out.println(minutes);
System.out.println(seconds);
System.out.println(millis);
System.out.println(nanos);
}
}