54人参与 • 2026-10-09 • Java
localdatetime、localdate、localtime 是java8全新的日期框架,较之前的util.date以及calander使用起来更加的方便直观,下面介绍几种常见的日期对象用法。
localdate、localtime、localdatetime、instant均为不可变对象,修改这些对象对象会返回一个副本。而并非在原对象上修改。
localdatetime now = localdatetime.now();
localdatetime localdatetime = localdatetime.of(2021, 6, 16, 16, 37, 20, 814 * 1000 * 1000); localdatetime ldt = localdatetime.now().withyear(2021).withmonth(6).withdayofmonth(16).withhour(10).withminute(10).withsecond(59).withnano(999 * 1000 * 1000);
注意:最后一个参数是纳秒
localdatetime datetime = localdatetime.now(zoneid.of("asia/shanghai"));
localdatetime datetime2 = localdatetime.now(zoneid.of("+8"));
localdatetime now = localdatetime.now(); int year = now.getyear(); int month = now.getmonthvalue(); int dayofyear = now.getdayofyear(); int dayofmonth = now.getdayofmonth(); int hour = now.gethour(); int minute = now.getminute(); int second = now.getsecond(); int nano = now.getnano(); system.out.println(year + " " + month + " " + dayofyear + " " + dayofmonth); system.out.println(hour + " " + minute + " " + second + " " + nano);
localdate localdate = now.tolocaldate(); localtime localtime = now.tolocaltime();
localdatetime now = localdatetime.now(); localdatetime tomorrow = now.plusdays(1l); tomorrow = tomorrow.plushours(2l); tomorrow = tomorrow.plusminutes(10l); localdatetime yesterday = now.minus(duration.ofdays(1)); yesterday = yesterday.plushours(2l); yesterday = yesterday.plusminutes(10l); system.out.println(tomorrow); system.out.println(yesterday);
注意:localdatetime、localdate、localtime 日期对象每次修改都会创建一个新的对象,而非在原有对象基础上修改
localdatetime now = localdatetime.now(); localdatetime ldt1 = now.withhour(10).withminute(0).withsecond(0).withnano(0);
instant instant = instant.now(); long min = instant.toepochmilli(); system.out.println(min);
下面这种方式可以获取两个时间点相差n年n月n日n时n分n秒
localdatetime fromdatetime = localdatetime.of(1992, 6, 11, 10, 23, 55);
localdatetime todatetime = localdatetime.now();
localdatetime tempdatetime = localdatetime.from(fromdatetime);
long years = tempdatetime.until(todatetime, chronounit.years);
tempdatetime = tempdatetime.plusyears(years);
long months = tempdatetime.until(todatetime, chronounit.months);
tempdatetime = tempdatetime.plusmonths(months);
long days = tempdatetime.until(todatetime, chronounit.days);
tempdatetime = tempdatetime.plusdays(days);
long hours = tempdatetime.until(todatetime, chronounit.hours);
tempdatetime = tempdatetime.plushours(hours);
long minutes = tempdatetime.until(todatetime, chronounit.minutes);
tempdatetime = tempdatetime.plusminutes(minutes);
long seconds = tempdatetime.until(todatetime, chronounit.seconds);
system.out.println("" + java.time.duration.between(tempdatetime, todatetime).tohours());
system.out.println(years + " years "
+ months + " months "
+ days + " days "
+ hours + " hours "
+ minutes + " minutes "
+ seconds + " seconds.");
如果只想获取两个时间相差的天数或者小时数,可以使用以下方法
localdatetime ldt1 = localdatetime.of(1992, 6, 11, 10, 23, 55); localdatetime ldt2 = localdatetime.now(); system.out.println(duration.between(ldt1, ldt2).todays()); system.out.println(duration.between(ldt1, ldt2).tohours());
public static date localdatetime2date(localdatetime localdatetime) {
return date.from(localdatetime.atzone(zoneid.systemdefault()).toinstant());
}
private static final datetimeformatter date_time_formatter = datetimeformatter.ofpattern("yyyy-mm-dd hh:mm:ss");
public static string localdatetime2string(localdatetime localdatetime) {
return localdatetime.format(date_time_formatter);
}
注意:datetimeformatter是线程安全的类,可以将此类放到常量中。
private static final datetimeformatter date_time_formatter = datetimeformatter.ofpattern("yyyy-mm-dd hh:mm:ss");
public static localdatetime string2localdatetime(string str) {
return localdatetime.parse(str, date_time_formatter);
}
public static date string2date(string str) throws parseexception {
simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss");
return simpledateformat.parse(str);
}
注意:simpledateformat是非线程安全的类,在多线程操作时会报错,所以此对象每次用的时候最好新建。或者作为方法入参传递进来。
public static localdatetime date2localdatetime(date date) {
return date.toinstant()
.atzone(zoneid.systemdefault())
.tolocaldatetime();
}
public static string date2string(date date) {
simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
return formatter.format(date);
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论