Java 时间工具类

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

	public static String getRandom() {
		String Random=String.valueOf((int)((Math.random()*9+1)*10000));
		return Random;
	}

    private static long nd = 86400000;//每天毫秒数
    private static long nh = 3600000;//每小时毫秒数
    private static long nm = 60000;//每分钟毫秒数

    /**
     * 返回两个时间相差 :0天1小时1分钟
     *
     * @throws ParseException
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long diff = endDate.getTime() - nowDate.getTime(); // 获得两个时间的毫秒时间差异
        long day = diff / nd;   // 计算差多少天
        long hour = diff % nd / nh; // 计算差多少小时
        long min = diff % nd % nh / nm;  // 计算差多少分钟
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 返回两个时间相差分钟数
     *
     * @throws ParseException
     */
	public static int getDatemMin(Date endDate, Date nowDate) {
        long diff = endDate.getTime() - nowDate.getTime(); // 获得两个时间的毫秒时间差异
//		long day = diff / nd;   // 计算差多少天
//		long hour = diff % nd / nh; // 计算差多少小时
        long min = diff / nm;  // 计算差多少分钟
		return (int) min;
    }



	/**
	 * 获取当前时间  20180110102701
	 * @throws ParseException 
	*/
	public static String getNewTimeyMd( ) throws ParseException{
		//有多个重载方法创建 Calendar 对象
		  Calendar now = Calendar.getInstance(); //默认
		 //指定时区和地区,也可以只输入其中一个参数
//		  Calendar now = Calendar.getInstance(timeZone, locale); 
//		  int year = now.get(Calendar.YEAR); //2015,当前年份
//		  System.out.println("当前年份:"+year);
//		  int month = now.get(Calendar.MONTH) + 1; //12,当前月,注意加 1
//		  System.out.println("当前月:"+month);
//		  int day = now.get(Calendar.DATE); //23,当前日
//		  System.out.println("当前日:"+day);
		  
		  SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		  Date date = now.getTime(); //直接取得一个 Date 类型的日期
		 
		return formatter.format(date);
	}
	
	/**
	 * 获取当前时间  2018-01-10
	 * @throws ParseException 
	*/
	public static String getNewDate( ) throws ParseException{
		  Calendar now = Calendar.getInstance(); //默认
		  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		  Date date = now.getTime(); //直接取得一个 Date 类型的日期
		 
		return formatter.format(date);
	}
	
	/**
	 *  格式化当前默认日期时间   yyyy-MM-DD
	 * @throws ParseException 
	*/
	public static String getFormatTime(Date date) throws ParseException{
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(date);
	}
	
	
	
	/**
	* 获取当前时间的前30分钟
	 * @throws ParseException 
	*/
	public static String getNewTime30( ) throws ParseException{
		  Date now=new Date();
		  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		  Date date =new Date(now.getTime()-1800000) ; //直接取得当前时间的前30分钟 时间点	 
		return formatter.format(date);
	}
	
	/**
	 * 日期比较 单位 日
	 * 相等则返回0,date1大返回1,否则返回-1;
	 * @throws ParseException 
	*/
	public static int dayTimeSize(String filetime,String newtime) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = sdf.parse(filetime);
		Date date2 = sdf.parse(newtime);	
		int res=date1.compareTo(date2);
		return res;
	}
	
	
	/**
	 * 时间 比较 单位:秒
	 * 相等则返回0,date1大返回1,否则返回-1;
	 * @throws ParseException 
	*/
	public static int  TimeSize(String filetime,String newtime) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");		
		Date date1 = sdf.parse(filetime);
		Date date2 = sdf.parse(newtime);
		int res=date1.compareTo(date2);
		return res;
	}
	
	/** 
	   * 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss a'(12小时制)<br> 
	   * 如Sat May 11 17:23:22 CST 2002 to '2002-05-11 05:23:22 下午'<br> 
	   * @param time Date 日期<br> 
	   * @param x int 任意整数如:1<br> 
	   * @return String 字符串<br> 
	   */ 
	public static String dateToString(Date time,int x){ 
	    SimpleDateFormat formatter; 
	    formatter = new SimpleDateFormat ("yyyy-MM-dd KK:mm:ss a"); 
	    String ctime = formatter.format(time); 

	    return ctime; 

}
	/**
     * 通过时间秒毫秒数判断两个时间的间隔
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1,Date date2)
    {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
        return days;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值