时间相关类

Date 时间类

用来描述时间,精确到毫秒

利用空参构造创建的对象,默认表示系统当前时间

利用有参构造创建的对象,表示指定的时间

//创建一个对象表示时间
Date d1 = new Date();
System.out.println(d1);

//创建对象表示一个指定的时间
Date d2=new Date(0L);
System.out.println(d2);

//setTime修改时间
d2.setTime(1000L);
System.out.println(d2);

//getTime获取当前时间的毫秒值
System.out.println(d2.getTime());

ZoneId

static set<String> getAvailableZoneIds() 获取Java中支持的所有时区

static ZoneId systemDefault 获取系统默认时区

static ZoneId of(String zoneId) 获取一个指定时区

Set<String> zongIds = ZoneId.getAvailableZoneIds();//Asia/Shanghai
System.out.println(zongIds);
System.out.println(zongIds.size());

//2.获取当前系统的默认时区
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);//Asia/Shanghai

//3.获取指定的时区
ZoneId zoneId1 = ZoneId.of("Asia/Pontianak");
System.out.println(zoneId1);

Instant时间戳

static Instant now()        获取当前时间的Instant对象(标准时间)

static Instant ofXXX(long epochMilli)         根据(秒/毫秒/纳秒)获取Instant对象

ZonedDateTime atZone(ZongId zone)         指定时区

Boolean isXXX(Instant otherInstant )        判断系列的方法
Instant minusXxx(long millisToSubtract)        减少时间系列的方法

Instant plusXxx(long millisToSubtract)        增加时间系列的方法

//1.获取当前时间的Instant 对象
Instant now = Instant.now();
System.out.println(now);

//2.根据毫秒获取时间
Instant instant1 = Instant.ofEpochMilli(0L);
System.out.println(instant1);
Instant instant2 = Instant.ofEpochSecond(1L);
System.out.println(instant2);
Instant instant3 = Instant.ofEpochSecond(1L, 100000000L);
System.out.println(instant3);

//3.指定时区
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(time);

//4.isXxx判断
Instant instant4 = Instant.ofEpochMilli(0L);
Instant instant5 = Instant.ofEpochMilli(10000L);
//isBefore:判断调用者的时间是否在参数表示的时间后面
boolean before = instant4.isBefore(instant5);
System.out.println(before);//true

//isAfter:判断调用者的时间是否在参数表示的时间前面
boolean after = instant4.isAfter(instant5);
System.out.println(after);//false

//5.minusXxx减少时间的方法
Instant instant6 = Instant.ofEpochMilli(3000L);
System.out.println(instant6);//1970-01-01T00:00:03Z
System.out.println(instant6.minusMillis(2000L));//1970-01-01T00:00:01Z

ZoneDateTime

//1.获取当前时间对象(带时区)
ZonedDateTime now = ZonedDateTime.now();
System.out.println(now);

//2.获取指定的时间对象(带时区)
//年月日时分秒纳秒方式指定
ZonedDateTime time2 = ZonedDateTime.of(2023, 7, 7, 12,
        8, 12, 0, ZoneId.of("Asia/Shanghai"));
System.out.println(time2);

//通过Instant+时区的方式获取时间对象
Instant instant = Instant.ofEpochMilli(0L);
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
ZonedDateTime time3 = ZonedDateTime.ofInstant(instant, zoneId);
System.out.println(time3);

//3.withXXx 修改时间的系列方法
ZonedDateTime time4 = time3.withYear(2020);
System.out.println(time4);

//4.减少时间
ZonedDateTime time5 = time4.minusYears(50);
System.out.println(time5);

//5.增加时间
ZonedDateTime time6 = time5.plusYears(200);
System.out.println(time6);

JDK8之后的时间创建后不可修改,如果要修改会自动生成一个新的对象

DateTimeFormatter用于时间的格式化和解析

static DateTimeFormatter ofPattern(格式)          获取格式对象

String format()时间对象                按照指定方式格式化

//获取时间对象
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));

//解析/格式化器
DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EE a");
//格式化
System.out.println(dtf1.format(time));

日历类

static XXX now();获取当前时间对象
static XXX of(); 获取指定时间的对象

LocalDate

//1.获取当前时间的日历对象(只包含年月日)
LocalDate nowDate = LocalDate.now();
System.out.println(nowDate);

//2.获取指定时间的日历对象
LocalDate ldTime = LocalDate.of(2023, 7, 8);
System.out.println(ldTime);

System.out.println("====================================");

//3.get系列获取日历中每一个属性值
//获取年
int year = ldTime.getYear();
System.out.println(year);
System.out.println("====================================");

//获取月
Month month = ldTime.getMonth();
//获取月时候获取的是对象
//需要对对象获取值
System.out.println(month);
System.out.println(month.getValue());

int monthValue = ldTime.getMonthValue();
System.out.println(monthValue);

//获取日
int dayOfMonth = ldTime.getDayOfMonth();
System.out.println(dayOfMonth);

//获取一年的第几天
int dayOfYear = ldTime.getDayOfYear();
System.out.println(dayOfYear);

//获取星期
DayOfWeek dayOfWeek = ldTime.getDayOfWeek();
System.out.println(dayOfWeek);

//is方法判断前后
System.out.println(ldTime.isAfter(ldTime));
System.out.println(ldTime.isBefore(ldTime));

//with方法修改年月日,并产生一个新的对象

//minus表示减少,只能减少年月日,返回一个新的对象
//plus表示增加,只能增加年月日,返回一个新的对象

//-----------------------------------------------------------

LocalTime

只能关注时分秒纳秒,在对时间具有精确性的时候需要用

方法与LocalDate一样

LocalDateTime

可以有年月日时分秒,方法跟LocalDate一样

Duration

用于计算两个时间间隔(秒,纳秒) }

Period

用于计算两个日期间隔(年,月,日)

    //当前本地年月日
    LocalDate now = LocalDate.now();
    System.out.println(now);

    //生日的年月日
    LocalDate birthday = LocalDate.of(2004, 2, 6);
    System.out.println(birthday);

    Period between = Period.between(birthday, now);

    System.out.println("时间间隔:"+between);
    System.out.println(between.getYears());
    System.out.println(between.getMonths());
    System.out.println(between.getDays());

    System.out.println(between.toTotalMonths());
}

ChronoUnit

用于计算两个日期间隔

最为常用!!!!!!!!!!!!!!!!!!!!!

LocalDateTime now = LocalDateTime.now();
LocalDateTime birthday = LocalDateTime.of(2004, 2, 6, 12, 12);
System.out.println(ChronoUnit.YEARS.between(birthday, now));
System.out.println(ChronoUnit.MONTHS.between(birthday, now));
System.out.println(ChronoUnit.DAYS.between(birthday, now));
System.out.println(ChronoUnit.HOURS.between(birthday, now));
System.out.println(ChronoUnit.MINUTES.between(birthday, now));
System.out.println(ChronoUnit.SECONDS.between(birthday, now));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值