时间工具类

1.直接算差
public static long getDiff(String time1,String time2) throws ParseException {
		//比较接收请求的时间和系统记录时间差
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date1 = sd.parse(time1);
		Date date2 = sd.parse(time2);
			
		//得到2个日期的总毫秒数
		long s1 = date1.getTime();//时间的毫秒
		long s2 = date2.getTime();
		
		long diff = s2-s1;
		return diff;
	}	

2.方法2:直接得到时间的毫秒数
/**
	 * 将传入的yyyy-MM-dd HH:mm:ss字符类型的时间转换成毫秒数
	 * 
	 * @param time
	 * @return
	 * @throws ParseException
	 */
	public static long getMillisecond(String time) {
		logger.info("传入参数为:" + time);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long millisecond = 0l;
		try {
			Date date = sdf.parse(time);
			millisecond = date.getTime();
		} catch (ParseException e) {
			logger.info("时间格式转换异常");
			e.printStackTrace();
		}
		return millisecond;
	}

3.其他各种与时间相关:
/**
	 * 获取时间戳
	 * 
	 * @return yyyyMMddHHmmss
	 */
	public static String getTime() {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}
	
	/**
	 * 获取格式为yyyy-MM-dd的系统时间
	 * 
	 * @return yyyy-MM-dd
	 */
	public static String getTimes() {
		return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	}
	
	/**
	 * 获取格式为yyyy-MM-dd HH:mm:ss的系统时间
	 * 
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String getSystemTime() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}
	
	/**
	 * 获取格式为yyyy-MM-dd HH:mm系统时间
	 * 
	 * @return yyyy-MM-dd HH:mm
	 */
	public static String getSystemTimes() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
	}

	/**
	 * 获取yyyy年MM月dd日 HH:mm格式的系统时间
	 * 
	 * @return
	 */
	public static String getChineseFormatTime() {
		return new SimpleDateFormat("yyyy年MM月dd日 HH:mm").format(new Date());
	}

	/**
	 * 过去当前的年月
	 */
	public static String getCurrentYearAndMonth() {
		return new SimpleDateFormat("yyyyMM").format(new Date());
	}

	/**
	 * 把传入的日期类型的数据转换成字符串类型
	 * 
	 * @param date
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String dateToString(Date date) {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
	}

	/**
	 * 将传入的yyyy-MM-dd HH:mm:ss字符类型的时间转换成毫秒数
	 * 
	 * @param time
	 * @return
	 * @throws ParseException
	 */
	public static long getMillisecond(String time) {
		logger.info("传入参数为:" + time);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long millisecond = 0l;
		try {
			Date date = sdf.parse(time);
			millisecond = date.getTime();
		} catch (ParseException e) {
			logger.info("时间格式转换异常");
			e.printStackTrace();
		}
		return millisecond;
	}
	
	/**
	 * 将传入的yyyy-MM-dd字符类型的时间转换成毫秒数
	 * 
	 * @param time
	 * @return
	 * @throws ParseException
	 */
	public static long getDayMillisecond(String time) {
		logger.info("传入参数为:" + time);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		long millisecond = 0l;
		try {
			Date date = sdf.parse(time);
			millisecond = date.getTime();
		} catch (ParseException e) {
			logger.info("时间格式转换异常");
			e.printStackTrace();
		}
		return millisecond;
	}

	/**
	 * 获取当前日期是星期几
	 * 
	 * @return
	 */
	public static String getDay() {
		return new SimpleDateFormat("EEEE").format(new Date());
	}

	/**
	 * 获得格式为yyyy.MM.dd的当前日期
	 * 
	 * @return
	 */
	public static String getDate() {
		return new SimpleDateFormat("yyyy.MM.dd").format(new Date());
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值