此工具类提供的方法都比较简单,但实用。在此备忘。
类中方法包括:
1. 获取N天后(前)的日期,getDateAfterNDays(int)
2. 获取两日期间隔天数,countDateInterval(Date earlyDate, Date lateDate)
3. 字符串转日期(支持四种格式),string2Date(String dateStr)
其中获取两日期的间隔天数,考虑到两日期可能间隔秒数不足一日的情况,统一规格化为0时0分0秒
package chow;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateHandler {
/**
* 日期字符串转为Date
* 支持格式 yyyy-MM-dd, yyyyMMdd, yyyy/MM/dd, yyyy年MM月dd日
* @param dateStr
* @return Date
*/
public static Date string2Date(String dateStr){
Date date = null;
DateFormat df = null;
int index = dateStr.indexOf('-');
dateStr = dateStr.trim();
if(index > 0){
df = new SimpleDateFormat("yyyy-MM-dd");
}else if(dateStr.indexOf('/') > 0){
df = new SimpleDateFormat("yyyy/MM/dd");
}else if(dateStr.indexOf("年") > 0){
df = new SimpleDateFormat("yyyy年MM月dd日");
}else if(dateStr.length() == 8){
df = new SimpleDateFormat("yyyyMMdd");
}
try {
date = df.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 以baseDateStr为标准,返回n天后的date
* @param days 多少天后,负数代表多少天前
* @param baseDateStr 日期字符串
* @return Date days天后的日期
*/
public static Date getDateAfterNDays(int days, String baseDateStr){
Date date = string2Date(baseDateStr);
return getDateAfterNDays(days, date);
}
/**
* 以当前日期为标准,返回n天后的date
*
* @author Chow 2010-7-22
*/
public static Date getDateAfterNDays(int days) {
return getDateAfterNDays(days, new Date());
}
/**
* 以baseDate为标准,返回n天后的date
*
* @author chow 2010-7-22
*/
public static Date getDateAfterNDays(int days, Date baseDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(baseDate);
calendar.add(Calendar.DAY_OF_MONTH, days);
return calendar.getTime();
}
/**
* 返回earlyDate 距离 lateDate的天数
*
* @author chow 2010-7-22 下午05:00:47
*/
public static long countDateInterval(Date earlyDate, Date lateDate) {
long intervalDays = 0;
earlyDate = setHms0(earlyDate);
lateDate = setHms0(lateDate);
intervalDays = (earlyDate.getTime() - lateDate.getTime())
/ (1000 * 60 * 60 * 24);
return Math.abs(intervalDays);
}
/**
* 把date的时,分,秒置为0
* @param d Date
* @return Date
* @author chow 2010-9-3 下午03:20:15
*/
public static Date setHms0(Date d){
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
d = cal.getTime();
return d;
}
public static void main(String[] args) throws ParseException {
System.out.println(getDateAfterNDays(100, getDateAfterNDays(-3)));
System.out
.println(countDateInterval(getDateAfterNDays(120), new Date()));
System.out.println(new Date().getTime()+"\n"+1267866075);
handle();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String ss = df.format(string2Date("2019年4月1日"));
System.out.println(ss);
System.out.println(string2Date("2010-3-4"));
System.out.println(string2Date("20100903"));
System.out.println(string2Date("2010/3/2"));
System.out.println(string2Date("2019年4月1日"));
}
protected static void handle() {
long curTime = new Date().getTime() + 1;
// 转化为特定格式 YYMMDDHHMI
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(curTime);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = 0;
int minute = 0;
System.out.println(year + ":" + month + ":" + day);
// vedio.setPublic_time(year * 100000000 + month * 1000000 + day * 10000
// + hour * 100 + minute);
// "YYMMDDHHMI" -- "0711190501" -- 711190501
}
}