java 关于日期

来源:互联网 发布:淘宝卖家为什么不发货 编辑:程序博客网 时间:2024/06/01 10:09

1,获取当前毫秒数

两种方式:

方式一:new Date().getTime()

 

方式二:System.currentTimeMillis()

 

2,Date 转化为Timestamp

Java代码  收藏代码
  1. timestamp=new Timestamp(new Date().getTime());  

 

3,格式化:

Java代码  收藏代码
  1. /*** 
  2.      * yyyy-MM-dd HH:mm:ss 
  3.      *  
  4.      * @param date 
  5.      * @return 
  6.      */  
  7.     public static String formatDateTime(Date date) {// format date ,such as  
  8.         SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMddHHmmss);  
  9.         String formatTimeStr = null;  
  10.         if(StringUtil.isNullOrEmpty(date)){  
  11.             /*若没有传递参数,则默认为当前时间*/  
  12.             date=new Date();  
  13.         }  
  14.         if (date != null) {  
  15.             formatTimeStr = sdf.format(date);  
  16.         }  
  17.         return formatTimeStr;  
  18.     }  

 

Java代码  收藏代码
  1. /*** 
  2.      * format : yyyy年MM月dd日 HH点mm分ss秒 
  3.      *  
  4.      * @param timestamp 
  5.      * @return 
  6.      */  
  7.     public static String formatTimestampZH(Timestamp timestamp) {  
  8.         if(StringUtil.isNullOrEmpty(timestamp)){  
  9.             /* 如果没有传参数timestamp,则默认为当前时间*/  
  10.             timestamp=new Timestamp(new Date().getTime());  
  11.         }  
  12.         SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDDHHMMSS_ZH);  
  13.         return sdf.format(timestamp);  
  14.     }  

 

4,获取指定天数之前的时间

Java代码  收藏代码
  1. public static Timestamp getTimestampBefore(Date d, int day) {  
  2.         Calendar now = Calendar.getInstance();  
  3.         now.setTime(d);  
  4.         now.set(Calendar.DATE, now.get(Calendar.DATE) - day);  
  5.         return new Timestamp(now.getTimeInMillis());  
  6.     }  
  7. /*** 
  8.      *  
  9.      * @param d : 基准日期 
  10.      * @param day : 几天前 
  11.      * @return 
  12.      * @throws ParseException 
  13.      */  
  14.     public static java.util.Date getDateBefore(String d, int day) throws ParseException {  
  15.         java.util.Date date=getDate4Str(d);  
  16.         return getDateBefore(date, day);  
  17.     }  
  18. /*** 
  19.      *  
  20.      * @param d : 基准时间 
  21.      * @param day 
  22.      * @return 
  23.      * @throws ParseException 
  24.      */  
  25.     public static long getSecondBefore(String d, int day) throws ParseException {  
  26.         java.util.Date date=getDate4Str(d);  
  27.         return getDateBefore(date, day).getTime()/1000;  
  28.     }  
  29.   
  30.     public static java.util.Date getDateBeforeMinute(Date d, int minutes) {  
  31.         Calendar now = Calendar.getInstance();  
  32.         now.setTime(d);  
  33.         now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) - minutes);  
  34.         return now.getTime();  
  35.     }  

 

5,获取指定天数之后的时间

Java代码  收藏代码
  1. /*** 
  2.      *  
  3.      * @param d 
  4.      *            :Base Date 
  5.      * @param day 
  6.      *            :Delayed days 
  7.      * @return 
  8.      */  
  9.     public static java.util.Date getDateAfter(Date d, int day) {  
  10.         Calendar now = Calendar.getInstance();  
  11.         now.setTime(d);  
  12.         now.set(Calendar.DATE, now.get(Calendar.DATE) + day);  
  13.         return now.getTime();  
  14.     }  
  15.   
  16.     public static java.util.Date getDateAfterByYear(Date d, int year) {  
  17.         Calendar now = Calendar.getInstance();  
  18.         now.setTime(d);  
  19.         now.set(Calendar.YEAR, now.get(Calendar.YEAR) + year);  
  20.         return now.getTime();  
  21.     }  
  22.   
  23.     /*** 
  24.      * 以月为单位 
  25.      * @param d 
  26.      * @param month 
  27.      * @return 
  28.      */  
  29.     public static java.util.Date getDateAfterByMonth(Date d, int month) {  
  30.         Calendar now = Calendar.getInstance();  
  31.         now.setTime(d);  
  32.         now.set(Calendar.MONTH, now.get(Calendar.MONTH) + month);  
  33.         return now.getTime();  
  34.     }  
  35.     /*** 
  36.      * 以小时为单位 
  37.      * @param d 
  38.      * @param month 
  39.      * @return 
  40.      */  
  41.     public static java.util.Date getDateAfterByHour(Date d, int hour) {  
  42.         Calendar now = Calendar.getInstance();  
  43.         now.setTime(d);  
  44.         now.set(Calendar.HOUR, now.get(Calendar.HOUR) + hour);  
  45.         return now.getTime();  
  46.     }  

 

6,Date 转化为Calendar

Java代码  收藏代码
  1. public static Calendar getCalendar(Date date) {  
  2.         Calendar c = Calendar.getInstance();  
  3.         c.setTime(date);  
  4.         return c;  
  5.     }  

 

7,校验日期

Java代码  收藏代码
  1. /** 
  2.      *  
  3.      * Determine whether date is valid, including the case of a leap year 
  4.      *  
  5.      * @param date 
  6.      *            YYYY-mm-dd 
  7.      * @return 
  8.      */  
  9.     public static boolean isDate(String date) {  
  10.         StringBuffer reg = new StringBuffer(  
  11.                 "^((\\d{2}(([02468][048])|([13579][26]))-?((((0?");  
  12.         reg.append("[13578])|(1[02]))-?((0?[1-9])|([1-2][0-9])|(3[01])))");  
  13.         reg.append("|(((0?[469])|(11))-?((0?[1-9])|([1-2][0-9])|(30)))|");  
  14.         reg.append("(0?2-?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][12");  
  15.         reg.append("35679])|([13579][01345789]))-?((((0?[13578])|(1[02]))");  
  16.         reg.append("-?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))");  
  17.         reg.append("-?((0?[1-9])|([1-2][0-9])|(30)))|(0?2-?((0?[");  
  18.         reg.append("1-9])|(1[0-9])|(2[0-8]))))))");  
  19.         Pattern p = Pattern.compile(reg.toString());  
  20.         return p.matcher(date).matches();  
  21.     }  

8,转化为cron 

Java代码  收藏代码
  1. /*** 
  2.      * convert Date to cron ,eg.  "0 06 10 15 1 ? 2014" 
  3.      * @param date  : 时间点 
  4.      * @return 
  5.      */  
  6.     public static String getCron(java.util.Date  date){  
  7.         String dateFormat="ss mm HH dd MM ? yyyy";  
  8.         return formatDateByPattern(date, dateFormat);  
  9.     }  

0 0
原创粉丝点击