package com.ejoy.android.utils;
import android.annotation.SuppressLint;
import android.content.Context;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
@SuppressLint("SimpleDateFormat")
public class DateOperation {
/**
*
* @param begin
* 开始时间
* @param end
* 当前时间
* @param dateFormate
* 时间格式
* @return 两个时间点之间的差
*/
public static long compareTime(String end, SimpleDateFormat dateFormate) {
long min = 0;
try {
String dfdate1 = dateFormate.format(new Date());
Date date1 = dateFormate.parse(dfdate1);
Date date2 = (Date) dateFormate.parseObject(end.replace('/', '-'));
long diff = date2.getTime() - date1.getTime();
min = diff / (1000 * 60); // 分钟
if (min < 0) {
// 错误
}
} catch (ParseException e) {
e.printStackTrace();
}
return min;
}
/**
* @param begin
* 开始时间
* @param end
* 当前时间
* @param dateFormate
* 时间格式
* @return 两个时间点之间的差,单位分钟 返回正数,负数返回绝对值
*/
public static long compareTime2(String end, SimpleDateFormat dateFormate) {
return Math.abs(compareTime(end, dateFormate));
}
public static String GetDateFormatTodays(long min) {
try {
if (min > 60) {
if (min > 60 * 24) {
long a1 = 0;
long a2 = 0;
long a6 = 0;
long a3 = 0;
a1 = min / (60 * 24);
a6 = min % (60 * 24);
if (a6 > 60) {
a2 = a6 / 60;
a3 = a6 % 60;
} else {
a3 = a6;
}
return a1 + "天" + a2 + "小时" + a3 + "分钟";
} else {
long a4 = 0;
long a5 = 0;
a4 = min / 60;
a5 = min % 60;
return a4 + "小时" + a5 + "分钟";
}
} else {
return min + "分钟";
}
} catch (Exception er) {
er.printStackTrace();
return "";
}
}
public static String getTimeDiff(Date date) {
Calendar cal = Calendar.getInstance();
long diff = 0;
Date dnow = cal.getTime();
String str = "";
diff = dnow.getTime() - date.getTime();
if (diff > 24 * 60 * 60 * 1000) {
str = "1天前";
} else if (diff > 5 * 60 * 60 * 1000) {
str = "2小时前";
} else if (diff > 1 * 60 * 60 * 1000) {
str = "1小时前";
} else if (diff > 30 * 60 * 1000) {
str = "30分钟前";
} else if (diff > 15 * 60 * 1000) {
str = "15分钟前";
} else if (diff > 5 * 60 * 1000) {
str = "5分钟前";
} else if (diff > 1 * 60 * 1000) {
str = "1分钟前";
} else {
str = "刚刚";
}
return str;
}
public static String getTimeDiff(String date) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
long diff = 0;
Date dnow = cal.getTime();
String str = "";
Date date2 = df.parse(date);
diff = dnow.getTime() - date2.getTime();
if (diff > 7 * 24 * 60 * 60 * 1000) {
str = "7天前 " + date2.getHours() + ":" + date2.getMinutes();
} else if (diff > 6 * 24 * 60 * 60 * 1000) {
str = "6天前 " + date2.getHours() + ":" + date2.getMinutes();
} else if (diff > 5 * 24 * 60 * 60 * 1000) {
str = "5天前 " + date2.getHours() + ":" + date2.getMinutes();
} else if (diff > 4 * 24 * 60 * 60 * 1000) {
str = "4天前 " + date2.getHours() + ":" + date2.getMinutes();
} else if (diff > 3 * 24 * 60 * 60 * 1000) {
str = "3天前 " + date2.getHours() + ":" + date2.getMinutes();
} else if (diff > 2 * 24 * 60 * 60 * 1000) {
str = "前天 " + date2.getHours() + ":" + date2.getMinutes();
} else if (diff > 24 * 60 * 60 * 1000) {
str = "昨天 " + date2.getHours() + ":" + date2.getMinutes();
} else if (diff > 5 * 60 * 60 * 1000) {
str = "2小时前";
} else if (diff > 1 * 60 * 60 * 1000) {
str = "1小时前";
} else if (diff > 30 * 60 * 1000) {
str = "30分钟前";
} else if (diff > 15 * 60 * 1000) {
str = "15分钟前";
} else if (diff > 5 * 60 * 1000) {
str = "5分钟前";
} else if (diff > 1 * 60 * 1000) {
str = "1分钟前";
} else {
str = "刚刚";
}
return str;
}
public static String getTimeDiffDay(String date) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
Date dnow = cal.getTime();
String str = "";
Date date2 = df.parse(date);
int diffyear = dnow.getYear() - date2.getYear();
if (diffyear == 0) {
int diffmonth = dnow.getMonth() - date2.getMonth();
if (diffmonth == 0) {
int diffday = dnow.getDay() - date2.getDay();
if (diffday == 0) {
str = "今天" + date2.getHours() + ":" + date2.getMinutes();
} else if (diffday == 1) {
str = "昨天" + date2.getHours() + ":" + date2.getMinutes();
} else if (diffday == 2) {
str = "前天" + date2.getHours() + ":" + date2.getMinutes();
} else {
str = diffday + "天以前" + date2.getHours() + ":"
+ date2.getMinutes();
}
} else {
str = diffmonth + "月以前";
}
} else {
str = diffyear + "年以前";
}
return str;
}
public static String DateFormatProcess(String DateTimeString,
SimpleDateFormat dateFormate) {
SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date1 = dateFormate.parse(DateTimeString);
if (date1.getHours() == 0) {
return dateformat1.format((dateFormate.parse(DateTimeString)));
}
} catch (Exception e) {
e.printStackTrace();
}
return DateTimeString;
}
/***
* 获取时间的年月日
*
* @param DateTimeString
* @return
*/
public static String DateFormatProcess(String DateTimeString) {
SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd");
try {
return dateformat1.format((dateformat1.parse(DateTimeString)));
} catch (Exception e) {
e.printStackTrace();
}
return DateTimeString;
}
public static String GetSyscurrentTimeMillis() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
Date curDate = new Date(System.currentTimeMillis());
return formatter.format(curDate);
}
public static String GetSyscurrentTimeMillis(int iMin) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
Date curDate = new Date(System.currentTimeMillis() + iMin * 60 * 1000);
return formatter.format(curDate);
}
public static String GetSysCurrentTime() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
String str = formatter.format(curDate);
return str;
}
/**
* @param iMin
* 分钟 需要增加的分钟数
* @return 获取当前的时间
*/
public static String GetSysCurrentTime(int iMin) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis() + iMin * 60 * 1000);// 获取当前时间
String str = formatter.format(curDate);
return str;
}
public static int GetCurrentDay(String datetimeString) {
Calendar cal = new GregorianCalendar();
SimpleDateFormat oSdf = new SimpleDateFormat("yyyy-MM-dd");
try {
cal.setTime(oSdf.parse(datetimeString));
} catch (ParseException e) {
e.printStackTrace();
}
return cal.get(Calendar.DAY_OF_MONTH);
}
public static int GetCurrentMonth(String datetimeString) {
Calendar cal = new GregorianCalendar();
SimpleDateFormat oSdf = new SimpleDateFormat("yyyy-MM-dd");
try {
cal.setTime(oSdf.parse(datetimeString));
} catch (ParseException e) {
e.printStackTrace();
}
return cal.get(Calendar.MONTH);
}
public static int GetCurrentYear(String datetimeString) {
Calendar cal = new GregorianCalendar();
SimpleDateFormat oSdf = new SimpleDateFormat("yyyy-MM-dd");
try {
cal.setTime(oSdf.parse(datetimeString));
} catch (ParseException e) {
e.printStackTrace();
}
return cal.get(Calendar.YEAR);
}
public static int GetCurrentHour(String datetimeString, Context mContext) {
Calendar cal = new GregorianCalendar();
SimpleDateFormat oSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
cal.setTime(oSdf.parse(datetimeString));
} catch (ParseException e) {
e.printStackTrace();
}
boolean a = android.text.format.DateFormat.is24HourFormat(mContext);
boolean b = false;
if (!a) {
// 12小时
int am = cal.get(Calendar.AM_PM);
if (am == 1) {
// pm
b = true;
}
}
return b == true ? cal.get(Calendar.HOUR) + 12 : cal.get(Calendar.HOUR);
}
public static int GetCurrentMinute(String datetimeString) {
Calendar cal = new GregorianCalendar();
SimpleDateFormat oSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
cal.setTime(oSdf.parse(datetimeString));
} catch (ParseException e) {
e.printStackTrace();
}
return cal.get(Calendar.MINUTE);
}
}