java 日期工具_Java日期工具类(最全)

importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.time.LocalDate;importjava.time.LocalDateTime;importjava.time.Period;importjava.time.ZoneId;importjava.time.format.DateTimeFormatter;importjava.time.temporal.ChronoUnit;importjava.time.temporal.TemporalUnit;importjava.util.Calendar;importjava.util.Date;public classDateUtils {private static final String DATE_FORMAT = "yyyy-MM-dd";private static final String TIME_FORMAT = "HH:mm:ss";private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");/*** 格式化日期

*

*@paramdate

*@returnyyyy-MM-dd*/

public staticString getFormatDate(Date date) {

SimpleDateFormat format= newSimpleDateFormat(DATE_FORMAT);returnformat.format(date);

}/*** 格式化时间

*

*@paramdate

*@returnHH:mm:ss*/

public staticString getFormatTime(Date date) {

SimpleDateFormat format= newSimpleDateFormat(TIME_FORMAT);returnformat.format(date);

}/*** 格式化日期及时间

*

*@paramdate

*@returnyyyy-MM-dd HH:mm:ss*/

public staticString getFormatDateTime(Date date) {

SimpleDateFormat format= newSimpleDateFormat(DATE_TIME_FORMAT);returnformat.format(date);

}/*** 格式化日期

*

*@paramlocalDate

*@return

*/

public staticString getFormatDate(LocalDate localDate) {returnlocalDate.format(DATE_FORMATTER);

}/*** 格式化时间

*

*@paramlocalDate

*@return

*/

public staticString getFormatTime(LocalDateTime localDateTime) {returnlocalDateTime.format(TIME_FORMATTER);

}/*** 格式化日期及时间

*

*@paramlocalDate

*@return

*/

public staticString getFormatDateTime(LocalDateTime localDateTime) {returnlocalDateTime.format(DATE_TIME_FORMATTER);

}/*** 日期时间字符串转LocalDateTime

*

*@paramdateStr

*@return

*/

public staticLocalDateTime dateStrToLocalDateTime(String dateStr) {returnLocalDateTime.parse(dateStr, DATE_TIME_FORMATTER);

}/*** 日期字符串转LocalDate

*

*@paramdateStr

*@return

*/

public staticLocalDate dateStrToLocalDate(String dateStr) {returnLocalDate.parse(dateStr, DATE_FORMATTER);

}/*** 日期字符串 转 Date

*

*@paramdateStr

*@return

*/

public staticDate dateStrToDate(String dateStr) {returnDate.from(LocalDate.parse(dateStr, DATE_FORMATTER).atStartOfDay(ZoneId.systemDefault()).toInstant());

}/*** 日期时间字符串 转 Date

*

*@paramdateStr

*@return

*/

public staticDate dateTimeStrToDate(String dateStr) {returnDate.from(LocalDateTime.parse(dateStr, DATE_TIME_FORMATTER).atZone(ZoneId.systemDefault()).toInstant());

}/*** LocalDate 转 Date

*

*@paramlocalDate

*@return

*/

public staticDate localDateToDate(LocalDate localDate) {returnDate.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

}/*** LocalDateTime 转 Date

*

*@paramlocalDateTime

*@return

*/

public staticDate localDateTimeToDate(LocalDateTime localDateTime) {returnDate.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

}/*** Date 转 LocalDate

*

*@return

*/

public staticLocalDate dateToLocalDate(Date date) {returndate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

}/*** Date 转 LocalDateTime

*

*@paramdate

*@return

*/

public staticLocalDateTime dateToLocalDateTime(Date date) {returndate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

}/*** 计算两个日期时间差

*

*@paramstart yyyy-MM-dd HH:mm:ss

*@paramend yyyy-MM-dd HH:mm:ss

*@return

*/

public static longdateDifference(String start, String end) {

SimpleDateFormat format= newSimpleDateFormat(DATE_TIME_FORMAT);try{

Date startTime=format.parse(start);

Date endTime=format.parse(end);return endTime.getTime() -startTime.getTime();

}catch(ParseException e) {

e.printStackTrace();

}return -1;

}/*** 计算两个日期之间的天数差

*

*@paramstart 2018-03-01 12:00:00

*@paramend 2018-03-12 12:00:00

*@return

*/

public static longcalculationDays(String start, String end) {

SimpleDateFormat simpleFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try{

Date fromDate=simpleFormat.parse(start);

Date toDate=simpleFormat.parse(end);long from =fromDate.getTime();long to =toDate.getTime();long days = (int) ((to - from) / (1000 * 60 * 60 * 24));returndays;

}catch(ParseException e) {

e.printStackTrace();

}return -1;

}/*** 计算小时差

*

*@paramstart

*@paramend

*@return

*/

public static longcalculationHours(String start, String end) {

SimpleDateFormat simpleFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try{

Date fromDate=simpleFormat.parse(start);

Date toDate=simpleFormat.parse(end);long from =fromDate.getTime();long to =toDate.getTime();long hours = (int) ((to - from) / (1000 * 60 * 60));returnhours;

}catch(ParseException e) {

e.printStackTrace();

}return -1;

}/*** 计算分钟差

*

*@paramstart

*@paramend

*@return

*/

public static longcalculationMinutes(String start, String end) {

SimpleDateFormat simpleFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try{

Date fromDate=simpleFormat.parse(start);

Date toDate=simpleFormat.parse(end);long from =fromDate.getTime();long to =toDate.getTime();long minutes = (int) ((to - from) / (1000 * 60));returnminutes;

}catch(ParseException e) {

e.printStackTrace();

}return -1;

}/*** 计算两个日期之间的秒数差

*@paramstart

*@paramend

*@return

*/

public static longcalculationSecond(String start, String end) {

SimpleDateFormat simpleFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try{

Date fromDate=simpleFormat.parse(start);

Date toDate=simpleFormat.parse(end);long from =fromDate.getTime();long to =toDate.getTime();long seconds = (int) ((to - from) / 1000);returnseconds;

}catch(ParseException e) {

e.printStackTrace();

}return -1;

}/*** 获取两个日期的差 field参数为ChronoUnit.*

*@paramstartTime

*@paramendTime

*@paramfield 单位(年月日时分秒)

*@return

*/

public static longbetweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {

Period period=Period.between(LocalDate.from(startTime), LocalDate.from(endTime));if (field ==ChronoUnit.YEARS)returnperiod.getYears();if (field ==ChronoUnit.MONTHS)return period.getYears() * 12 +period.getMonths();returnfield.between(startTime, endTime);

}/*** 日期加上一个数,根据field不同加不同值,field为ChronoUnit.*

*@paramtime

*@paramnumber

*@paramfield

*@return

*/

public static LocalDateTime plus(LocalDateTime time, longnumber, TemporalUnit field) {returntime.plus(number, field);

}/*** 日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*

*@paramtime

*@paramnumber

*@paramfield

*@return

*/

public static LocalDateTime minu(LocalDateTime time, longnumber, TemporalUnit field){returntime.minus(number,field);

}/*** 根据field不同加减不同值

*@paramdate

*@paramfield Calendar.YEAR

*@paramnumber 1000/-1000*/

public static Date calculationDate(Date date, int field, intnumber) {

Calendar calendar=Calendar.getInstance();

calendar.add(field, number);returncalendar.getTime();

}/*** 比较两个日期先后

*@paramdate1

*@paramdate2

*@return

*/

public static booleancompareDate(Date firstDate, Date secondDate) {return firstDate.getTime()

}/*** 比较第一个日期是否大于第二个日期

*@paramfirstDate 第一个日期

*@paramsecondDate 第二个日期

*@returntrue-大于;false-不大于*/

public booleanlocalDateIsBefore(LocalDate firstDate, LocalDate secondDate) {returnfirstDate.isBefore(secondDate);

}public static voidmain(String[] args) {

Date date= newDate();

LocalDateTime localDate=DateUtils.dateToLocalDateTime(date);

System.out.println(localDate);

String start= "2020-03-01 12:00:00";

String end= "2022-03-12 12:01:00";long seconds =DateUtils.calculationSecond(start, end);

System.out.println("相差秒数: " +seconds);long minutes =DateUtils.calculationMinutes(start, end);

System.out.println("相差分钟数: " +minutes);long days =DateUtils.calculationDays(start, end);

System.out.println("相差天数: " +days);

LocalDateTime startTime= LocalDateTime.of(2000, 10, 13, 11, 11);

LocalDateTime endTime= LocalDateTime.of(2000, 10, 14, 13, 13);long time =DateUtils.betweenTwoTime(startTime, endTime, ChronoUnit.SECONDS);

System.out.println(time);

date= DateUtils.calculationDate(date, Calendar.DATE, -10);

System.out.println("减去10天: " +DateUtils.getFormatDate(date));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值