public class TimeUtil {
/**
* 對比當前時間,轉換時間戳
*
* @param cc_time 需要轉換的時間戳
* @return 返回的時間格式:獲取時間年份與當前年份一致,返回 MM-dd HH:mm;否則返回yyyy-MM-dd HH:mm
*/
public static String timeCompareYMDHMinSFigure(long cc_time) {
Calendar now = Calendar.getInstance();
String year = now.get(Calendar.YEAR) + "";
String re_StrTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
re_StrTime = sdf.format(new Date(cc_time));
String years = re_StrTime.substring(0, 4);
if (!year.equals(years)) {
return re_StrTime;
} else {
return re_StrTime.substring(5, re_StrTime.length());
}
}
//根據時間戳獲取格式後的時間,可根據需要自行截取數據顯示
public static String getDateTime(java.util.Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // HH:mm:ss
return format.format(date);
}
//根據時間戳獲取當前時間
public static String getTime(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");// HH:mm:ss
java.util.Date date = new java.util.Date(System.currentTimeMillis());
return simpleDateFormat.format(date);
}
/**
* 時間格式轉換
*
* @param time 需要轉換的時間戳
* @return 返回時間格式 yyyy年MM月dd日
*/
public static String timeYMDChinese(long time) {
String re_StrTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
re_StrTime = sdf.format(new Date(time));
return re_StrTime;
}
/**
* 時間格式轉換
*
* @param time 需要轉換的時間戳
* @return 返回時間格式 yyyy-MM-dd HH:mm:ss
*/
public static String timeYMDHMinSFigure(long time) {
String re_StrTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
re_StrTime = sdf.format(new Date(time));
return re_StrTime;
}
/**
* 時間格式轉換
*
* @param time 需要轉換的時間戳
* @return 返回時間格式 yyyy-MM-dd
*/
public static String timeYMDFigure(long time) {
String re_StrTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
re_StrTime = sdf.format(new Date(time));
return re_StrTime;
}
/**
* 毫秒 轉化爲 天 時 分 秒 毫秒
*
* @param ms 毫秒數
* @return dd天HH時mm秒xx毫秒
*/
public static String formatTime(Long ms) {
Integer ss = 1000;
Integer mi = ss * 60;
Integer hh = mi * 60;
Integer dd = hh * 24;
Long day = ms / dd;
Long hour = (ms - day * dd) / hh;
Long minute = (ms - day * dd - hour * hh) / mi;
Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
StringBuffer sb = new StringBuffer();
if (day > 0) {
sb.append(day + "天");
}
if (hour > 0) {
sb.append(hour + "小時");
}
if (minute > 0) {
sb.append(minute + "分鐘");
}
if (second > 0) {
sb.append(second + "秒");
}
if (milliSecond > 0) {
sb.append(milliSecond + "毫秒");
}
return sb.toString();
}
/**
* 時間比較
*
* @param startTime 起始時間
* @param endTime 終止時間
* @param format 轉換的時間格式,如:yyyy-MM-dd
* @return 返回兩個時間相差多少天
*/
public static long dateDiff(String startTime, String endTime, String format) {
// 按照傳入的格式生成一個simpledateformate對象
SimpleDateFormat sd = new SimpleDateFormat(format);
long nd = 1000 * 24 * 60 * 60;// 一天的毫秒數
long nh = 1000 * 60 * 60;// 一小時的毫秒數
long nm = 1000 * 60;// 一分鐘的毫秒數
long ns = 1000;// 一秒鐘的毫秒數
long diff;
long day = 0;
try {
// 獲得兩個時間的毫秒時間差異
diff = sd.parse(endTime).getTime()
- sd.parse(startTime).getTime();
day = diff / nd;// 計算差多少天
long hour = diff % nd / nh;// 計算差多少小時
long min = diff % nd % nh / nm;// 計算差多少分鐘
long sec = diff % nd % nh % nm / ns;// 計算差多少秒
// 輸出結果
// Log.d("TimeUTIL", "時間相差:" + day + "天" + hour + "小時" + min + "分鐘" + sec + "秒。");
if (day >= 1) {
return day;
} else {
if (day == 0) {
return 1;
} else {
return 0;
}
}
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
}
Android工具類——時間相關工具類集合
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.