import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
*
* 日期时间工具类
*
* 功能:
* 获取当前日期 时间 ,获取年月日时分秒毫秒
* 格式化日期时间
* 日期时间加上或减少n天、n月、n年
* 计算日期相差的天数
*
* @author Administrator
* @Date Jul 19, 2008
* @Time 9:47:53 AM
* @version 1.0
*/
public class DateUtil {
public static Date date
= null;
/**
* 英文简写(默认)如:2010-12-01
*/
public static String FORMAT_SHORT
= "yyyy-MM-dd";
/**
* 英文全称 如:2010-12-01 23:15:06
*/
public static String FORMAT_LONG
= "yyyy-MM-dd HH:mm:ss";
/**
* 精确到毫秒的完整时间 如:yyyy-MM-dd HH:mm:ss.S
*/
public static String FORMAT_FULL
= "yyyy-MM-dd HH:mm:ss.S";
/**
* 中文简写 如:2010年12月01日
*/
public static String FORMAT_SHORT_CN
= "yyyy年MM月dd";
/**
* 中文全称 如:2010年12月01日 23时15分06秒
*/
public static String FORMAT_LONG_CN
= "yyyy年MM月dd日 HH时mm分ss秒";
/**
* 精确到毫秒的完整中文时间
*/
public static String FORMAT_FULL_CN
= "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒";
/**
* 获得默认的 date pattern
*/
public static String getDatePattern
() {
return FORMAT_LONG;
}
/**
* 根据预设格式返回当前日期
*
* @return
*/
public static String getNow
() {
return format
(new Date());
}
/**
* 根据用户格式返回当前日期
*
* @param format
* @return
*/
return format
(new Date(), format
);
}
/**
* 使用预设格式格式化日期
*
* @param date
* @return
*/
return format(date, getDatePattern());
}
/**
* 使用用户格式格式化日期
*
* @param date
* 日期
* @param pattern
* 日期格式
* @return
*/
if (date != null) {
returnValue = df.format(date);
}
return (returnValue);
}
/**
* 使用预设格式提取字符串日期
*
* @param strDate
* 日期字符串
* @return
*/
return parse(strDate, getDatePattern());
}
/**
* 使用用户格式提取字符串日期
*
* @param strDate
* 日期字符串
* @param pattern
* 日期格式
* @return
*/
try {
return df.parse(strDate);
e.printStackTrace();
return null;
}
}
/**
* 在日期上增加数个整月
*
* @param date
* 日期
* @param n
* 要增加的月数
* @return
*/
public static Date addMonth
(Date date,
int n
) {
cal.setTime(date);
return cal.getTime();
}
/**
* 在日期上增加天数
*
* @param date
* 日期
* @param n
* 要增加的天数
* @return
*/
public static Date addDay
(Date date,
int n
) {
cal.setTime(date);
return cal.getTime();
}
/**
* 获取距现在某一小时的时刻
*
* @param format
* 格式化时间的格式
* @param h
* 距现在的小时 例如:h=-1为上一个小时,h=1为下一个小时
* @return
*/
date.setTime(date.getTime() + h * 60 * 60 * 1000);
return sdf.format(date);
}
/**
* 获取时间戳
*/
public static String getTimeString
() {
return df.format(calendar.getTime());
}
/**
* 获取日期年份
*
* @param date
* 日期
* @return
*/
return format(date).substring(0, 4);
}
/**
* 功能描述:返回月
*
* @param date
* Date 日期
* @return 返回月份
*/
public static int getMonth
(Date date
) {
calendar.setTime(date);
return calendar.
get(Calendar.
MONTH) + 1;
}
/**
* 功能描述:返回日
*
* @param date
* Date 日期
* @return 返回日份
*/
public static int getDay
(Date date
) {
calendar.setTime(date);
return calendar.
get(Calendar.
DAY_OF_MONTH);
}
/**
* 功能描述:返回小
*
* @param date
* 日期
* @return 返回小时
*/
public static int getHour
(Date date
) {
calendar.setTime(date);
return calendar.
get(Calendar.
HOUR_OF_DAY);
}
/**
* 功能描述:返回分
*
* @param date
* 日期
* @return 返回分钟
*/
public static int getMinute
(Date date
) {
calendar.setTime(date);
}
/**
* 返回秒钟
*
* @param date
* Date 日期
* @return 返回秒钟
*/
public static int getSecond
(Date date
) {
calendar.setTime(date);
}
/**
* 功能描述:返回毫
*
* @param date
* 日期
* @return 返回毫
*/
public static long getMillis
(Date date
) {
calendar.setTime(date);
return calendar.getTimeInMillis();
}
/**
* 按默认格式的字符串距离今天的天数
*
* @param date
* 日期字符串
* @return
*/
public static int countDays
(String date
) {
long t
= Calendar.
getInstance().
getTime().
getTime();
c.setTime(parse(date));
long t1 = c.getTime().getTime();
return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
}
/**
* 按用户格式字符串距离今天的天数
*
* @param date
* 日期字符串
* @param format
* 日期格式
* @return
*/
long t
= Calendar.
getInstance().
getTime().
getTime();
c.setTime(parse(date, format));
long t1 = c.getTime().getTime();
return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
}
public static void main
(String[] args
) {
System.
out.
println(DateUtil.
getNow());
}
}