java的DateUtil

经常使用


import java.util.Calendar;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;


public class DateUtil {
// SimpleDateFormat线程不安全, so ThreadLocal
private static final ThreadLocal<DateFormats> dateFormats = new ThreadLocal<DateFormats>() {
protected DateFormats initialValue() {
return new DateFormats();
}
};


private static final int DAY_MI_SECOND = 24 * 60 * 60 * 1000;
private static final String TOADY_STR = formatYMD(new Date());


public static String formatYMD(Date date) {
return dateFormats.get().ymd.format(date);
}


public static String formatHMS(Date date) {
return dateFormats.get().hms.format(date);
}

public static String formatHM(Date date) {
return dateFormats.get().hm.format(date);
}


public static String formatYMDHM(Date date) {
return dateFormats.get().ymdhm.format(date);
}


public static String formatYMDHMS(Date date) {
return dateFormats.get().ymdhms.format(date);
}


public static String formatYMDChinese(Date date) {
return dateFormats.get().ymdChinese.format(date);
}


public static String formatYMDSlash(Date date) {
return dateFormats.get().ymdSlash.format(date);
}


public static Date parseYMD(String dateStr) {
return parse(dateFormats.get().ymd, dateStr);
}


public static Date parseYMDHMS(String dateStr) {
return parse(dateFormats.get().ymdhms, dateStr);
}

public static Date parseTodayHMS(String dateStr) {
String today = formatYMD(new Date());
String todayDateStr = String.format("%s %s", today, dateStr);
return parse(dateFormats.get().ymdhms, todayDateStr);
}


/**
* 判断当前时间是否在某段时间内 参数不区分先后顺序
*/
public static boolean isDuringTwoDate(Date date, Date another) {
long dateTime = date.getTime();
long anotherTime = another.getTime();
long currentTime = new Date().getTime();


if (currentTime > dateTime && currentTime < anotherTime) {
return true;
} else if (currentTime > anotherTime && currentTime < dateTime) {
return true;
} else {
return false;
}
}


public static Date parse(SimpleDateFormat format, String dateStr) {
try {
Date d = format.parse(dateStr);
// check range for mysql
// mysql date range : '1000-01-01' to '9999-12-31'
Calendar c = Calendar.getInstance();
c.setTime(d);
int year = c.get(Calendar.YEAR);
if (year >= 1000 && year <= 9999) {
return d;
} else {
return null;
}
} catch (ParseException ex) {
return null;
}
}


public static Date changeString2Date(String time) {
String t = String.format("%s-%s-%s %s", Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH) + 1, Calendar.getInstance().get(Calendar.DATE), time);
return DateUtil.parseYMDHMS(t);
}


public static long daysOffset(Date date1, Date date2) {
date1 = DateUtil.parseYMD(DateUtil.formatYMD(date1));
date2 = DateUtil.parseYMD(DateUtil.formatYMD(date2));


return (date1.getTime() - date2.getTime()) / DAY_MI_SECOND;
}


/**
* 今天是星期几 , 7表示星期日
* 
* @return
*/
public static int getTodayDayOfWeek() {
Calendar now = Calendar.getInstance();
int dayOfweek = now.get(Calendar.DAY_OF_WEEK);
dayOfweek--;
if (dayOfweek == 0) {
dayOfweek = 7;
}
return dayOfweek;
}


public static boolean isCorrectDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Calendar todayCalendar = Calendar.getInstance();
if (calendar.get(Calendar.YEAR) != todayCalendar.get(Calendar.YEAR)) {
return false;
} else if (calendar.get(Calendar.MONTH) != todayCalendar.get(Calendar.MONTH)) {
return false;
} else if (calendar.get(Calendar.DAY_OF_MONTH) != todayCalendar.get(Calendar.DAY_OF_MONTH)) {
return false;
}
return true;
}


public static boolean isToday(Date createDate) {
String formatYMD = formatYMD(createDate);
return TOADY_STR.equals(formatYMD);
}
}


class DateFormats {
public final SimpleDateFormat hms = new SimpleDateFormat("HH:mm:ss");
public final SimpleDateFormat hm = new SimpleDateFormat("HH:mm");
public final SimpleDateFormat ymdhm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
public final SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
public final SimpleDateFormat ymdhms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public final SimpleDateFormat ymdChinese = new SimpleDateFormat("yyyy年MM月dd");
public final SimpleDateFormat ymdSlash = new SimpleDateFormat("yyyy/MM/dd");
}


你可能感兴趣的:(Date)