- /**
- *
- * <p>
- * Title: DateUtil.java
- * </p>
- * <p>
- * Description: 一些日期的工具方法,時間格式全部爲二十四小時制
- * </p>
- *
- */
- public class DateUtil {
- // //////////////////////////////////////////////////////////
- // /以下是將日期轉換成字符串的方法
- /**
- * 返回當前的時間 yyyy-MM-dd HH:mm:ss
- *
- * @return
- */
- public static String getNowDateTime() {
- return DateFormateFactory.getS("datetime").format(new Date());
- }
- /**
- * 將傳入的日期轉換成 yyyy-MM-dd HH:mm:ss的形式
- *
- * @param date
- * @return
- */
- public static String getDateTime(Date date) {
- return DateFormateFactory.getS("datetime").format(date);
- }
- /**
- * 將傳入的long轉換成 yyyy-MM-dd HH:mm:ss的形式
- *
- * @param time
- * @return
- */
- public static String getDateTime(long time) {
- return DateFormateFactory.getS("datetime").format(time);
- }
- /**
- * 返回當前日期 yyyy-MM-dd
- *
- * @return
- */
- public static String getNowDate() {
- return DateFormateFactory.getS("date").format(new Date());
- }
- /**
- * 將傳入的日期轉換成 yyyy-MM-dd的形式
- *
- * @param date
- * @return
- */
- public static String getDate(Date date) {
- return DateFormateFactory.getS("date").format(date);
- }
- /**
- * 將傳入的long轉換成 yyyy-MM-dd的形式
- *
- * @param date
- * @return
- */
- public static String getDate(long time) {
- return DateFormateFactory.getS("date").format(time);
- }
- /**
- * 返回當前的年月 yyyy-MM
- *
- * @return
- */
- public static String getNowYearMonth() {
- return DateFormateFactory.getS("yearmonth").format(new Date());
- }
- /**
- * 將傳入的日期轉換成 yyyy-MM形式
- *
- * @param date
- * @return
- */
- public static String getYearMonth(Date date) {
- return DateFormateFactory.getS("yearmonth").format(date);
- }
- /**
- * 將傳入的long轉換成 yyyy-MM形式
- *
- * @param date
- * @return
- */
- public static String getYearMonth(long time) {
- return DateFormateFactory.getS("yearmonth").format(time);
- }
- /**
- * 得到程序員自定義格式的當前時間字符串
- *
- * @param pattern
- * @return
- * @throws IllegalArgumentException
- * 如果輸入的pattern不正確
- */
- public static String getNowByPattern(String pattern)
- throws IllegalArgumentException {
- try {
- return new SimpleDateFormat(pattern).format(new Date());
- } catch (IllegalArgumentException e) {
- throw new IllegalArgumentException("輸入參數不正確", e);
- }
- }
- /**
- * 得到程序員自定義格式的特定時間字符串
- *
- * @param pattern
- * @param date
- * @return
- * @throws IllegalArgumentException
- * 如果輸入的pattern不正確
- */
- public static String getByPattern(String pattern, Date date)
- throws IllegalArgumentException {
- try {
- return new SimpleDateFormat(pattern).format(date);
- } catch (IllegalArgumentException e) {
- throw new IllegalArgumentException("輸入參數不正確", e);
- }
- }
- /**
- * 得到程序員自定義格式的特定long時間的字符串
- *
- * @param pattern
- * @param date
- * @return
- * @throws IllegalArgumentException
- * 如果輸入的pattern不正確
- */
- public static String getByPattern(String pattern, long time)
- throws IllegalArgumentException {
- try {
- return new SimpleDateFormat(pattern).format(time);
- } catch (IllegalArgumentException e) {
- throw new IllegalArgumentException("輸入參數不正確", e);
- }
- }
- // /////////////////////////////////////////////////////
- // 以下是將字符串轉換成日期的方法,一般用於解析界面上傳入的日期數據
- /**
- * 將格式爲 yyyy-MM-dd 形式的字符串轉換成日期
- */
- public static Date parseDate(String sourse) throws Exception {
- try {
- return DateFormateFactory.getS("date").parse(sourse);
- } catch (ParseException e) {
- throw new Exception("期待 yyyy-MM-dd 格式的日期,但輸入格式爲: " + sourse, e);
- }
- }
- /**
- * 將格式爲 yyyy-MM-dd HH:mm:ss 的字符串轉換成日期
- *
- * @param sourse
- * @return
- * @throws Exception
- */
- public static Date parseDateTime(String sourse) throws Exception {
- try {
- return DateFormateFactory.getS("datetime").parse(sourse);
- } catch (ParseException e) {
- throw new Exception("期待 yyyy-MM-dd HH:mm:ss 格式的日期,但輸入格式爲: "
- + sourse, e);
- }
- }
- /**
- * 將傳入的時間間隔(毫秒)轉換成 xx天xx小時xx分xx秒的形式
- *
- * @param distance
- * @return
- * @deprecated 因爲暫時沒有實現,所以就不推薦使用了 ^.^
- */
- public static String getDistance(long distance) {
- return null;
- }
- /**
- * 得到與傳入時間date,在字段offsetField上相差offset的date
- *
- * @param date
- * @param offset
- * @param offsetField
- * Calendar類的field
- * @return
- */
- public static Date getDistanceDate(Date date, int offset, int offsetField) {
- Calendar c = Calendar.getInstance();
- c.setTime(date);
- c.set(offsetField, c.get(offsetField) + offset);
- return c.getTime();
- }
- /**
- * 得到兩個日期之間查幾天。
- * @param date1
- * @param date2
- * @return
- */
- public static int getOffsetIn2Date(Date date1, Date date2) {
- long offsetNum = date1.getTime() - date2.getTime();
- return (int) (offsetNum / (60 * 60 * 24 * 1000));
- }
- /**
- * 兩天之前差多少分鐘
- * @param date1
- * @param date2
- * @return
- */
- public static int getOffsetMinIn2Date(Date date1, Date date2){
- long offsetNum = date1.getTime() - date2.getTime();
- return ((int) (offsetNum / (60 * 1000)));
- }
- public static void main(String[] args) throws Exception {
- Date date = getDistanceDate(new Date(), -1, Calendar.DAY_OF_MONTH);
- System.out.println(getByPattern("yyyyMMdd", date));
- }
- }
- class DateFormateFactory {
- private static Map<String, SimpleDateFormat> map = new HashMap<String, SimpleDateFormat>();
- static {
- map.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
- map.put("date", new SimpleDateFormat("yyyy-MM-dd"));
- map.put("yearmonth", new SimpleDateFormat("yyyy-MM"));
- map.put("date1", new SimpleDateFormat("yyyyMMdd"));
- }
- protected static SimpleDateFormat getS(String formate) {
- return map.get(formate);
- }
- }