Java编程时间日期API实例解析
本文实例主要是关于Java8中的新特性,时间日期api的相关实例,具体如下:
packagecom.effective.common.base.date; importjava.time.Instant; importjava.time.LocalDate; importjava.time.LocalDateTime; importjava.time.LocalTime; importjava.time.Period; importjava.time.ZoneId; importjava.time.format.DateTimeFormatter; importjava.time.temporal.ChronoUnit; importjava.util.Date; /** *日期工具类 *@authoryanweiqi *@since2016-5-6 * */ publicclassLocalDateUtils{ privatestaticZoneIdzone=ZoneId.systemDefault(); /** *字符串转Date *@paramdate *@return *@throwsException */ publicstaticDateconvertToDate(Stringdate)throwsException{ LocalDatelocalDate=null; if(null==date){ thrownewNullPointerException("dateisn'tnull"); }else{ localDate=LocalDate.parse(date); returnconvertToDate(localDate); } } /** *字符串转LocalDateTime *@paramdate *@returnlocalDateTime */ publicstaticLocalDateTimeconvertToLocalDateTime(Stringdate){ LocalDateTimelocalDateTime=null; if(null==date){ thrownewNullPointerException("dateisn'tnull"); }else{ localDateTime=LocalDateTime.parse(date); returnlocalDateTime; } } /** *LocalDate转Date *@paramlocalDate *@returnDate */ publicstaticDateconvertToDate(LocalDatelocalDate){ Instantinstant=localDate.atStartOfDay().atZone(zone).toInstant(); returnDate.from(instant); } /** *LocalDate转Date *@paramlocalDateTime *@returnDate */ publicstaticDateconvertToDate(LocalDateTimelocalDateTime){ Instantinstant=localDateTime.atZone(zone).toInstant(); returnDate.from(instant); } /** *Date转LocalDate *@paramdate *@returnlocalDate */ publicstaticLocalDateconvertToLocalDate(Datedate){ Instantinstant=date.toInstant(); returnconvertToLocalDateTime(instant).toLocalDate(); } /** *Date转LocalTime *@paramdate *@returnlocalDate */ publicstaticLocalTimeconvertToLocalTime(Datedate){ Instantinstant=date.toInstant(); returnconvertToLocalDateTime(instant).toLocalTime(); } /** *Date转LocalDatetime *@paramdate *@returnlocalDate */ publicstaticLocalDateTimeconvertToLocalDateTime(Datedate){ Instantinstant=date.toInstant(); returnconvertToLocalDateTime(instant); } /** *Instant转LocalDateTime *@paraminstant *@return */ publicstaticLocalDateTimeconvertToLocalDateTime(Instantinstant){ returnLocalDateTime.ofInstant(instant,zone); } /** *LocalDateTime转Instant *@paramlocalDateTime *@return */ publicstaticInstantconvertToInstant(LocalDateTimelocalDateTime){ returnlocalDateTime.atZone(zone).toInstant(); } /** *LocalDate转Instant *@paramlocalDate *@return */ publicstaticInstantconvertToInstant(LocalDatelocalDate){ returnlocalDate.atStartOfDay(zone).toInstant(); } /** *LocalDate转LocalDateTime *@paramlocalDate *@returnLocalDateTime */ publicstaticLocalDateTimeconvertToLocalDateTime(LocalDatelocalDate){ returnlocalDate.atStartOfDay(); } /** *日周期格式化 *@paramlocalDateTime *@paramformatStyle *@return */ publicstaticStringformatter(LocalDateTimelocalDateTime,StringformatStyle){ returnDateTimeFormatter.ofPattern(formatStyle).format(localDateTime); } /** *设置年 *@paramsourceDate *@paramyear *@returnLocalDateTime */ publicstaticLocalDateTimesetYear(LocalDateTimesourceDate,Integeryear){ returnsourceDate.withYear(year); } /** *设置月 *@paramsourceDate *@parammonth *@returnLocalDateTime */ publicstaticLocalDateTimesetMonth(LocalDateTimesourceDate,Integermonth){ returnsourceDate.withMonth(month); } /** *设置天 *@paramsourceDate *@parammonth *@returnLocalDateTime */ publicstaticLocalDateTimesetDayOfMonth(LocalDateTimesourceDate,IntegerdayOfMonth){ returnsourceDate.withDayOfMonth(dayOfMonth); } /** *设置小时 *@paramsourceDate *@paramhour *@return */ publicstaticLocalDateTimesetHour(LocalDateTimesourceDate,Integerhour){ returnsourceDate.withHour(hour); } /** *设置分钟 *@paramsourceDate *@paramminute *@return */ publicstaticLocalDateTimesetMinute(LocalDateTimesourceDate,Integerminute){ returnsourceDate.withMinute(minute); } /** *设置秒 *@paramsourceDate *@paramsecond *@return */ publicstaticLocalDateTimesetSecond(LocalDateTimesourceDate,Integersecond){ returnsourceDate.withSecond(second); } /** *修改年月日 *@paramsourceDate *@paramyear *@parammonth *@paramdayOfMonth *@return */ publicstaticLocalDateTimesetYMD(LocalDateTimesourceDate,Integeryear,Integermonth,IntegerdayOfMonth){ returnsourceDate.withYear(year).withMonth(month).withDayOfMonth(dayOfMonth); } /** *修改时分秒 *@paramsourceDate *@paramhour *@paramminute *@paramsecond *@return */ publicstaticLocalDateTimesetHMS(LocalDateTimesourceDate,Integerhour,Integerminute,Integersecond){ returnsourceDate.withHour(hour).withMinute(minute).withSecond(second); } /** *计算相差的天数 *@parambeginDate *@paramendDate *@return */ publicstaticintgetInteverDays(LocalDatebeginDate,LocalDateendDate){ Periodperiod=Period.between(beginDate,endDate); returnperiod.getDays(); } /** *日期加减 *@paramnum数量 *@paramunit单位 *@paramLocalDate原日期 *@returnLocalDate增加后的日期 */ @SuppressWarnings("static-access") publicstaticLocalDateaddLocalDate(longnum,ChronoUnitunit,finalLocalDatelocalDate){ LocalDateresultDate; if(num>0){ resultDate=localDate.now().plus(num,unit); }else{ resultDate=localDate.now().minus(Math.abs(num),unit); } returnresultDate; } /** *日期时分秒加 *@paramnum数量 *@paramunit单位 *@paramlocalDateTime原日期 *@returnLocalDateTime增加后的日期 */ @SuppressWarnings("static-access") publicstaticLocalDateTimeaddLocalDateTime(longnum,ChronoUnitunit,LocalDateTimelocalDateTime){ LocalDateTimeresultDateTime; if(num>0){ resultDateTime=localDateTime.now().plus(num,unit); }else{ resultDateTime=localDateTime.now().minus(Math.abs(num),unit); } returnresultDateTime; } /** *时分秒加减 *@paramnum数量 *@paramunit单位 *@paramlocalTime原日期 *@returnLocalDateTime增加后的日期 */ @SuppressWarnings("static-access") publicstaticLocalTimeaddLocalTime(longnum,ChronoUnitunit,LocalTimelocalTime){ LocalTimeresultTime; if(num>0){ resultTime=localTime.now().plus(num,unit); }else{ resultTime=localTime.now().minus(Math.abs(num),unit); } returnresultTime; } publicstaticvoidmain(String[]args){ LocalDateTimetime=LocalDateTime.now(); Stringrr=formatter(time,"yyyy-MM-ddHH:mm:ss"); System.out.println(rr); LocalDateTimetime2=addLocalDateTime(-2,ChronoUnit.HOURS,time); Stringyy=formatter(time2,"yyyy-MM-ddHH:mm:ss"); System.out.println(yy); }
代码涉及时间日期类的使用等内容,具有简单注释,大家可自行参考。
总结
以上就是本文关于Java编程时间日期API实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!