生活随笔
收集整理的這篇文章主要介紹了
java Date工具类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??????????public?class?DateUtil?{????????????????????????public?static?String?getNowDateTime()?{?????????return?DateFormateFactory.getS("datetime").format(new?Date());?????}????????????????public?static?String?getDateTime(Date?date)?{?????????return?DateFormateFactory.getS("datetime").format(date);?????}????????????????public?static?String?getDateTime(long?time)?{?????????return?DateFormateFactory.getS("datetime").format(time);?????}???????????????public?static?String?getNowDate()?{?????????return?DateFormateFactory.getS("date").format(new?Date());?????}????????????????public?static?String?getDate(Date?date)?{?????????return?DateFormateFactory.getS("date").format(date);?????}????????????????public?static?String?getDate(long?time)?{?????????return?DateFormateFactory.getS("date").format(time);?????}???????????????public?static?String?getNowYearMonth()?{?????????return?DateFormateFactory.getS("yearmonth").format(new?Date());?????}????????????????public?static?String?getYearMonth(Date?date)?{?????????return?DateFormateFactory.getS("yearmonth").format(date);?????}????????????????public?static?String?getYearMonth(long?time)?{?????????return?DateFormateFactory.getS("yearmonth").format(time);?????}??????????????????public?static?String?getNowByPattern(String?pattern)?????????????throws?IllegalArgumentException?{?????????try?{?????????????return?new?SimpleDateFormat(pattern).format(new?Date());?????????}?catch?(IllegalArgumentException?e)?{?????????????throw?new?IllegalArgumentException("輸入參數不正確",?e);?????????}?????}???????????????????public?static?String?getByPattern(String?pattern,?Date?date)?????????????throws?IllegalArgumentException?{?????????try?{?????????????return?new?SimpleDateFormat(pattern).format(date);?????????}?catch?(IllegalArgumentException?e)?{?????????????throw?new?IllegalArgumentException("輸入參數不正確",?e);?????????}?????}???????????????????public?static?String?getByPattern(String?pattern,?long?time)?????????????throws?IllegalArgumentException?{?????????try?{?????????????return?new?SimpleDateFormat(pattern).format(time);?????????}?catch?(IllegalArgumentException?e)?{?????????????throw?new?IllegalArgumentException("輸入參數不正確",?e);?????????}?????}???????????????????????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);?????????}?????}?????????????????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);?????????}?????}?????????????????public?static?String?getDistance(long?distance)?{??????????return?null;?????}???????????????????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();?????}????????????????public?static?int?getOffsetIn2Date(Date?date1,?Date?date2)?{?????????long?offsetNum?=?date1.getTime()?-?date2.getTime();?????????return?(int)?(offsetNum?/?(60?*?60?*?24?*?1000));?????}????????????????????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);?????}??}? ?
轉載于:https://blog.51cto.com/3760651/1135124
總結
以上是生活随笔為你收集整理的java Date工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。