public static String getDateToString(long milSecond) {
//現在時間
String now = new SimpleDateFormat("yyyy-MM-dd ").format(milSecond);
//昨天
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
String yday = new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime());
//今天
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 0);
String today = new SimpleDateFormat("yyyy-MM-dd ").format(c.getTime());
//其他時間段
Date date = new Date(milSecond);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
if (now.equals(today)) {
return "今天";
} else if (now.equals(yday)) {
return "昨天";
} else {
return format.format(date);
}
}
//日期轉換工具類:
public class DateTimeUtility {
/**
* 根據時間毫秒數格式化時間
*
* @param time 時間
* @return 格式化時間
*/
public static String formatDate(long time) {
return formatDate(time, 0);
}
/**
* 根據時間毫秒數格式化時間
* 0: "yyyy-MM-dd hh:mm:ss"
* 1: "yyyy/MM/dd hh:mm:ss"
* 2: "MM-dd HH:mm"
*
* @param time 時間
* @param style 樣式
* @return 格式化時間
*/
public static String formatDateTime(long time, int style) {
Date date = new Date(time);
String strTime;
SimpleDateFormat df;
switch (style) {
case 0:
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
break;
case 1:
df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
break;
case 2:
df = new SimpleDateFormat("MM-dd HH:mm");
break;
case 3:
df = new SimpleDateFormat("MM-dd HH:mm:ss");
break;
case 4:
df = new SimpleDateFormat("M-d");
break;
case 5:
df = new SimpleDateFormat("M-d HH:mm:ss");
break;
case 6:
df = new SimpleDateFormat("mm:ss");
break;
case 7:
df = new SimpleDateFormat("yyyy/M/d");
break;
case 8:
df = new SimpleDateFormat("yyyy/MM/dd HH:mm");
break;
case 9:
df = new SimpleDateFormat("M/d HH:mm");
break;
case 10:
df = new SimpleDateFormat("yyyy年MM月");
break;
case 11:
df = new SimpleDateFormat("HH:mm:ss");
break;
case 12:
df = new SimpleDateFormat("M月d日");
break;
case 13:
df = new SimpleDateFormat("M-d");
break;
case 14:
df = new SimpleDateFormat("HH:mm");
break;
default:
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
break;
}
strTime = df.format(date);
return strTime;
}
/**
* 根據時間毫秒數格式化時間
* 0: "yyyy-MM-dd"
* 1: "yyyy/MM/dd"
* 2: "MM/dd yyyy"
*
* @param time 時間
* @param style 樣式
* @return 格式化時間
*/
public static String formatDate(long time, int style) {
String strTime;
SimpleDateFormat df;
switch (style) {
case 0:
df = new SimpleDateFormat("yyyy-MM-dd");
break;
case 1:
df = new SimpleDateFormat("yyyy/MM/dd");
break;
case 2:
df = new SimpleDateFormat("MM/dd yyyy");
break;
case 3:
df = new SimpleDateFormat("MM/dd HH:mm");
break;
case 4:
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
break;
default:
df = new SimpleDateFormat("yyyy-MM-dd");
break;
}
strTime = df.format(new Date(time));
return strTime;
}
/**
* 計算兩個日期相隔的天數.
*
* @param d1
* @param d2
* @return 返回兩個日期相隔的天數, 如果是同一天返回0.
*/
public static int getDaysBetween(java.util.Calendar d1, java.util.Calendar d2) {
if (d1.after(d2)) {
java.util.Calendar swap = d1;
d1 = d2;
d2 = swap;
}
int days = d2.get(java.util.Calendar.DAY_OF_YEAR) - d1.get(java.util.Calendar.DAY_OF_YEAR);
int y2 = d2.get(java.util.Calendar.YEAR);
if (d1.get(java.util.Calendar.YEAR) != y2) {
d1 = (java.util.Calendar) d1.clone();
do {
days += d1.getActualMaximum(java.util.Calendar.DAY_OF_YEAR);
d1.add(java.util.Calendar.YEAR, 1);
}
while (d1.get(java.util.Calendar.YEAR) != y2);
}
return days;
}
public static String formatMsTime(long lt) {
Date msgTime = new Date(lt);
return msgTime.toString().substring(11, 19);
}
public static String formatMsShortTime(long lt) {
Date msgTime = new Date(lt);
return msgTime.toString().substring(11, 16);
}
public static String formatMsDate(long lt) {
Date msgTime = new Date(lt);
int year = msgTime.getYear() + 1900;
int month = msgTime.getMonth();
int day = msgTime.getDate();
return year + "-" + (month + 1) + "-" + (day);
}
public static String formatMsDate(long lt, String separator) {
Date msgTime = new Date(lt);
int year = msgTime.getYear() + 1900;
int month = msgTime.getMonth();
int day = msgTime.getDate();
return year + separator + (month + 1) + separator + (day);
}
/**
* 根據文件路徑
* 返回文件修改時間
*
* @param path 文件路徑
* @return 文件修改時間
*/
public static long getFileDate(String path) {
File file = new File(path);
return file.lastModified();
}
/**
* 將2013:10:08 11:48:07如此格式的時間
* 轉化爲毫秒數
*
* @param datetime 字符串時間
* @return 毫秒數
*/
public static long dateTimeToMS(String datetime) {
if (TextUtils.isEmpty(datetime)) return 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
final Date parse;
try {
parse = sdf.parse(datetime);
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
return parse.getTime();
}
/**
* 將2013:10:08 11:48:07如此格式的時間
* 轉化爲毫秒數
*
* @param datetime 字符串時間
* @return 毫秒數
*/
public static long dateTimeToMS(String datetime, int style) {
if (TextUtils.isEmpty(datetime)) return 0;
SimpleDateFormat sdf = null;
switch (style) {
case 0:
sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
break;
case 1:
sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
break;
case 2:
sdf = new SimpleDateFormat("yyyy年MM月");
break;
}
final Date parse;
try {
parse = sdf.parse(datetime);
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
return parse.getTime();
}
/**
* 將2013:10:08 11:48:07如此格式的時間
* 轉化爲毫秒數
*
* @param datetime 字符串時間
* @return 毫秒數
*/
public static long dateTimeToMSs(String datetime) {
if (TextUtils.isEmpty(datetime)) return 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
final Date parse;
try {
parse = sdf.parse(datetime);
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
return parse.getTime();
}
/**
* 將20131008114807如此格式的時間
* 轉化爲毫秒數
*
* @param datetime 字符串時間
* @return 毫秒數
*/
public static long dateTime2MS(String datetime) {
if (TextUtils.isEmpty(datetime) || datetime.length() != 14) {
return 0;
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(datetime.substring(0, 4));
stringBuilder.append(":");
stringBuilder.append(datetime.substring(4, 6));
stringBuilder.append(":");
stringBuilder.append(datetime.substring(6, 8));
stringBuilder.append(" ");
stringBuilder.append(datetime.substring(8, 10));
stringBuilder.append(":");
stringBuilder.append(datetime.substring(10, 12));
stringBuilder.append(":");
stringBuilder.append(datetime.substring(12, 14));
return dateTimeToMS(stringBuilder.toString(), 0);
}
public static Calendar getNextTime() {
Calendar cal = Calendar.getInstance();
cal.setTime(new java.util.Date());
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.DAY_OF_MONTH, 1);
return cal;
}
/**
* 將毫秒轉化爲時分秒,用於播放錄音顯示時長
*
* @param timeLength
* @return
*/
public static String ssToTime(long timeLength) {
SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
// SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
String hms = formatter.format(timeLength);
return hms;
}
/**
* 方法名:getNowTime()
* 功 能:獲取當前年月日
* 參 數:無
* 返回值:String
*/
public static String getNowTime() {
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); //當前年月日 時分秒
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
return simpleDateFormat.format(date);
}
/**
* 方法名:getNowLongTime()
* 功 能:獲取當前系統時間戳
* 參 數:無
* 返回值:long
*/
public static long getNowLongTime() {
return System.currentTimeMillis();
}
/**
* 方法名:getNowLongDateTime()
* 功 能:獲取當前時間戳(只精確到天)
* 參 數:無
* 返回值:long
*/
public static long getNowLongDateTime() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
String format = simpleDateFormat.format(date);
Date parse = null;
try {
parse = simpleDateFormat.parse(format);
} catch (ParseException e) {
e.printStackTrace();
}
return parse.getTime();
}
/**
* 方法名:getCurrentMinute()
* 功 能:獲取當前時間的總分鐘數
* 參 數:無
* 返回值:int
*/
public static int getCurrentMinute() {
Calendar cal = Calendar.getInstance();// 當前日期
int hour = cal.get(Calendar.HOUR_OF_DAY);// 獲取小時
int minute = cal.get(Calendar.MINUTE);// 獲取分鐘
int minuteOfDay = hour * 60 + minute;// 從0:00分開是到目前爲止的分鐘數
return minuteOfDay;
}
/**
* 方法名:getDate(long time)
* 功 能:獲取到指定時間的long值(精確到天)
* 參 數:long time
* 返回值:long
*/
public static long getDate(long time) {
Date date = new Date(time);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String format = simpleDateFormat.format(date);
Date parse = null;
try {
parse = simpleDateFormat.parse(format);
} catch (ParseException e) {
e.printStackTrace();
}
return parse.getTime();
}
/**
* 方法名:getIncreaseTime(int days)
* 功 能:獲取增加指定天數後系統的時間long
* 參 數:int days
* 返回值:long
*/
public static long getIncreaseTime(int days) {
Calendar c = Calendar.getInstance();//日期
c.add(Calendar.DAY_OF_MONTH, days);//日期增加到指定天數後
long time = c.getTime().getTime();
long date = DateTimeUtility.getDate(time);
return date;
}
/**
* 方法名: timeInterval(long startTime, long endTime)
* 功 能:獲取兩個時間點,間隔多少分鐘
* 參 數:daysBetween(Date startDate, Date endDate)
* 返回值:int
*/
public static int timeInterval(long startTime, long endTime) {
Date startDate = new Date(startTime);
Date endDate = new Date(endTime);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
startDate = sdf.parse(sdf.format(startDate));
endDate = sdf.parse(sdf.format(endDate));
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
long time1 = calendar.getTimeInMillis();
calendar.setTime(endDate);
long time2 = calendar.getTimeInMillis();
long betweenDays = (time2 - time1) / 1000 / 60;
return Integer.parseInt(String.valueOf(betweenDays));
}
/**
* 方法名:judgingTime(int startMinutes, int endMinutes)
* 功 能:判斷當前總分鐘對應的時間是否處於兩個總分鐘數對應的時間點之間
* 參 數:int startMinutes, int endMinutes
* 返回值:boolean
*/
public static boolean judgingTime(int startMinutes, int endMinutes) {
int currentMinute = DateTimeUtility.getCurrentMinute();//獲取當前時間的總分鐘數
if (currentMinute >= startMinutes && currentMinute <= endMinutes) {
return true;
} else {
return false;
}
}
/**
* 方法名:getTodayAppointTime(String time)
* 功 能:獲取今日指定時間的時間戳
* 參 數:String time
* 返回值:long
*/
public static long getTodayAppointTime(String time) {
String dateTime = getNowTime() + " " + time;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition position = new ParsePosition(0);
Date date = simpleDateFormat.parse(dateTime, position);
return date.getTime();
}
public static String getCrmTimeData(String date) {
if (TextUtils.isEmpty(date)) {
return "";
}
long time = Long.valueOf(date);
String today = DateTimeUtility.formatDateTime(System.currentTimeMillis(), 9);
String timeStr = DateTimeUtility.formatDateTime(time, 9);
if (today.split(" ")[0].equals(timeStr.split(" ")[0])) {
return "今天 " + timeStr.split(" ")[1];
} else {
return timeStr.replace("/", "月").replaceFirst(" ", "日 ");
}
}
public static String getCrmData(String date) {
if (TextUtils.isEmpty(date)) {
return "";
}
long time = Long.valueOf(date);
String timeStr = DateTimeUtility.formatDateTime(time, 13);
return timeStr;
}
public static String getCrmTimeFlag(String date) {
long time = Long.valueOf(date);
String timeStr = DateTimeUtility.formatDateTime(time, 12);
//時間戳轉成提示性日期格式(昨天、今天……)
String dateToString = getDateToString(Long.valueOf(date));
if (dateToString.equals("今天")) {
return dateToString;
} else if (dateToString.equals("昨天")) {
return dateToString;
} else {
return timeStr;
}
}
/**
* 方法名:getDateToString(long milSecond)
* 功 能:時間戳轉成提示性日期格式(昨天、今天……)
* 參 數:long milSecond
* 返回值:String
*/
public static String getDateToString(long milSecond) {
//現在時間
String now = new SimpleDateFormat("yyyy-MM-dd ").format(milSecond);
//昨天
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
String yday = new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime());
//今天
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 0);
String today = new SimpleDateFormat("yyyy-MM-dd ").format(c.getTime());
//其他時間段
Date date = new Date(milSecond);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
if (now.equals(today)) {
return "今天";
} else if (now.equals(yday)) {
return "昨天";
} else {
return format.format(date);
}
}
public static String getCrmDataTime(String date) {
if (TextUtils.isEmpty(date)) {
return "";
}
long time = Long.valueOf(date);
String today = DateTimeUtility.formatDateTime(System.currentTimeMillis(), 9);
String timeStr = DateTimeUtility.formatDateTime(time, 9);
if (today.split(" ")[0].equals(timeStr.split(" ")[0])) {
return "今天";
} else {
return timeStr.replace("/", "月").replaceFirst(" ", "日 ");
}
}
public static String timeCovert(String timeLengthStr) {
if (TextUtils.isEmpty(timeLengthStr)) {
return "";
}
try {
int timeLength = Integer.valueOf(timeLengthStr);
if (timeLength >= 60) {
return (int) (timeLength / 60) + "分" + (timeLength % 60) + "秒";
} else if (timeLength > 0 && timeLength < 60) {
return timeLength + "秒";
}
} catch (Exception e) {
e.printStackTrace();
return "";
}
return "";
}
}