1. 程式人生 > >【Java】DateUtil(2)

【Java】DateUtil(2)

繼承 ava sim pla bool private throw ons tar

import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.lang3.time.DateFormatUtils; /** * 日期工具類, 繼承org.apache.commons.lang.time.DateUtils類 * @author * @version 2015-10-12 */ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" }; /** * 得到當前日期字符串 格式(yyyy-MM-dd) */ public static String getDate() { return getDate("yyyy-MM-dd"); }
/** * 得到當前日期字符串 格式(yyyy-MM-dd) pattern可以為:"yyyy-MM-dd" "HH:mm:ss" "E" */ public static String getDate(String pattern) { return DateFormatUtils.format(new Date(), pattern); } /** * 得到日期字符串 默認格式(yyyy-MM-dd) pattern可以為:"yyyy-MM-dd" "HH:mm:ss" "E" */
public static String formatDate(Date date, Object... pattern) { String formatDate = null; if (pattern != null && pattern.length > 0) { formatDate = DateFormatUtils.format(date, pattern[0].toString()); } else { formatDate = DateFormatUtils.format(date, "yyyy-MM-dd"); } return formatDate; } /** * 得到日期時間字符串,轉換格式(yyyy-MM-dd HH:mm:ss) */ public static String formatDateTime(Date date) { return formatDate(date, "yyyy-MM-dd HH:mm:ss"); } /** * 得到當前時間字符串 格式(HH:mm:ss) */ public static String getTime() { return formatDate(new Date(), "HH:mm:ss"); } /** * 得到當前日期和時間字符串 格式(yyyy-MM-dd HH:mm:ss) */ public static String getDateTime() { return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"); } /** * 得到當前年份字符串 格式(yyyy) */ public static String getYear() { return formatDate(new Date(), "yyyy"); } /** * 得到當前月份字符串 格式(MM) */ public static String getMonth() { return formatDate(new Date(), "MM"); } /** * 得到當天字符串 格式(dd) */ public static String getDay() { return formatDate(new Date(), "dd"); } /** * 得到當前星期字符串 格式(E)星期幾 */ public static String getWeek() { return formatDate(new Date(), "E"); } /** * 日期型字符串轉化為日期 格式 * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" } */ public static Date parseDate(Object str) { if (str == null){ return null; } try { return parseDate(str.toString(), parsePatterns); } catch (ParseException e) { return null; } } /** * 獲取過去的天數 * @param date * @return */ public static long pastDays(Date date) { long t = new Date().getTime()-date.getTime(); return t/(24*60*60*1000); } public static Date getDateStart(Date date) { if(date==null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { date= sdf.parse(formatDate(date, "yyyy-MM-dd")+" 00:00:00"); } catch (ParseException e) { e.printStackTrace(); } return date; } public static Date getDateEnd(Date date) { if(date==null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { date= sdf.parse(formatDate(date, "yyyy-MM-dd") +" 23:59:59"); } catch (ParseException e) { e.printStackTrace(); } return date; } /** * 判斷字符串是否是日期 * @param timeString * @return */ public static boolean isDate(String timeString){ SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); format.setLenient(false); try{ format.parse(timeString); }catch(Exception e){ return false; } return true; } /** * 格式化時間 * @param timestamp * @return */ public static String dateFormat(Date timestamp){ SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.format(timestamp); } /** * 獲取系統時間Timestamp * @return */ public static Timestamp getSysTimestamp(){ return new Timestamp(new Date().getTime()); } /** * 獲取系統時間Date * @return */ public static Date getSysDate(){ return new Date(); } /** * 生成時間隨機數 * @return */ public static String getDateRandom(){ String s=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()); return s; } /** * @param args * @throws ParseException */ public static void main(String[] args) throws ParseException { // System.out.println(formatDate(parseDate("2010/3/6"))); // System.out.println(getDate("yyyy年MM月dd日 E")); // long time = new Date().getTime()-parseDate("2012-11-19").getTime(); // System.out.println(time/(24*60*60*1000)); } }

【Java】DateUtil(2)