import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* 作者:guoyzh
* 时间:2018年6月5日 17点37分
* 功能:时间判断的工具类 检测时间是否是今天 或者昨天
*/
public class DataUtils {
/**
* 日期格式:yyyy-MM-dd HH:mm:ss
*/
public static final String DATE_LONG = "yyyy-MM-dd HH:mm:ss";
/**
* 日期格式:yyyy-MM-dd
*/
public static final String DATE_SHORT = "yyyy-MM-dd";
/**
* 日期格式:yyyy-MM
*/
public static final String MONTH_SHORT = "yyyy-MM";
/**
* 日期格式 yyyyMMdd
*/
public static final String DATE_NORMAL = "yyyyMMdd";
/**
* 将字符串转位日期类型
*
* @param sdate
* @return
*/
public static Date toDate(String sdate) {
try {
return dateFormater.get().parse(sdate);
} catch (ParseException e) {
return null;
}
}
private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();
/**
* 判断是否为今天(效率比较高)
*
* @param day 传入的 时间 "2016-06-28 10:10:30" "2016-06-28" 都可以
* @return true今天 false不是
* @throws ParseException
*/
public static boolean IsToday(String day) throws ParseException {
Calendar pre = Calendar.getInstance();
Date predate = new Date(System.currentTimeMillis());
pre.setTime(predate);
Calendar cal = Calendar.getInstance();
Date date = getDateFormat().parse(day);
cal.setTime(date);
if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
int diffDay = cal.get(Calendar.DAY_OF_YEAR)
- pre.get(Calendar.DAY_OF_YEAR);
if (diffDay == 0) {
return true;
}
}
return false;
}
/**
* 判断是否为昨天(效率比较高)
*
* @param day 传入的 时间 "2016-06-28 10:10:30" "2016-06-28" 都可以
* @return true今天 false不是
* @throws ParseException
*/
public static boolean IsYesterday(String day) throws ParseException {
Calendar pre = Calendar.getInstance();
Date predate = new Date(System.currentTimeMillis());
pre.setTime(predate);
Calendar cal = Calendar.getInstance();
Date date = getDateFormat().parse(day);
cal.setTime(date);
if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
int diffDay = cal.get(Calendar.DAY_OF_YEAR)
- pre.get(Calendar.DAY_OF_YEAR);
if (diffDay == -1) {
return true;
}
}
return false;
}
public static SimpleDateFormat getDateFormat() {
if (null == DateLocal.get()) {
DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA));
}
return DateLocal.get();
}
/**
* 获取当前时间字符串
*
* @return
*/
public static String getNowString() {
return dateFormat(new Date());
}
/**
* 从时间格式化字符串
* 默认格式为:yyyy-MM-dd HH:mm:ss
*
* @param date
* @return
*/
public static String dateFormat(Date date) {
return dateFormat(date, DATE_LONG);
}
/**
* 从时间格式化字符串
*
* @param date
* @param format
* @return
*/
public static String dateFormat(Date date, String format) {
if (date == null) {
date = new Date();
}
if (format == null || format.equals("")) {
format = DATE_LONG;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
/**
* 从字符串格式化时间
*
* @param dateStr
* @param format
* @return
*/
public static Date dateFromString(String dateStr, String format) {
if (!dateStr.equals("") && !format.equals("")) {
DateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 增加时间数
*
* @param date
* @param field
* @param interval
* @return
*/
public static Date addOnField(Date date, int field, int interval) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.add(field, interval);
return ca.getTime();
}
/**
* 获取某个块
*
* @param date
* @param field
* @return
*/
public static int getFieldOfDate(Date date, int field) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(field);
}
/**
* 去除时分秒
*/
public static Date getShortDate(Date date) {
date = date == null ? new Date() : date;
String cleanDateStr = DateUtil.dateFormat(date, DateUtil.DATE_SHORT);
return DateUtil.dateFromString(cleanDateStr, DateUtil.DATE_SHORT);
}
/**
* 通过时间str获取当前年月日
*
* @param dateStr
* @return
*/
public static String getShortStrDate(String dateStr) {
Date date = DateUtil.dateFromString(dateStr, DateUtil.DATE_SHORT);
return DateUtil.dateFormat(date, DateUtil.DATE_SHORT);
}
}
Android 工具类 判断 今天 昨天 dateUtils
猜你喜欢
转载自blog.csdn.net/u010838785/article/details/80584760
今日推荐
周排行