“yyyy-MM-dd HH:mm:ss” 這是常用的時間顯示格式,表示了“年-月-日 時:分:秒”
注意:HH表示24小時制,hh表示12小時制
1、日期字符串轉(zhuǎn)換Date
//參數(shù)一:時間字符串; 參數(shù)二:日期格式
public static Date parseServerTime(String serverTime, String format) {
if (format == null || format.isEmpty()) {
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINESE);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
Date date = null;
try {
date = sdf.parse(serverTime);
} catch (Exception e) {
Timber.e(e, "");
}
return date;
}
2、秒數(shù)轉(zhuǎn)換成時分秒
public static String convertSecToTimeString(long lSeconds) {
long nHour = lSeconds / 3600;
long nMin = lSeconds % 3600;
long nSec = nMin % 60;
nMin = nMin / 60;
return String.format("%02d小時%02d分鐘%02d秒", nHour, nMin, nSec);
}
3、Date對象獲取時間字符串
public static String getDateStr(Date date,String format) {
if (format == null || format.isEmpty()) {
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.format(date);
}
4、時間戳轉(zhuǎn)換日期格式字符串
public static String timeStamp2Date(long time, String format) {
if (format == null || format.isEmpty()) {
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date(time));
}
5、日期格式字符串轉(zhuǎn)換時間戳
public static String date2TimeStamp(String date, String format) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return String.valueOf(sdf.parse(date).getTime() / 1000);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
6、獲取某個日期前后N天的日期
/**
* 獲取某個日期前后N天的日期
*
* @param beginDate
* @param distanceDay 前后幾天 如獲取前7天日期則傳-7即可;如果后7天則傳7
* @param format 日期格式,默認(rèn)"yyyy-MM-dd"
* @return
*/
public static String getOldDateByDay(Date beginDate, int distanceDay, String format) {
if (format == null || format.isEmpty()) {
format = "yyyy-MM-dd";
}
SimpleDateFormat dft = new SimpleDateFormat(format);
Calendar date = Calendar.getInstance();
date.setTime(beginDate);
date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay);
Date endDate = null;
try {
endDate = dft.parse(dft.format(date.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
return dft.format(endDate);
}
7、獲取前后幾個月的日期
/**
* 獲取前后幾個月的日期
* @param beginDate
* @param distanceMonth
* @param format
* @return
*/
public static String getOldDateByMonth(Date beginDate, int distanceMonth, String format) {
if (format == null || format.isEmpty()) {
format = "yyyy-MM-dd";
}
SimpleDateFormat dft = new SimpleDateFormat(format);
Calendar date = Calendar.getInstance();
date.setTime(beginDate);
date.set(Calendar.MONTH, date.get(Calendar.MONTH) + distanceMonth);
Date endDate = null;
try {
endDate = dft.parse(dft.format(date.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
return dft.format(endDate);
}
8、判斷一個時間是否在某個時間范圍內(nèi)
public static boolean isCurrentInTimeScope(int deadlineHour, int deadlineMin) {
boolean result;
// 1000 * 60 * 60 * 24
final long aDayInMillis = 86400000;
final long currentTimeMillis = SntpClock.currentTimeMillis();
//截止時間
Time deadlineTime = new Time();
deadlineTime.set(currentTimeMillis);
deadlineTime.hour = deadlineHour;
deadlineTime.minute = deadlineMin;
//當(dāng)前時間
Time startTime = new Time();
startTime.set(currentTimeMillis);
//當(dāng)前時間推后20分鐘
Date d = new Date(currentTimeMillis);
long myTime = (d.getTime() / 1000) + 20 * 60;
d.setTime(myTime * 1000);
Time endTime = new Time();
endTime.set(myTime);
if (!startTime.before(endTime)) {
// 跨天的特殊情況(比如22:00-8:00)
startTime.set(startTime.toMillis(true) - aDayInMillis);
result = !deadlineTime.before(startTime) && !deadlineTime.after(endTime);
// startTime <= deadlineTime <=endTime
Time startTimeInThisDay = new Time();
startTimeInThisDay.set(startTime.toMillis(true) + aDayInMillis);
if (!deadlineTime.before(startTimeInThisDay)) {
result = true;
}
} else {
// 普通情況(比如 8:00 - 14:00)
result = !deadlineTime.before(startTime) && !deadlineTime.after(endTime);
// startTime <= deadlineTime <=endTime
}
return result;
}