public class DateUtil {
public final static String DATE_FORMAT = "yyyy-MM-dd";
public final static String DATE_FORMAT2 = "yyyy/MM/dd";
public final static String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public final static String DATE_TIME_FORMAT2 = "yyyy/MM/dd HH:mm:ss";
public final static String FORMAT_DEFAULT = "default";
public final static String FORMAT_FULL = "full";
public final static String FORMAT_LONG = "long";
public final static String FORMAT_MEDIUM = "medium";
public final static String FORMAT_SHORT = "short";
public final static String TIME_FORMAT = "HH:mm:ss";
/**
* @category 当前时间(毫秒)
*/
public static long current() {
return System.currentTimeMillis();
}
/**
* @category 返回int的值,表示两个指定日期间的时间间隔数目
*/
public static int dateDiff(String interval, Date date1, Date date2)
throws Exception {
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar1.setTime(date1);
calendar2.setTime(date2);
// interval 年;
if (interval.equals("yyyy")) {
return (calendar2.get(Calendar.YEAR) - calendar1.get(Calendar.YEAR));
}
// interval 季;
if (interval.equals("q")) {
int increaseYear = calendar2.get(Calendar.YEAR)
- calendar1.get(Calendar.YEAR);
int increaseMonth = calendar2.get(Calendar.MONTH)
- calendar1.get(Calendar.MONTH);
return (increaseMonth + increaseYear * 12) / 3;
}
// interval 月;
if (interval.equals("m")) {
int increaseYear = calendar2.get(Calendar.YEAR)
- calendar1.get(Calendar.YEAR);
int increaseMonth = calendar2.get(Calendar.MONTH)
- calendar1.get(Calendar.MONTH);
return increaseMonth + increaseYear * 12;
}
// interval 日;
if (interval.equals("d")) {
long increaseDate = (calendar2.getTimeInMillis() - calendar1
.getTimeInMillis()) / 1000 / 60 / 60 / 24;
return (int) increaseDate;
}
// interval 周;
if (interval.equals("ww")) {
long increaseDate = (calendar2.getTimeInMillis() - calendar1
.getTimeInMillis()) / 1000 / 60 / 60 / 24 / 7;
return (int) increaseDate;
}
// interval 时;
if (interval.equalsIgnoreCase("h")) {
long increaseDate = (calendar2.getTimeInMillis() - calendar1
.getTimeInMillis()) / 1000 / 60 / 60;
return (int) increaseDate;
}
// interval 分;
if (interval.equals("n")) {
long increaseDate = (calendar2.getTimeInMillis() - calendar1
.getTimeInMillis()) / 1000 / 60;
return (int) increaseDate;
}
// interval 秒;
if (interval.equals("s")) {
long increaseDate = (calendar2.getTimeInMillis() - calendar1
.getTimeInMillis()) / 1000;
return (int) increaseDate;
}
throw new Exception("interval error!");
}
/**
* @category 返回一个包含已知日期的指定时间部分的值
*/
public static int datePart(String interval, Date date) throws Exception {
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(date);
// interval 年;
if (interval.equals("yyyy")) {
return rightNow.get(Calendar.YEAR);
}
// interval 月;
if (interval.equals("m")) {
return rightNow.get(Calendar.MONTH) + 1;
}
// interval 日;
if (interval.equals("d")) {
return rightNow.get(Calendar.DAY_OF_MONTH);
}
// interval 时;
if (interval.equals("h")) {
return rightNow.get(Calendar.HOUR_OF_DAY);
}
// interval 分;
if (interval.equals("n")) {
return rightNow.get(Calendar.MINUTE);
}
// interval 秒;
if (interval.equals("s")) {
return rightNow.get(Calendar.SECOND);
}
throw new Exception("interval error!");
}
/**
* @category 返回包含指定的年、月、日的日期
*/
public static Date dateSerial(int year, int month, int day) {
Calendar rightNow = Calendar.getInstance();
rightNow.set(year, month - 1, day);
return rightNow.getTime();
}
/**
* @category 返回包含指定的年、月、日,小时,分,秒的日期
*/
public static Date dateSerial(int year, int month, int day, int hour,
int minute, int second) {
Calendar rightNow = Calendar.getInstance();
rightNow.set(year, month - 1, day, hour, minute, second);
return rightNow.getTime();
}
/**
* @category 返回一个日期,格式yyyy-MM-dd"; "yyyy-MM-dd HH:mm:ss";
*/
public static Date dateValue(String date) {
String date_format;
Date fDate = null;
try {
if (date.indexOf(":") > 0) {
if (date.indexOf("/") > 0) {
date_format = DATE_TIME_FORMAT2;
} else {
date_format = DATE_TIME_FORMAT;
}
} else {
if (date.indexOf("/") > 0) {
date_format = DATE_FORMAT2;
} else {
date_format = DATE_FORMAT;
}
}
SimpleDateFormat formatter = new SimpleDateFormat(date_format);
fDate = formatter.parse(date);
} catch (Exception e) {
fDate = null;
}
return fDate;
}
/**
* @category 返回一个日期,格式yyyy-MM-dd"; "yyyy-MM-dd HH:mm:ss";
*/
public static Date dateValue(long millis) {
Date fDate = null;
try {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millis);
fDate = calendar.getTime();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
return fDate;
}
/**
* @category 格式化日期
*/
public static String format(Date date, String pattern) {
if (date == null) {
return "";
} else if (pattern == null) {
return DateFormat.getDateInstance().format(date);
} else if (pattern.trim().length() == 0) {
return DateFormat.getDateInstance().format(date);
} else if (pattern.equalsIgnoreCase(FORMAT_DEFAULT)) {
return DateFormat.getDateInstance().format(date);
} else if (pattern.equalsIgnoreCase(FORMAT_LONG)) {
return DateFormat.getDateInstance(DateFormat.LONG).format(date);
} else if (pattern.equalsIgnoreCase(FORMAT_MEDIUM)) {
return DateFormat.getDateInstance(DateFormat.MEDIUM).format(date);
} else if (pattern.equalsIgnoreCase(FORMAT_SHORT)) {
return DateFormat.getDateInstance(DateFormat.SHORT).format(date);
} else if (pattern.equalsIgnoreCase(FORMAT_FULL)) {
return DateFormat.getDateInstance(DateFormat.FULL).format(date);
} else if (pattern.equalsIgnoreCase(FORMAT_LONG)) {
return DateFormat.getInstance().format(date);
} else {
return new SimpleDateFormat(pattern).format(date);
}
}
}