java8 time工具_java8时间工具类

package com.voole.platform.util;

import java.time.Instant;

import java.time.LocalDateTime;

import java.time.ZoneId;

import java.time.format.DateTimeFormatter;

import java.time.temporal.ChronoUnit;

import java.util.Calendar;

import java.util.Date;

import org.junit.Test;

public class DateUtils {

private final static String DATE_TIME_FORMAT = "yyyymmddhhmmss";

private final static String DATE_TIME_FORMAT_1 = "yyyy-MM-dd HH:mm:ss";

/**

* 计算起始日期间的相隔天数

* @param starttime

* @param endtime

* @return

*/

public static long getTimeDayDiff(Date starttime, Date endtime){

long nd = 1000 * 24 * 60 * 60;

long diff = endtime.getTime() - starttime.getTime();

long day = diff / nd;

return day;

}

/**

* 获取给定日期增加给定天数后的日期

* @param date

* @param hour

* @return

*/

public static Date addHour(Date date, int hour){

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

long millis = calendar.getTimeInMillis() + ((long) hour) * 3600 * 1000;

calendar.setTimeInMillis(millis);

return calendar.getTime();

}

/**

*

*

Title: getDateNow

*

Description: 获取当前时间 格式yyyymmddhhmmss

* @return

* @author 冯浩 2019年2月25日 下午5:36:56

*/

public static String getDateNow() {

LocalDateTime date = LocalDateTime.now();

DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT);

return date.format(ofPattern);

}

/**

*

*

Title: format

*

Description: 时间格式化

* @param date

* @return

* @author 冯浩 2019年3月7日 上午11:10:05

*/

public static String format(Date date) {

DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT_1);

LocalDateTime tranfer = tranfer(date);

return tranfer.format(ofPattern);

}

/**

*

*

Title: tranfer

*

Description: 时间类型转换

* @param date

* @return

* @author 冯浩 2019年3月7日 上午11:10:33

*/

public static LocalDateTime tranfer(Date date) {

Instant instant = date.toInstant();

ZoneId systemDefault = ZoneId.systemDefault();

return LocalDateTime.ofInstant(instant, systemDefault);

}

/**

*

*

Title: betweenTimes

*

Description: 求两个时间之间的差值(秒)

* @param start

* @param end

* @return

* @author 冯浩 2019年3月7日 上午11:27:32

*/

public static long betweenTimes(Date start,Date end) {

LocalDateTime starttime = tranfer(start);

LocalDateTime endtime = tranfer(end);

return ChronoUnit.SECONDS.between(starttime, endtime);

}

/**

*

*

Title: isBefore

*

Description: 两个时间比较

* @param start

* @param end

* @return

* @author 冯浩 2019年3月7日 上午11:33:20

*/

public static boolean isBefore(Date start,Date end) {

LocalDateTime starttime = tranfer(start);

LocalDateTime endtime = tranfer(end);

return starttime.isBefore(endtime);

}

@Test

public void test() {

Date addHour = DateUtils.addHour(new Date(), 40);

//long betweenTimes = DateUtils.betweenTimes(new Date(), addHour);

boolean before = DateUtils.isBefore(new Date(), addHour);

System.out.println(before);

}

}

/**

*

*

Title: addMinutes

*

Description: 当前时间添加分钟

* @param minutes

* @return

* @author 冯浩 2019年3月7日 下午7:12:24

*/

public static Date addMinutes(int minutes) {

LocalDateTime now = LocalDateTime.now();

LocalDateTime plusMinutes = now.plusMinutes(minutes);

return localDateTime2Date(plusMinutes);

}

/**

*

*

Title: localDateTime2Date

*

Description: localdatetime转Date

* @param time

* @return

* @author 冯浩 2019年3月7日 下午7:12:45

*/

public static Date localDateTime2Date(LocalDateTime time) {

ZoneId systemDefault = ZoneId.systemDefault();

ZonedDateTime atZone = time.atZone(systemDefault);

return Date.from(atZone.toInstant());

}

public staticLocalDate conversion(String date){returnLocalDate.parse(date,

DateTimeFormatter.ofPattern(DATE));

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值