java8 日期时间处理
Posted 1501220038
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java8 日期时间处理相关的知识,希望对你有一定的参考价值。
之前有个项目频繁用到年、月、周的数据库统计。为此专门写了一个蛮复杂的工具类。最近了解了一下java8的新日期时间类库,简直方便到感人。重构了一下代码,行数少了多一半,有些方法直接删掉了,因为已经自带了。适用多种业务场景:展示、统计、设置有效期、增加/减少日期、日期比较等。
package com.utils.zjc;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
public class DateMethod8
//把毫秒数转换为标准日期时间字符串
public String formatMilliSecond(long milliSeconds)
ZoneId z=ZoneId.systemDefault();
Instant instant = Instant.now();
LocalDateTime datetime = LocalDateTime.ofEpochSecond(milliSeconds/1000, 0,z.getRules().getOffset(instant));
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return datetime.format(formatter);
//获取当前日期时间字符串
public String getNowDateAndTimeString()
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return now.format(formatter);
//获取当前日期字符串
public String getNowDateString()
LocalDate today=LocalDate.now();
return today.toString();
//获取当前时间字符串
public String getNowTimeString()
LocalTime now = LocalTime.now();
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("HH:mm:ss");
return now.format(formatter);
//获取某年某月第一天
public String getYearMonthFirstDayString(int year, int month) throws Exception
if(month<1||month>12)
throw new Exception("invalid parameters");
Integer iyear = Integer.valueOf(year);
Integer imonth = Integer.valueOf(month);
if(month<10)
return iyear.toString()+"-0"+imonth.toString()+"-01 00:00:00";
else
return iyear.toString()+"-"+imonth.toString()+"-01 00:00:00";
//获取某年某月最后一天
public String getYearMonthLastDayString(int year, int month) throws Exception
if(month<1||month>12)
throw new Exception("invalid parameters");
LocalDate date = LocalDate.of(year,month,1);
Integer lastday=date.getMonth().length(date.isLeapYear());
Integer iyear = Integer.valueOf(year);
Integer imonth = Integer.valueOf(month);
if(month<10)
return iyear.toString()+"-0"+imonth.toString()+"-"+lastday.toString()+" 23:59:59";
else
return iyear.toString()+"-"+imonth.toString()+"-"+lastday.toString()+" 23:59:59";
//获取某年某月某日所在周的某一天
public String getWeekDayString(int year, int month, int day, DayOfWeek dow) throws Exception
try
LocalDate date = LocalDate.of(year,month,day);
LocalDate newDate = date.with(TemporalAdjusters.nextOrSame(dow));
return newDate.toString();
catch(Exception e)
throw new Exception("invalid parameters");
public static void main(String[] args)
LocalDateTime now = LocalDateTime.now();
LocalDateTime nextMonth = now.plusMonths(1); //向后延续一个月
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(nextMonth.format(formatter));
int compareResult = nextMonth.compareTo(now); //比较日期和时间
if(compareResult>0)
System.out.println("晚于当前");
else if(compareResult==0)
System.out.println("相同");
else
System.out.println("早于当前");
;
LocalDate temp = LocalDate.now();
System.out.println(temp.with(TemporalAdjusters.lastDayOfMonth()));//月末一天的另一种方法
Duration dtime = Duration.between(now, nextMonth); //计算时间间隔
long seconds = dtime.getSeconds();//秒表示
long millis = dtime.toMillis();//毫秒表示
System.out.println(seconds);
System.out.println(millis);
try
LocalDate newDate = LocalDate.of(2005,2,28); //验证日期有效性
LocalDateTime newTime = LocalDateTime.of(newDate, LocalTime.now());
System.out.println("日期有效");
System.out.println(newTime.format(formatter));
catch(Exception e)
System.out.println("日期无效");
以上是关于java8 日期时间处理的主要内容,如果未能解决你的问题,请参考以下文章