1. 简介
localdatetime、localdate、localtime 是java8全新的日期框架,较之前的util.date以及calander使用起来更加的方便直观,下面介绍几种常见的日期对象用法。
- localdatetime:日期加时间的日期对象,包含年月日时分秒
- localdate:日期类,包含年月日
- localtime:时间类,包含时分秒
localdate、localtime、localdatetime、instant均为不可变对象,修改这些对象对象会返回一个副本。而并非在原对象上修改。
2. localdatetime用法
2.1 获取当前的时间
localdatetime now = localdatetime.now();
2.2 根据年月日格式化时间
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);
注意:最后一个参数是纳秒
2.3 获取时区东八区的时间
localdatetime datetime = localdatetime.now(zoneid.of("asia/shanghai"));
localdatetime datetime2 = localdatetime.now(zoneid.of("+8"));
2.4 从localdatetime中获取年月日等时间信息
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);
2.5 从localdatetime中获取localdate与localtime
localdate localdate = now.tolocaldate(); localtime localtime = now.tolocaltime();
2.6 日期计算
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 日期对象每次修改都会创建一个新的对象,而非在原有对象基础上修改
2.7 获取某天的整点时间,通过with修改值
localdatetime now = localdatetime.now(); localdatetime ldt1 = now.withhour(10).withminute(0).withsecond(0).withnano(0);
2.8 获取毫秒值
instant instant = instant.now(); long min = instant.toepochmilli(); system.out.println(min);
2.9 计算两个时间的时间差
下面这种方式可以获取两个时间点相差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());
3. localdatetime与date、string互转
3.1 localdatetime 转 date
public static date localdatetime2date(localdatetime localdatetime) {
return date.from(localdatetime.atzone(zoneid.systemdefault()).toinstant());
}
3.2 localdatetime 转 string
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是线程安全的类,可以将此类放到常量中。
3.3 string 转 localdatetime
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);
}
3.4 string 转 date
public static date string2date(string str) throws parseexception {
simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss");
return simpledateformat.parse(str);
}
注意:simpledateformat是非线程安全的类,在多线程操作时会报错,所以此对象每次用的时候最好新建。或者作为方法入参传递进来。
3.5 date 转 localdatetime
public static localdatetime date2localdatetime(date date) {
return date.toinstant()
.atzone(zoneid.systemdefault())
.tolocaldatetime();
}
3.6 date 转 string
public static string date2string(date date) {
simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
return formatter.format(date);
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论