1. 程式人生 > >安卓時間工具類,計算時間差,比較當前時間

安卓時間工具類,計算時間差,比較當前時間

自己的工具類貼上來。。。有什麼不足的地方請大家指出。

<span style="font-size:18px;">package com.example.property.util;

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

import android.text.format.Time;

/**
 * 
 * @author cj 2016-5-26
 * 
 */
public class TimeUtil {

	private SimpleDateFormat sf = null;

	/**
	 * 獲取當前時間
	 * 
	 * @return
	 */
	public String getNowTime() {
		String timeString = null;
		Time time = new Time();
		time.setToNow();
		String year = thanTen(time.year);
		String month = thanTen(time.month + 1);
		String monthDay = thanTen(time.monthDay);
		String hour = thanTen(time.hour);
		String minute = thanTen(time.minute);

		timeString = year + "-" + month + "-" + monthDay + " " + hour + ":"
				+ minute;
		// System.out.println("-------timeString----------" + timeString);
		return timeString;
	}

	public int calculate(int year, int month) {

		boolean yearleap = judge(year);
		int day;
		if (yearleap && month == 2) {
			day = 29;
		} else if (!yearleap && month == 2) {
			day = 28;
		} else if (month == 4 || month == 6 || month == 9 || month == 11) {
			day = 30;
		} else {
			day = 31;
		}
		return day;
	}

	public boolean judge(int year) {
		boolean yearleap = (year % 400 == 0) || (year % 4 == 0)
				&& (year % 100 != 0);// 採用布林資料計算判斷是否能整除
		return yearleap;
	}

	/**
	 * 十一下加零
	 * 
	 * @param str
	 * @return
	 */
	public String thanTen(int str) {

		String string = null;

		if (str < 10) {
			string = "0" + str;
		} else {

			string = "" + str;

		}
		return string;
	}

	/**
	 * 計算時間差
	 * 
	 * @param starTime
	 *            開始時間
	 * @param endTime
	 *            結束時間
	 * @param type
	 *            返回型別 ==1----天,時,分。 ==2----時
	 * @return 返回時間差
	 */
	public String getTimeDifference(String starTime, String endTime) {
		String timeString = "";
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		try {
			Date parse = dateFormat.parse(starTime);
			Date parse1 = dateFormat.parse(endTime);

			long diff = parse1.getTime() - parse.getTime();

			long day = diff / (24 * 60 * 60 * 1000);
			long hour = (diff / (60 * 60 * 1000) - day * 24);
			long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
			long s = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
			long ms = (diff - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
					- min * 60 * 1000 - s * 1000);
			// System.out.println(day + "天" + hour + "小時" + min + "分" + s +
			// "秒");
			long hour1 = diff / (60 * 60 * 1000);
			String hourString = hour1 + "";
			long min1 = ((diff / (60 * 1000)) - hour1 * 60);
			timeString = hour1 + "小時" + min1 + "分";
			// System.out.println(day + "天" + hour + "小時" + min + "分" + s +
			// "秒");

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return timeString;

	}

	/**
	 * 計算相差的小時
	 * 
	 * @param starTime
	 * @param endTime
	 * @return
	 */
	public String getTimeDifferenceHour(String starTime, String endTime) {
		String timeString = "";
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		try {
			Date parse = dateFormat.parse(starTime);
			Date parse1 = dateFormat.parse(endTime);

			long diff = parse1.getTime() - parse.getTime();
			String string = Long.toString(diff);

			float parseFloat = Float.parseFloat(string);

			float hour1 = parseFloat / (60 * 60 * 1000);

			timeString = Float.toString(hour1);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return timeString;

	}

	/**
	 * 獲取時間中的某一個時間點
	 * 
	 * @param str
	 * @param type
	 * @return
	 */
	public String getJsonParseShiJian(String str, int type) {
		String shijanString = null;
		String nian = str.substring(0, str.indexOf("-"));
		String yue = str.substring(str.indexOf("-") + 1, str.lastIndexOf("-"));
		String tian = str.substring(str.lastIndexOf("-") + 1, str.indexOf(" "));
		String shi = str.substring(str.indexOf(" ") + 1, str.lastIndexOf(":"));
		String fen = str.substring(str.lastIndexOf(":") + 1, str.length());

		switch (type) {
		case 1:
			shijanString = nian;
			break;
		case 2:
			shijanString = yue;
			break;
		case 3:
			shijanString = tian;
			break;
		case 4:
			shijanString = shi;
			break;
		case 5:
			shijanString = fen;
			break;

		}
		return shijanString;
	}

	/**
	 * Sring變int
	 * 
	 * @param str
	 * @return
	 */
	public int strToInt(String str) {
		int value = 0;
		value = Integer.parseInt(str);
		return value;
	}

	/**
	 * 與當前時間比較早晚
	 * 
	 * @param time
	 *            需要比較的時間
	 * @return 輸入的時間比現在時間晚則返回true
	 */
	public boolean compareNowTime(String time) {
		boolean isDayu = false;

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		try {
			Date parse = dateFormat.parse(time);
			Date parse1 = dateFormat.parse(getNowTime());

			long diff = parse1.getTime() - parse.getTime();
			if (diff <= 0) {
				isDayu = true;
			} else {
				isDayu = false;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return isDayu;
	}

	/**
	 * 把時間戳變yyyy-MM-dd HH:mm格式時間
	 * 
	 * @param time
	 * @return
	 */
	public String getDateToString(long time) {
		Date d = new Date(time);
		sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sf.format(d);
	}

	/**
	 * 返回時間戳
	 * 
	 * @param time
	 * @return
	 */
	public long dataOne(String time) {
		SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm",
				Locale.CHINA);
		Date date;
		long l = 0;
		try {
			date = sdr.parse(time);
			l = date.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return l;
	}

	/**
	 * 比較兩個時間
	 * 
	 * @param starTime
	 *            開始時間
	 * @param endString
	 *            結束時間
	 * @return 結束時間大於開始時間返回true,否則反之֮
	 */
	public boolean compareTwoTime(String starTime, String endString) {
		boolean isDayu = false;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

		try {
			Date parse = dateFormat.parse(starTime);
			Date parse1 = dateFormat.parse(endString);

			long diff = parse1.getTime() - parse.getTime();
			if (diff >= 0) {
				isDayu = true;
			} else {
				isDayu = false;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return isDayu;

	}

	public boolean compareTwoTime2(String starTime, String endString) {
		boolean isDayu = false;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		try {
			Date parse = dateFormat.parse(starTime);
			Date parse1 = dateFormat.parse(endString);

			long diff = parse1.getTime() - parse.getTime();
			if (diff >= 0) {
				isDayu = true;
			} else {
				isDayu = false;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return isDayu;

	}

	/**
	 * 獲取年
	 * 
	 * @param time
	 * @return
	 */
	public String getTimeYear(String time) {

		String substring = time.substring(0, time.lastIndexOf(" "));
		return substring;

	}

	/**
	 * 換算小時,0.5小時-->0小時30分
	 * 
	 * @param hour
	 * @return
	 */
	private String convertTime(String hour) {

		String substring = hour.substring(0, hour.lastIndexOf("."));
		String substring2 = hour.substring(hour.lastIndexOf(".") + 1,
				hour.length());
		substring2 = "0." + substring2;
		float f2 = Float.parseFloat(substring2);
		f2 = f2 * 60;
		String string = Float.toString(f2);
		String min = string.substring(0, string.lastIndexOf("."));
		return substring + "小時" + min + "分";

	}
}
</span>