public class DateUtil {
public static final String YYYY_MM_DD = "yyyy-MM-dd";
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static final long ONE_DAY = 1000 * 60 * 60 * 24;
@SuppressLint("SimpleDateFormat")
public static SimpleDateFormat create(String format) {
return new SimpleDateFormat(format);
}
/**
* 获取某年某月有多少天
*/
public static int getDayOfMonth(int year, int month) {
Calendar c = Calendar.getInstance();
c.set(year, month, 0);
//输入类型为int类型
return c.get(Calendar.DAY_OF_MONTH);
}
/**
* 当前时间往前往后monthOffsize个月
* @param monthOffsize
* @return 处理后的字符串
*/
public static String getMonthOffSize(int monthOffsize){
String result = "";
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH,monthOffsize);
result = getYYMMddByDate(c.getTime());
return result;
}
/**
* 获取当前时间往前往后dayOffsize天的日期
*
* @param dayOffsize
* @return
*/
public static String getDayOffSize(int dayOffsize) {
String result = "";
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH, dayOffsize);
result = getYYMMddByDate(c.getTime());
return result;
}
/**
* 获取两个时间相差的天数
*
* @param time1 time1
* @param time2 time2
* @return time1 - time2相差的天数
*/
public static int getDayOffset(long time1, long time2) {
// 将小的时间置为当天的0点
long offsetTime;
if (time1 > time2) {
offsetTime = time1 - getDayStartTime(getCalendar(time2)).getTimeInMillis();
} else {
offsetTime = getDayStartTime(getCalendar(time1)).getTimeInMillis() - time2;
}
return (int) (offsetTime / ONE_DAY);
}
public static Calendar getCalendar(long time) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
return calendar;
}
public static Calendar getDayStartTime(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar;
}
/**
* 字符串转Date:如 2020-06-01-->Date
*
* @param dateStr
* @return
*/
public static Date getDateFromStr(String dateStr) {
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
} catch (Exception e) {
e.printStackTrace();
date = new Date();
}
return date;
}
/**
* 日期字符串格式化:如 2020-06-01 11:11:11.0-->2020-06-01 11:11:11
*
* @param dateStr
* @param withHMS 是否带有时分秒
* @return
*/
public static String getDateStrFromStr(String dateStr, boolean withHMS) {
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
} catch (Exception e) {
e.printStackTrace();
date = new Date();
}
return withHMS ? getYYMMddHHMMSS(date) : getYYMMddByDate(date);
}
public static String getDateStrFromStr(String dateStr) {
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
} catch (Exception e) {
e.printStackTrace();
return "";
}
return getYMdByDate(date);
}
/**
* 获取当天的日期
*
* @return eg. 2020-06-04
*/
public static String getTodayYYMMdd() {
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
}
public static String getTodayYYMMddWithYMD() {
return new SimpleDateFormat("yyyy年MM月dd日").format(new Date());
}
/**
* 获取当天的日期
*
* @return eg. 2020-06-04 18:00:00
*/
public static String getTodayYYMMddHHMMSS() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
/**
* 获取指定日期字符串格式
*
* @return eg. 2020-06-04 18:00:00
*/
public static String getYYMMddHHMMSS(Date date) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
}
/**
* 获取指定日期的字符串个数
*
* @return eg. 2020-06-04
*/
public static String getYYMMddByDate(Date date) {
if (date == null) {
return null;
}
return new SimpleDateFormat("yyyy-MM-dd").format(date);
}
public static String getYMdByDate(Date date) {
if (date == null) {
return null;
}
return new SimpleDateFormat("yyyy年MM月dd日").format(date);
}
public static long getStandardTime(String text, String formatStr) {
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
SimpleDateFormat format = new SimpleDateFormat(formatStr);
Date date = null;
long time;
try {
date = format.parse(text);
time = date.getTime();
} catch (ParseException var6) {
time = System.currentTimeMillis();
var6.printStackTrace();
}
return time;
}
}