1、日期的工具辅助类
import android.text.TextUtils;
import android.text.format.Time;
import com.ankoninc.ESDiagnoseUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
private static final String LOG_TAG = "DateUtil";
public static final String DEFAULT_DATE_TIME_FORMAT = "%Y-%m-%d %H:%M:%S";
// 1s = 1000ms
public final static int SECOND_IN_MILLIS = 1000;
public final static int INVALID_TIME = -1;
/**
* 显示聊天记录时间的最小间隔(ms)
*/
private final static int SHORTEST_INTERVAL = 5 * 60 * 1000;
/**
* 判断是否为当天
* @param date
* @return
*/
public static boolean isToday(Date date) {
if (null == date) {
return false;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
Calendar today = Calendar.getInstance();
today.setTime(date);
int tYear = today.get(Calendar.YEAR);
int tMonth = today.get(Calendar.MONTH);
int tDay = today.get(Calendar.DAY_OF_MONTH);
if (year == tYear && month == tMonth && day == tDay) {
return true;
} else {
return false;
}
}
/**
* 获取当天的时间
* @return
*/
public static String today() {
Date today = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(today);
}
/**
* 获取当前时间,格式“yyyy-MM-dd HH:mm:ss”
* @return
*/
public static String getCurrentDateTimeStr() {
Time time = new Time();
time.set(System.currentTimeMillis());
return time.format(DateUtil.DEFAULT_DATE_TIME_FORMAT);
}
/**
* 判断两条聊天记录时间间隔是否在分钟之内
*
* @param current
* @param last
* @return
*/
public static boolean isCloseEnough(long current, long last) {
return current - last < SHORTEST_INTERVAL ? true : false;
}
/**
* 根据当前时间,计算出几天前的时间戳返回
* @param days 天数
* @return 时间戳
*/
public static long daysAgo(int days) {
Calendar today = Calendar.getInstance();
today.add(Calendar.DATE, -days);
return today.getTimeInMillis();
}
/**
* 将毫秒转为Time格式的string,格式:yyyy-MM-dd hh:MM:ss
* @param milliSeconds
* @return
*/
public static String convertMillisToTimeStrWithDefaultFormat(long milliSeconds) {
return convertMillisToTimeStrWithFormat(milliSeconds, DEFAULT_DATE_TIME_FORMAT);
}
/**
* 使用指定格式,将毫秒转为Time格式的string
* @param milliSeconds 时间,毫秒
* @param format 格式类似“%Y-%m-%d”
* @return
*/
public static String convertMillisToTimeStrWithFormat(long milliSeconds, String format) {
try {
Time time = convertMillisToTime(milliSeconds);
return time != null ? time.format(format) : null;
} catch (Exception e) {
Log.e(LOG_TAG, e);
}
return null;
}
/**
* 将长整型的毫秒转化为Time返回
* @param milliSeconds
* @return
*/
public static Time convertMillisToTime(long milliSeconds) {
if (milliSeconds <= 0) {
return null;
}
try {
Time time = new Time();
time.set(milliSeconds);
return time;
} catch (Exception e) {
Log.e(LOG_TAG, e);
}
return null;
}
/**
* 将时间日期格式处理,只截取年月日(yyyy-MM-dd)返回
* @param timeStr 格式:yyyy-MM-dd HH:mm:ss
* @return
*/
public static String getDateFromDateTimeStr(String timeStr) {
if (TextUtils.isEmpty(timeStr)) {
return timeStr;
}
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(timeStr);
SimpleDateFormat destFormat = new SimpleDateFormat("yyyy-MM-dd");
return destFormat.format(date);
} catch (Exception e) {
Log.e(LOG_TAG, "getDateFromDateTimeStr error: %s", e.getMessage());
}
return timeStr;
}
/**
* 将时间字符串转化为长整数,便于比较
* @param timeStr String类型的字符串
* @return
*/
public static long convertTimeStrToMillis(String timeStr) {
if (TextUtils.isEmpty(timeStr)) {
return -1;
}
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(timeStr);
return date.getTime();
} catch (Exception e) {
Log.e(LOG_TAG, "parse timeStr: %s error: %s", timeStr, e.getMessage());
}
return -1;
}
/**
* 将长整数表示的时间按照某种format转化为时分秒展示
* @param passedMillis 耗费的时间,单位毫秒
* @return
*/
public static String convertPassedMillisToDisplayTimeFormat(long passedMillis) {
long second = (passedMillis / 1000) % 60;
long minute = (passedMillis / (1000 * 60)) % 60;
long hour = (passedMillis / (1000 * 60 * 60)) % 24;
return String.format("%02d:%02d:%02d", hour, minute, second);
}
/**
* 把HH:mm:ss格式时间转为整形,单位:秒
* @param timeStr 界面展示的时分秒,格式: HH:mm:ss
* @return
*/
public static int convertHourMinuteToSeconds(String timeStr) {
if (TextUtils.isEmpty(timeStr)) {
return 0;
}
String[] ss = timeStr.split(":");
if (ss == null || ss.length != 3) {
return 0;
}
int hour = ESDiagnoseUtil.parseIntSafely(ss[0]);
int minute = ESDiagnoseUtil.parseIntSafely(ss[1]);
int second = ESDiagnoseUtil.parseIntSafely(ss[2]);
return hour * 60 * 60 + minute * 60 + second;
}
/**
* 根据出生日期计算年龄,出生日期格式:yyyy-MM-dd HH:mm:ss
* @param birth
* @return
*/
public static int computeAge(String birth) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date birthDate = format.parse(birth);
int years = 0, months = 0, days = 0;
Calendar birthDay = Calendar.getInstance();
birthDay.setTimeInMillis(birthDate.getTime());
Calendar now = Calendar.getInstance();
now.setTimeInMillis(System.currentTimeMillis());
years = now.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR);
int currMonth = now.get(Calendar.MONTH) + 1;
int birthMonth = birthDay.get(Calendar.MONTH) + 1;
months = currMonth - birthMonth;
//if month difference is in negative then reduce years by one and calculate the number of months.
if (months < 0) {
years--;
months = 12 - birthMonth + currMonth;
if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) {
months--;
}
} else if (months == 0 && now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) {
years--;
months = 11;
}
//Calculate the days
if (now.get(Calendar.DATE) > birthDay.get(Calendar.DATE))
days = now.get(Calendar.DATE) - birthDay.get(Calendar.DATE);
else if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) {
int today = now.get(Calendar.DAY_OF_MONTH);
now.add(Calendar.MONTH, -1);
days = now.getActualMaximum(Calendar.DAY_OF_MONTH) - birthDay.get(Calendar.DAY_OF_MONTH) + today;
} else {
days = 0;
if (months == 12)
{
years++;
months = 0;
}
}
Log.d("computeAge", "years: %s, months: %s, days: %s", years, months, days);
return years;
} catch (ParseException e) {
Log.e(LOG_TAG, "computeAge error: %s", e.getMessage());
}
return 0;
}
/**
* 比较时间的大小
* @param timeStr1 格式:yyyy-MM-dd HH:mm:ss
* @param timeStr2 格式:yyyy-MM-dd HH:mm:ss
* @return 0 时间相同;1 后面的大;-1 前面的大;
*/
public static int compareTimeStr(String timeStr1, String timeStr2) {
Log.d(LOG_TAG, "compareTimeStr: %s, %s", timeStr1, timeStr2);
if (TextUtils.isEmpty(timeStr2)) {
return -1;
}
long timeMillis1 = convertTimeStrToMillis(timeStr1);
long timeMillis2 = convertTimeStrToMillis(timeStr2);
return timeMillis2 > timeMillis1 ? 1 : (timeMillis2 < timeMillis1 ? -1 : 0);
}
}