LocalDateTime

LocalDate 年月日
LocalTime 时分秒
LocalDateTime 年月日时分秒

获取当前时间

LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();

指定时间创建对象

LocalDate localDate = LocalDate.of(2018, 1, 13);
LocalTime localTime = LocalTime.of(9, 43, 20);
LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 13, 9, 43, 20);

日期时间的加减

LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime plusYearsResult = localDateTime.plusYears(2L);
// 以下方法的参数都是long型,返回值都是LocalDateTime
// 加 plusYears、plusMonths、plusDays、plusHours、plusMinutes、plusSeconds
// 减 minusYears、minusMonths、minusDays、minusHours、minusMinutes、minusSeconds

将年、月、日等修改为指定的值,并返回新的日期(时间)对象

LocalDate localDate = LocalDate.now();
//当前时间基础上,指定本年当中的第几天,取值范围为1-365,366
LocalDate withDayOfYearResult = localDate.withDayOfYear(200);
//当前时间基础上,指定本月当中的第几天,取值范围为1-29,30,31
LocalDate withDayOfMonthResult = localDate.withDayOfMonth(5);
//当前时间基础上,直接指定年份
LocalDate withYearResult = localDate.withYear(2017);
//当前时间基础上,直接指定月份
LocalDate withMonthResult = localDate.withMonth(5);

获取日期的年月日周时分秒

LocalDateTime localDateTime = LocalDateTime.now(); 
int dayOfYear = localDateTime.getDayOfYear();// 本年当中第几天
int dayOfMonth = localDateTime.getDayOfMonth();// 本月当中第几天
DayOfWeek dayOfWeek = localDateTime.getDayOfWeek(); // 本周中星期几 英文 dayOfWeek.getValue();// 本周中星期几 数字

int year = localDateTime.getYear(); 
Month month = localDateTime.getMonth();
int day = localDateTime.getDayOfMonth();
int hour = localDateTime.getHour();
int minute = localDateTime.getMinute();
int second = localDateTime.getSecond();

判断两个时间点的前后

LocalDate localDate1 = LocalDate.of(2017, 8, 8);
LocalDate localDate2 = LocalDate.of(2018, 8, 8);
boolean date1IsBeforeDate2 = localDate1.isBefore(localDate2);

计算日期间隔年月日

Duration:用于计算两个“时间”间隔
Period:用于计算两个“日期”间隔

LocalDate date1 = LocalDate.of(2018, 2, 13);
LocalDate date2 = LocalDate.of(2017, 3, 12);
//内部是用date2-date1,所以得到的结果是负数
Period period = Period.between(date1, date2);
System.out.println("相差年数 : " + period.getYears());
System.out.println("相差月数 : " + period.getMonths());
System.out.println("相差日数 : " + period.getDays());
//还可以这样获取相差的年月日
long years = period.get(ChronoUnit.YEARS);
long months = period.get(ChronoUnit.MONTHS);
long days = period.get(ChronoUnit.DAYS);

天 :" + duration.toDays() + "\n"
时 :" + duration.toHours() + "\n"
分 :" + duration.toMinutes() + "\n"
毫秒 :" + duration.toMillis() + "\n"
纳秒 :" + duration.toNanos() + "\n"

运行耗时

Duration duration = Duration.between(ins1, ins2);
System.out.println("程序运行耗时为 : " + duration.toMillis() + "毫秒");

时间日期格式化

LocalDateTime date1 = LocalDateTime.now();
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String date2Str = formatter2.format(date1);

将时间字符串形式转化为日期对象

String datetime =  "2018-01-13 21:27:30";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.parse(datetime, dtf);

时间日期对象转为格式化日期对象

LocalDateTime formatedDateTime = LocalDateTime.parse(datetime, dtf);

long毫秒值转换为日期

DateTimeFormatter df= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String longToDateTime = df.format(LocalDateTime.ofInstant(
Instant.ofEpochMilli(System.currentTimeMillis()),ZoneId.of("Asia/Shanghai")));
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小心仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值