Java编程时间日期API实例解析
程序员文章站
2023-12-15 16:10:16
本文实例主要是关于java8中的新特性,时间日期api的相关实例,具体如下:
package com.effective.common.base.date;
i...
本文实例主要是关于java8中的新特性,时间日期api的相关实例,具体如下:
package com.effective.common.base.date; import java.time.instant; import java.time.localdate; import java.time.localdatetime; import java.time.localtime; import java.time.period; import java.time.zoneid; import java.time.format.datetimeformatter; import java.time.temporal.chronounit; import java.util.date; /** * 日期工具类 * @author yanweiqi * @since 2016-5-6 * */ public class localdateutils { private static zoneid zone = zoneid.systemdefault(); /** * 字符串转date * @param date * @return * @throws exception */ public static date converttodate(string date) throws exception{ localdate localdate = null; if(null == date){ throw new nullpointerexception("date isn't null"); } else { localdate = localdate.parse(date); return converttodate(localdate); } } /** * 字符串转localdatetime * @param date * @return localdatetime */ public static localdatetime converttolocaldatetime(string date){ localdatetime localdatetime = null; if(null == date){ throw new nullpointerexception("date isn't null"); } else { localdatetime = localdatetime.parse(date); return localdatetime; } } /** * localdate转date * @param localdate * @return date */ public static date converttodate(localdate localdate){ instant instant = localdate.atstartofday().atzone(zone).toinstant(); return date.from(instant); } /** * localdate转date * @param localdatetime * @return date */ public static date converttodate(localdatetime localdatetime){ instant instant = localdatetime.atzone(zone).toinstant(); return date.from(instant); } /** * date转localdate * @param date * @return localdate */ public static localdate converttolocaldate(date date){ instant instant = date.toinstant(); return converttolocaldatetime(instant).tolocaldate(); } /** * date转localtime * @param date * @return localdate */ public static localtime converttolocaltime(date date){ instant instant = date.toinstant(); return converttolocaldatetime(instant).tolocaltime(); } /** * date转localdatetime * @param date * @return localdate */ public static localdatetime converttolocaldatetime(date date){ instant instant = date.toinstant(); return converttolocaldatetime(instant); } /** * instant转localdatetime * @param instant * @return */ public static localdatetime converttolocaldatetime(instant instant){ return localdatetime.ofinstant(instant, zone); } /** * localdatetime转instant * @param localdatetime * @return */ public static instant converttoinstant(localdatetime localdatetime){ return localdatetime.atzone(zone).toinstant(); } /** * localdate转instant * @param localdate * @return */ public static instant converttoinstant(localdate localdate){ return localdate.atstartofday(zone).toinstant(); } /** * localdate转localdatetime * @param localdate * @return localdatetime */ public static localdatetime converttolocaldatetime(localdate localdate){ return localdate.atstartofday(); } /** * 日周期格式化 * @param localdatetime * @param formatstyle * @return */ public static string formatter(localdatetime localdatetime, string formatstyle){ return datetimeformatter.ofpattern(formatstyle).format(localdatetime); } /** * 设置年 * @param sourcedate * @param year * @return localdatetime */ public static localdatetime setyear(localdatetime sourcedate, integer year){ return sourcedate.withyear(year); } /** * 设置月 * @param sourcedate * @param month * @return localdatetime */ public static localdatetime setmonth(localdatetime sourcedate, integer month){ return sourcedate.withmonth(month); } /** * 设置天 * @param sourcedate * @param month * @return localdatetime */ public static localdatetime setdayofmonth(localdatetime sourcedate, integer dayofmonth){ return sourcedate.withdayofmonth(dayofmonth); } /** * 设置小时 * @param sourcedate * @param hour * @return */ public static localdatetime sethour(localdatetime sourcedate,integer hour){ return sourcedate.withhour(hour); } /** * 设置分钟 * @param sourcedate * @param minute * @return */ public static localdatetime setminute(localdatetime sourcedate,integer minute){ return sourcedate.withminute(minute); } /** * 设置秒 * @param sourcedate * @param second * @return */ public static localdatetime setsecond(localdatetime sourcedate,integer second){ return sourcedate.withsecond(second); } /** * 修改年月日 * @param sourcedate * @param year * @param month * @param dayofmonth * @return */ public static localdatetime setymd(localdatetime sourcedate, integer year, integer month, integer dayofmonth) { return sourcedate.withyear(year).withmonth(month).withdayofmonth(dayofmonth); } /** * 修改时分秒 * @param sourcedate * @param hour * @param minute * @param second * @return */ public static localdatetime sethms(localdatetime sourcedate,integer hour, integer minute, integer second) { return sourcedate.withhour(hour).withminute(minute).withsecond(second); } /** * 计算相差的天数 * @param begindate * @param enddate * @return */ public static int getinteverdays(localdate begindate,localdate enddate){ period period = period.between(begindate, enddate); return period.getdays(); } /** * 日期加减 * @param num 数量 * @param unit 单位 * @param localdate 原日期 * @return localdate 增加后的日期 */ @suppresswarnings("static-access") public static localdate addlocaldate(long num,chronounit unit,final localdate localdate){ localdate resultdate; if(num > 0){ resultdate = localdate.now().plus(num, unit); } else { resultdate = localdate.now().minus(math.abs(num), unit); } return resultdate; } /** * 日期时分秒加 * @param num 数量 * @param unit 单位 * @param localdatetime 原日期 * @return localdatetime 增加后的日期 */ @suppresswarnings("static-access") public static localdatetime addlocaldatetime(long num,chronounit unit,localdatetime localdatetime){ localdatetime resultdatetime; if(num > 0){ resultdatetime = localdatetime.now().plus(num, unit); } else { resultdatetime = localdatetime.now().minus(math.abs(num),unit); } return resultdatetime; } /** * 时分秒加减 * @param num 数量 * @param unit 单位 * @param localtime 原日期 * @return localdatetime 增加后的日期 */ @suppresswarnings("static-access") public static localtime addlocaltime(long num,chronounit unit,localtime localtime){ localtime resulttime; if(num > 0){ resulttime = localtime.now().plus(num, unit); } else { resulttime = localtime.now().minus(math.abs(num), unit); } return resulttime; } public static void main(string[] args){ localdatetime time = localdatetime.now(); string rr = formatter(time, "yyyy-mm-dd hh:mm:ss"); system.out.println(rr); localdatetime time2 = addlocaldatetime(-2, chronounit.hours, time); string yy = formatter(time2, "yyyy-mm-dd hh:mm:ss"); system.out.println(yy); }
代码涉及时间日期类的使用等内容,具有简单注释,大家可自行参考。
总结
以上就是本文关于java编程时间日期api实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!