首页 > 编程语言 > 详细

java8时间工具类

时间:2019-03-07 13:07:26      阅读:239      评论:0      收藏:0      [点我收藏+]
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();
	}
	
	/**
	 * 
	 * <p>Title: getDateNow</p>
	 * <p>Description: 获取当前时间   格式yyyymmddhhmmss</p>
	 * @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);
	}
	
	/**
	 * 
	 * <p>Title: format</p>
	 * <p>Description: 时间格式化</p>
	 * @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);
	}
	
	/**
	 * 
	 * <p>Title: tranfer</p>
	 * <p>Description: 时间类型转换</p>
	 * @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);
	}
	
	/**
	 * 
	 * <p>Title: betweenTimes</p>
	 * <p>Description: 求两个时间之间的差值(秒)</p>
	 * @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);
	}
	
	/**
	 * 
	 * <p>Title: isBefore</p>
	 * <p>Description: 两个时间比较</p>
	 * @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);
	}
	
	
}

  参考 https://github.com/biezhi/java-bible/blob/master/java8/java8-guide.md



java8时间工具类

原文:https://www.cnblogs.com/nihaofenghao/p/10488577.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!