• java工具包一:日期处理


    作者:NiceCui

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

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

      1 import java.text.ParseException;
      2 
      3 import java.text.SimpleDateFormat;
      4 import java.util.Date;
      5 
      6 import org.junit.Test;
      7 
      8 /**
      9  * 日期处理工具包
     10  * @author NiceCui
     11  * @date 2017-11-14
     12  *
     13  */
     14 public class DateTool {
     15     
     16     /**
     17      * 讲字符串格式的日期 转换成 自定义的 日期格式 返回Date类型
     18      * 
     19      * @param String
     20      *            date
     21      * @param String
     22      *            format
     23      * @return Date
     24      */
     25     public static Date getDate(String date, String format) {
     26         if (date == null)
     27             return null;
     28         java.text.DateFormat formatdatetime = new java.text.SimpleDateFormat(format);
     29         try {
     30             return formatdatetime.parse(date);
     31         } catch (ParseException e) {
     32             e.printStackTrace();
     33         }
     34         return null;
     35     }
     36 
     37     /**
     38      * 讲Date类型的日期 转换成 String 字符串形式的
     39      * 
     40      * @param date
     41      * @return String
     42      */
     43     public static String getTimeStr(Date date) {
     44         if (date == null)
     45             return null;
     46         java.text.DateFormat formatdatetime = new java.text.SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
     47         return formatdatetime.format(date);
     48     }
     49 
     50     /**
     51      * 比较两个 Date类型的 日期 是否是一样的
     52      * 
     53      * @param Date
     54      *            day1
     55      * @param Date
     56      *            day2
     57      * @return boolean
     58      */
     59     public static boolean isSameDay(Date day1, Date day2) {
     60         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
     61         String ds1 = dateFormat.format(day1);
     62         String ds2 = dateFormat.format(day2);
     63         if (ds1.equals(ds2)) {
     64             return true;
     65         } else {
     66             return false;
     67         }
     68     }
     69 
     70     /**
     71      * 返回开始日期 和 结束日期直接间隔的天数 返回的int类型 单位是 天
     72      * 
     73      * @param Date
     74      *            start
     75      * @param Date
     76      *            end
     77      * @return int
     78      */
     79     public static int getDays(Date start, Date end) {
     80         Date normStart = DateTool.getDateBegin(start);
     81         Date normEnd = DateTool.getDateBegin(end);
     82         int days = (int) ((normEnd.getTime() - normStart.getTime()) / (1000L * 24 * 3600));
     83 
     84         return days;
     85     }
     86 
     87     /**
     88      * 获得一天的第1s
     89      * 
     90      * @param date
     91      * @return
     92      */
     93     public static Date getDateBegin(Date date) {
     94         Date dateBegin = DateTool.getDate(DateTool.getDateStr(date), "yyyy-MM-dd");
     95         return dateBegin;
     96     }
     97     public static String getDateStr(Date date) {
     98         if(date==null) 
     99         return null;
    100         java.text.DateFormat formatdatetime = new java.text.SimpleDateFormat("yyyy-MM-dd");
    101         return formatdatetime.format(date);
    102     }
    103     public static String getDateStr(Date date, String format) {
    104         if(date==null) return null;
    105         java.text.DateFormat formatdatetime = new java.text.SimpleDateFormat(format);
    106         return formatdatetime.format(date);
    107     }
    108 
    109     /**
    110      * 获得一天的最后1s
    111      * 
    112      * @param date
    113      * @return
    114      */
    115     public static Date getDateEnd(Date date) {
    116         Date endDate = DateTool.getDate(DateTool.getDateStr(new Date()) + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
    117         return endDate;
    118     }
    119 
    120     /**
    121      * 讲字符串形式的日期 返回 固定格式 年月日形式的Date形式的 日期
    122      * 
    123      * @param String
    124      *            datestr
    125      * @return Date
    126      */
    127     public static Date getDateStr(String datestr) {
    128         java.text.DateFormat formatdatetime = new java.text.SimpleDateFormat("yyyy-MM-dd");
    129         try {
    130             return formatdatetime.parse(datestr);
    131         } catch (ParseException e) {
    132             e.printStackTrace();
    133             return null;
    134         }
    135     }
    136 
    137     /**
    138      * 返回 String 类型的 年
    139      * 
    140      * @param date
    141      * @return
    142      */
    143     public static String getYearStr(Date date) {
    144         java.text.DateFormat formatdatetime = new java.text.SimpleDateFormat("yyyy");
    145         return formatdatetime.format(date);
    146     }
    147 
    148     /**
    149      * 返回 int 类型 的年
    150      * 
    151      * @param date
    152      * @return
    153      */
    154     public static int getYear(Date date) {
    155         java.text.DateFormat formatdatetime = new java.text.SimpleDateFormat("yyyy");
    156         return Integer.parseInt(formatdatetime.format(date));
    157     }
    158 
    159     /**
    160      * 返回int类型的月
    161      * 
    162      * @param date
    163      * @return
    164      */
    165     public static int getMonth(Date date) {
    166         java.text.DateFormat formatdatetime = new java.text.SimpleDateFormat("MM");
    167         return Integer.parseInt(formatdatetime.format(date));
    168     }
    169 
    170     /**
    171      * "Tue May 17 10:26:26 CST 2011 由date.toString()得到 像这样的日期如何格式化?": String dstr =
    172      * "Tue May 17 10:26:26 CST 2011"; SimpleDateFormat formatter = new
    173      * SimpleDateFormat("EEEE MMM dd HH:mm:ss Z yyyy"); Date d =
    174      * formatter.parse(dstr);
    175      * 
    176      * @return
    177      */
    178     public static Date parseDateStr(String dstr) {
    179         Date d = null;
    180         SimpleDateFormat formatter = new SimpleDateFormat("EEEE MMM dd HH:mm:ss Z yyyy");
    181         try {
    182             d = formatter.parse(dstr);
    183         } catch (ParseException e) {
    184             formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    185             try {
    186                 d = formatter.parse(dstr);
    187             } catch (Exception e2) {
    188                 // TODO: handle exception
    189             }
    190         }
    191         return d;
    192     }
    193     
    194     @Test
    195     public void test() {
    196 
    197         String startTime = "2017-11-16";
    198         // Date time = Common.getDate(startTime+" 00:00:00","yyyy-MM-dd HH:mm:ss");
    199         // SimpleDateFormat fomat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    200         // System.out.println(fomat.format(time));
    201 
    202         // Date date = new Date();
    203         // Date date1 = new Date();
    204         // System.out.println(isSameDay(date,date1));
    205 
    206         System.out.println(parseDateStr(startTime));
    207     }
    208 
    209 }
  • 相关阅读:
    6、scala面向对象-对象
    C# App.config配置文件的讲解
    abstract、override、new、virtual、sealed使用和示例
    C# 枚举的使用
    深入浅出面向对象分析与设计
    数据契约(DataContract)的作用
    C# 启动停止SQLServer数据库服务器
    C# 定时器计划任务
    C# 程序只能执行一次
    WPF dataGrid中的check的改变事件
  • 原文地址:https://www.cnblogs.com/NiceCui/p/7846812.html
Copyright © 2020-2023  润新知