TimeUtil.java 2.08 KB
Newer Older
licc's avatar
licc committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
package cn.wisenergy.common.utils;

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

/**
 * 时间格式转换工具类
 */
public class TimeUtil {
	private static final String timeFormat = "yyyy-MM-dd HH:mm";
	private static final String dateFormat = "yyyy-MM-dd";

	/**
	 * 时间日期格式
	 * 
	 * @param time
	 * @return
	 * @throws ParseException
	 */
	public static Date getTime(String time) throws ParseException {
		SimpleDateFormat ft = new SimpleDateFormat(timeFormat);
		return ft.parse(time);
	}

	/**
	 * 时间日期格式
	 * 
	 * @param time
	 * @return
	 */
	public static String getTime(Date time) {
		SimpleDateFormat ft = new SimpleDateFormat(timeFormat);
		return ft.format(time);
	}

	/**
	 * 日期格式
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date getDate(String date) throws ParseException {
		SimpleDateFormat ft = new SimpleDateFormat(dateFormat);
		return ft.parse(date);
	}

	/**
	 * 日期格式
	 * 
	 * @param date
	 * @return
	 */
	public static String getDate(Date date) {
		SimpleDateFormat ft = new SimpleDateFormat(dateFormat);
		return ft.format(date);
	}

	/**
	 * 将日期转化成毫秒
	 * 
	 * @param date
	 * @return
	 */
	public static Long getTimeMill(Date date) {
		return date.getTime();
	}

	/**
	 * 将毫秒转化为日期
	 * 
	 * @param time
	 * @return
	 */
	public static Date getTimeMill(Long time) {
		Date date = new Date();
		date.setTime(time);
		return date;
	}

	/**
	 * 两个时间相差的分钟数
	 * 
	 * @param time1
	 * @param time2
	 * @return
	 * @Description:
	 */
	public static Integer getDistanceMinutes(Date time1, Date time2) {
		try {
			SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
			String fromDate = simpleFormat.format(time1);
			String toDate = simpleFormat.format(time2);
			long from = simpleFormat.parse(fromDate).getTime();
			long to = simpleFormat.parse(toDate).getTime();
			int minutes = (int) ((to - from) / (1000 * 60));
			return minutes;
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}

	}
}