TimeUtils:時間工具類
阿新 • • 發佈:2019-01-31
一個處理時間戳的工具類(●’◡’●)
public class TimeUtils {
private final static long minute = 60 * 1000;// 1分鐘
private final static long hour = 60 * minute;// 1小時
private final static long day = 24 * hour;// 1天
private final static long month = 31 * day;// 月
private final static long year = 12 * month;// 年
/**
* @param @return 設定檔案
* @return Long 返回型別
* @throws
* @Title: getCurrentSystemTime
* @Description: 獲取當前系統時間
*/
public static Long getCurrentSystemTime() {
return Calendar.getInstance().getTimeInMillis();
}
/**
* 取出當前的日期格式yyyy-MM-dd HH:mm:ss
*
* @return
*/
public static String getCurrentTimeStr() {
Calendar curCalendar = Calendar.getInstance();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(curCalendar.getTime());
}
/**
* 將時間戳轉成格式化時間
*
* @param milliseconds
*/
public static String getFormatTime(long milliseconds) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return sdf.format(new Date(milliseconds));
}
/**
* 將時間戳轉成格式化時間
*
* @param milliseconds
*/
public static String getFormatTimeYY(long milliseconds) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date(milliseconds));
}
/**
* 返回文字描述的日期
*
* @param date
* @return
*/
public static String getTimeFormatText(long date) {
if (date == 0) {
return "";
}
date = date * 1000;
long diff = new Date().getTime() - date;
long r = 0;
if (diff > year) {
r = (diff / year);
return r + "年前";
}
if (diff > month) {
r = (diff / month);
return r + "月前";
}
if (diff > day) {
r = (diff / day);
return r + "天前";
}
if (diff > hour) {
r = (diff / hour);
return r + "小時前";
}
if (diff > minute) {
r = (diff / minute);
return r + "分鐘前";
}
return "剛剛";
}
/**
* 將時間戳轉成格式化時間
*
* @param milliseconds
*/
public static String getFormatTimeHHMM(long milliseconds) {
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
return sdf.format(new Date(milliseconds));
}
/**
* 將時間戳轉成格式化時間
*
* @param milliseconds
*/
public static String getFormatTime24(long milliseconds) {
SimpleDateFormat sdf = new SimpleDateFormat("kk:mm");
return sdf.format(new Date(milliseconds));
}
/**
* 計算兩個日期之間相差的天數
*
* @param smdate 較小的時間
* @param bdate 較大的時間
* @return 相差天數
* @throws ParseException
*/
public static int daysBetween(Date smdate, Date bdate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 字串的日期格式的計算
*/
public static int daysBetween(String smdate, String bdate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 擷取Date(1418784200000-0000)的固定長度
*/
public static String spliteTime(String dateStr) {
String sequence = dateStr.replace("/Date(", "");
return sequence.substring(0, 13).trim();
}
/**
* 獲取分秒格式化字串
*
* @param duration
* @return
*/
public static String getFormatMiniteSecString(int duration) {
int minutes = duration % (60 * 60) / 60;//分鐘時長
int seconds = duration % 60;//秒時長
return String.format("%d'%d''", minutes, seconds);
}
/**
* 格式化時間字串
* <p/>
* 顯示規則大於1天,顯示天. 大於1小時,顯示1=小時. 大於1分鐘, 顯示分鐘
* 其中,大於7天以上的均顯示7天前
*
* @param time
* @return
*/
public static String getFormatTimeString(long time) {
Long currentTime = new Date().getTime() / 1000;//獲得當前時間
Long diffTime = currentTime - time;//當前時間減去建立時間,得到時間差
long diffDay = diffTime / (24 * 3600); //得到天數
long diffHour = diffTime % (24 * 3600) / 3600; //得到小時數
long diffMinute = diffTime % 3600 / 60; //得到分鐘數
String result = null;
if (diffDay >= 1) {
if (diffDay >= 7) {
result = "7天前";
} else {
result = diffDay + "天前";
}
} else if (diffHour >= 1) {
result = diffHour + "小時前";
} else if (diffMinute >= 1) {
result = diffMinute + "分鐘前";
} else {
result = "";
}
return result;
}
}