欢迎来到天天文库
浏览记录
ID:30775973
大小:102.50 KB
页数:14页
时间:2019-01-03
《java编程时间日期api实例解析》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、Java编程时间日期API实例解析本文实例主要是关于Java8中的新特性,吋间日期api的相关实例,具体如下:,苦二茁苻二若987654321packagecom・effective・common,base・date;importjava.time.Tnstant;importjava.time.LocalDate;importjava,time・LocalDateTime;importjava・time・LocalTime;importjava・time・Period;importjava,time・Zoneld;importjava,time.format.Date
2、TimeFormatter;importjava.time,temporal.ChronoUnit;importjava.util.Date;/***日期工具类*@authoryanweiqi*@since2016-5-6**/publicclassLocalDatelltils{privatestaticZoneldzone=Zoneld・systemDcfault();/***字符串转Date*@paramdate*^return*@throwsException*/publicstaticDateconvertToDste(Stringdate)throwsExc
3、eption{LocalDatelocalDate=nul1;if(nul1==date){thrownewNullPointerException("dateisn'tnull");}else{localDate=LocalDate.parse(date);returnconvertToDate(localDate);}}/***字符串转LocalDateTime*@paramdate*@returnlocalDateTime*/publicstaticLocalDateTimeconvertToLocalDateTime(Stringdate){LocalDateT
4、imelocalDateTime=nul1;if(null二二date){thrownowNullPointerException("dateisn'tnull");}else{return1ocaIDateTime;/***LocalDate转Date*@par8iTilocalDate*^returnDate*/publicstaticDateconvertToDate(LocalDatelocalDate){Instantinstant=localDate.atStartOfDay().atZone(zone).tolnstant();returnDate,fro
5、m(instant);}/***LocalDate转Date*@paTam1ocaIDateTime*©returnDate*/publicstaticDateconvertToDate(LocalDateTimelocalDateTime){Instantinstant=localDateTime.atZone(zone).toTnstant();returnDate,from(instant);}/***@paramdate*@returnlocalDate*/publicstaticLocalDateconvertToLocalDate(Datcdate){Ins
6、tantinstant=date・tolnstant();returnconvertToLocalDateTime(instant)・toLocalDate();}/***Date转LocalTime*@paramdate*localDate*/publicstaticLocalTimeconvertToLocalTime(Datedate){Instantinstant=date.tolnstant();returnconvertToLocalDateTime(instant).toLocalTimeO;}/***DateLocalDatetime*@paramdat
7、e*©return1oca.1Date*/publicstaticLocalDateTimcconvertToLocalDateTime(Datedate){Instantinstant=date・tolnstant();/***Instant转LocaIDateTime*©paraminstant*@return*/publicstaticLocalDateTimeconvertToLocalDateTime(Tnstantinstant){returnLocalDateTime.ofInstant(instant,zone);}/**
此文档下载收益归作者所有