zoukankan      html  css  js  c++  java
  • 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();
    	}
    	
    	/**
    	 * 
    	 * <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);
    	}
    	
    	
    }
    
    /**
    	 * 
    	 * <p>Title: addMinutes</p>
    	 * <p>Description: 当前时间添加分钟</p>
    	 * @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);
    	}
    	
    	/**
    	 * 
    	 * <p>Title: localDateTime2Date</p>
    	 * <p>Description: localdatetime转Date</p>
    	 * @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 static LocalDate conversion(String date){
        return LocalDate.parse(date, 
        DateTimeFormatter.ofPattern(DATE));
    }

      

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



  • 相关阅读:
    第一次留下自己的随笔
    NSOperation多线程方式
    NSThread多线程方式
    GCD多线程机制
    GCD创建单例常用的两种方法
    线程锁
    使用KVO模式,设置应用角标数字
    keyChain RSA加密 KVO
    NSURLConnection网络处理和NSURLSession网络处理
    UIAlertView和UIAlertViewController
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/10488577.html
Copyright © 2011-2022 走看看