码迷,mamicode.com
首页 > 编程语言 > 详细

java工具包一:日期处理

时间:2017-11-17 00:12:06      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:begin   else   邮箱   color   format   写代码   tar   ring   sim   

作者:NiceCui

  • 本文谢绝转载,如需转载需征得作者本人同意,谢谢。
  • 本文链接:http://www.cnblogs.com/NiceCui/p/7846812.html
  • 邮箱:moyi@moyibolg.com
  • 日期:2017-11-16

平时写代码有时会常用到一些处理日期的逻辑,自己写了一个工具包,方便每次处理

 

  1 import java.text.ParseException;
  2 import java.text.SimpleDateFormat;
  3 import java.util.Date;
  4 import org.junit.Test;
  5 
  6 /**
  7  * 
  8  * @author NiceCui
  9  * @date 2017-11-14
 10  * @cnblog 地址:http://www.cnblogs.com/NiceCui/
 11  *
 12  */    
 13 public class DateTool {
 14     
 15         
 16     
 17     /**
 18      * 讲字符串格式的日期 转换成 自定义的 日期格式 返回Date类型
 19      * @param String date
 20      * @param String format
 21      * @return Date 
 22      */
 23     public static Date getDate(String date, String format) {
 24         if(date==null) return null;
 25         java.text.DateFormat formatdatetime = new java.text.SimpleDateFormat(format);
 26         try {
 27             return formatdatetime.parse(date);
 28         } catch (ParseException e) {
 29             e.printStackTrace();
 30         }
 31         return null;
 32     }
 33     /**
 34      * 讲Date类型的日期 转换成 String 字符串形式的
 35      * @param date
 36      * @return String
 37      */
 38     public static String getTimeStr(Date date) {
 39         if(date==null) return null;
 40         java.text.DateFormat formatdatetime = new java.text.SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
 41         return formatdatetime.format(date);
 42     }
 43     /**
 44      * 比较两个 Date类型的 日期 是否是一样的
 45      * @param Date day1
 46      * @param Date day2
 47      * @return boolean
 48      */
 49     public static boolean isSameDay(Date day1, Date day2) {
 50         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 51         String ds1 = dateFormat.format(day1);
 52         String ds2 = dateFormat.format(day2);
 53         if (ds1.equals(ds2)) {
 54             return true;
 55         } else {
 56             return false;
 57         }
 58     }
 59     /**
 60      * 返回开始日期 和 结束日期直接间隔的天数 返回的int类型 单位是 天
 61      * @param Date start
 62      * @param Date end
 63      * @return int 
 64      */
 65     public static int getDays(Date start, Date end)
 66     {
 67         Date normStart = Common.getDateBegin(start);
 68         Date normEnd = Common.getDateBegin(end);
 69         int days = (int) ((normEnd.getTime() - normStart.getTime())/(1000L*24*3600));
 70 
 71         return days;
 72     }
 73     /**
 74      * 讲字符串形式的日期 返回 固定格式 年月日形式的Date形式的 日期
 75      * @param String datestr
 76      * @return Date
 77      */
 78     public static Date getDateStr(String datestr)
 79     {
 80         java.text.DateFormat formatdatetime = new java.text.SimpleDateFormat("yyyy-MM-dd");
 81         try {
 82             return formatdatetime.parse(datestr);
 83         } catch (ParseException e) {
 84             e.printStackTrace();
 85             return null;
 86         }
 87     }
 88     /**
 89      * 返回 String 类型的 年
 90      * @param date
 91      * @return
 92      */
 93     public static String getYearStr(Date date)
 94     {
 95          java.text.DateFormat formatdatetime = new java.text.SimpleDateFormat("yyyy");     
 96          return formatdatetime.format(date);
 97     }
 98     /**
 99      * 返回 int 类型 的年
100      * @param date
101      * @return
102      */
103     public static int getYear(Date date)
104     {
105          java.text.DateFormat formatdatetime = new java.text.SimpleDateFormat("yyyy");     
106          return Integer.parseInt(formatdatetime.format(date));
107     }
108     /**
109      * 返回int类型的月
110      * @param date
111      * @return
112      */
113     public static int getMonth(Date date)
114     {
115          java.text.DateFormat formatdatetime = new java.text.SimpleDateFormat("MM");     
116          return Integer.parseInt(formatdatetime.format(date));
117     }
118     
119     /**
120      * "Tue May 17 10:26:26 CST 2011 由date.toString()得到 像这样的日期如何格式化?":
121         String dstr = "Tue May 17 10:26:26 CST 2011";  
122         SimpleDateFormat formatter = new SimpleDateFormat("EEEE MMM dd HH:mm:ss Z yyyy");  
123         Date d = formatter.parse(dstr); 
124      * @return
125      */
126     public static Date parseDateStr(String dstr){
127         Date d = null;
128         SimpleDateFormat formatter = new SimpleDateFormat("EEEE MMM dd HH:mm:ss Z yyyy");  
129         try {
130             d = formatter.parse(dstr);
131         } catch (ParseException e) {
132             formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
133             try {
134                 d = formatter.parse(dstr);
135             } catch (Exception e2) {
136                 // TODO: handle exception
137             }
138         }
139         return d;
140     }
141     
142     
143     @Test
144     public void test() {
145         String startTime = "2017-11-16";
146         Date time = Common.getDate(startTime+" 00:00:00","yyyy-MM-dd HH:mm:ss");
147         SimpleDateFormat fomat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
148         System.out.println(fomat.format(time));
149         
150         Date date = new Date();
151         Date date1 = new Date();
152         System.out.println(isSameDay(date,date1));
153         
154         System.out.println(parseDateStr(startTime));
155     }
156     
157 }

 

java工具包一:日期处理

标签:begin   else   邮箱   color   format   写代码   tar   ring   sim   

原文地址:http://www.cnblogs.com/NiceCui/p/7846812.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!