DateUtil

简介:
[html] view plain copy
 print ?
  1. import java.text.ParseException;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Calendar;  
  4. import java.util.Date;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7.   
  8. /**  
  9.  * 所有时间按当前2014-12-02计算  
  10.  * @author alone  
  11.  *  
  12.  */  
  13. public class DateUtil {  
  14.     private static String ymdhms = "yyyy-MM-dd HH:mm:ss";    
  15.     private static String ymd = "yyyy-MM-dd";    
  16.     public static SimpleDateFormat ymdSDF = new SimpleDateFormat(ymd);    
  17.     private static String year = "yyyy";    
  18.     private static String month = "MM";    
  19.     private static String day = "dd";    
  20.     public static SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat(ymdhms);    
  21.     public static SimpleDateFormat yearSDF = new SimpleDateFormat(year);    
  22.     public static SimpleDateFormat monthSDF = new SimpleDateFormat(month);    
  23.     public static SimpleDateFormat daySDF = new SimpleDateFormat(day);    
  24.     
  25.     public static SimpleDateFormat yyyyMMddHHmm = new SimpleDateFormat(    
  26.             "yyyy-MM-dd HH:mm");    
  27.     
  28.     public static SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");    
  29.     
  30.     public static SimpleDateFormat yyyyMMddHH_NOT_ = new SimpleDateFormat(    
  31.             "yyyyMMdd");    
  32.     
  33.     public static long DATEMM = 86400L;    
  34.     /**  
  35.      * 获得当前时间  
  36.      * 格式:2014-12-02 10:38:53  
  37.      * @return String  
  38.      */  
  39.     public static String getCurrentTime() {    
  40.         return yyyyMMddHHmmss.format(new Date());    
  41.     }    
  42.     /**  
  43.      * 可以获取昨天的日期  
  44.      * 格式:2014-12-01  
  45.      * @return String  
  46.      */  
  47.     public static String getYesterdayYYYYMMDD() {    
  48.         Date date = new Date(System.currentTimeMillis() - DATEMM * 1000L);    
  49.         String str = yyyyMMdd.format(date);    
  50.         try {    
  51.             date = yyyyMMddHHmmss.parse(str + " 00:00:00");    
  52.             return yyyyMMdd.format(date);    
  53.         } catch (ParseException e) {    
  54.             e.printStackTrace();    
  55.         }    
  56.         return "";    
  57.     }    
  58.     /**  
  59.      * 可以获取后退N天的日期  
  60.      * 格式:传入2 得到2014-11-30  
  61.      * @param backDay  
  62.      * @return String  
  63.      */  
  64.     public String getStrDate(String backDay) {  
  65.         Calendar calendar = Calendar.getInstance() ;  
  66.         calendar.add(Calendar.DATE, Integer.parseInt("-" + backDay));  
  67.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ;  
  68.         String back = sdf.format(calendar.getTime()) ;  
  69.         return back ;  
  70.     }  
  71.     /**  
  72.      *获取当前的年、月、日  
  73.      * @return String  
  74.      */  
  75.     public static String getCurrentYear() {    
  76.         return yearSDF.format(new Date());    
  77.     }   
  78.     public static String getCurrentMonth() {    
  79.         return monthSDF.format(new Date());    
  80.     }   
  81.     public static String getCurrentDay() {    
  82.         return daySDF.format(new Date());    
  83.     }    
  84.     /**  
  85.      * 获取年月日 也就是当前时间  
  86.      * 格式:2014-12-02  
  87.      * @return String  
  88.      */  
  89.     public static String getCurrentymd() {    
  90.         return ymdSDF.format(new Date());    
  91.     }    
  92.     /**  
  93.      * 获取今天0点开始的秒数  
  94.      * @return long  
  95.      */  
  96.     public static long getTimeNumberToday() {    
  97.         Date date = new Date();    
  98.         String str = yyyyMMdd.format(date);    
  99.         try {    
  100.             date = yyyyMMdd.parse(str);    
  101.             return date.getTime() / 1000L;    
  102.         } catch (ParseException e) {    
  103.             e.printStackTrace();    
  104.         }    
  105.         return 0L;    
  106.     }    
  107.     /**  
  108.      * 获取今天的日期  
  109.      * 格式:20141202  
  110.      * @return String  
  111.      */  
  112.     public static String getTodateString() {    
  113.         String str = yyyyMMddHH_NOT_.format(new Date());    
  114.         return str;    
  115.     }   
  116.     /**  
  117.      * 获取昨天的日期  
  118.      * 格式:20141201  
  119.      * @return String  
  120.      */  
  121.     public static String getYesterdayString() {    
  122.         Date date = new Date(System.currentTimeMillis() - DATEMM * 1000L);    
  123.         String str = yyyyMMddHH_NOT_.format(date);    
  124.         return str;    
  125.     }    
  126.     /**    
  127.      * 获得昨天零点    
  128.      *     
  129.      * @return Date  
  130.      */    
  131.     public static Date getYesterDayZeroHour() {    
  132.         Calendar cal = Calendar.getInstance();    
  133.         cal.add(Calendar.DATE, -1);    
  134.         cal.set(Calendar.SECOND, 0);    
  135.         cal.set(Calendar.MINUTE, 0);    
  136.         cal.set(Calendar.HOUR, 0);    
  137.         return cal.getTime();    
  138.     }    
  139.     /**    
  140.      * 把long型日期转String ;---OK    
  141.      *     
  142.      * @param date    
  143.      *            long型日期;    
  144.      * @param format    
  145.      *            日期格式;    
  146.      * @return    
  147.      */    
  148.     public static String longToString(long date, String format) {    
  149.         SimpleDateFormat sdf = new SimpleDateFormat(format);    
  150.         // 前面的lSysTime是秒数,先乘1000得到毫秒数,再转为java.util.Date类型    
  151.         java.util.Date dt2 = new Date(date * 1000L);    
  152.         String sDateTime = sdf.format(dt2); // 得到精确到秒的表示:08/31/2006 21:08:00    
  153.         return sDateTime;    
  154.     }    
  155.     
  156.     /**    
  157.      * 获得今天零点    
  158.      *     
  159.      * @return Date  
  160.      */    
  161.     public static Date getTodayZeroHour() {    
  162.         Calendar cal = Calendar.getInstance();    
  163.         cal.set(Calendar.SECOND, 0);    
  164.         cal.set(Calendar.MINUTE, 0);    
  165.         cal.set(Calendar.HOUR, 0);    
  166.         return cal.getTime();    
  167.     }   
  168.     /**    
  169.      * 获得昨天23时59分59秒    
  170.      *     
  171.      * @return    
  172.      */    
  173.     public static Date getYesterDay24Hour() {    
  174.         Calendar cal = Calendar.getInstance();    
  175.         cal.add(Calendar.DATE, -1);    
  176.         cal.set(Calendar.SECOND, 59);    
  177.         cal.set(Calendar.MINUTE, 59);    
  178.         cal.set(Calendar.HOUR, 23);    
  179.         return cal.getTime();    
  180.     }    
  181.     /**    
  182.      * String To Date ---OK    
  183.      *     
  184.      * @param date    
  185.      *            待转换的字符串型日期;    
  186.      * @param format    
  187.      *            转化的日期格式    
  188.      * @return 返回该字符串的日期型数据;    
  189.      */    
  190.     public static Date stringToDate(String date, String format) {    
  191.         SimpleDateFormat sdf = new SimpleDateFormat(format);    
  192.         try {    
  193.             return sdf.parse(date);    
  194.         } catch (ParseException e) {    
  195.             return null;    
  196.         }    
  197.     }    
  198.     
  199.     /**    
  200.      * 获得指定日期所在的自然周的第一天,即周日    
  201.      *     
  202.      * @param date    
  203.      *            日期    
  204.      * @return 自然周的第一天    
  205.      */    
  206.     public static Date getStartDayOfWeek(Date date) {    
  207.         Calendar c = Calendar.getInstance();    
  208.         c.setTime(date);    
  209.         c.set(Calendar.DAY_OF_WEEK, 1);    
  210.         date = c.getTime();    
  211.         return date;    
  212.     }    
  213.     
  214.     /**    
  215.      * 获得指定日期所在的自然周的最后一天,即周六    
  216.      *     
  217.      * @param date    
  218.      * @return    
  219.      */    
  220.     public static Date getLastDayOfWeek(Date date) {    
  221.         Calendar c = Calendar.getInstance();    
  222.         c.setTime(date);    
  223.         c.set(Calendar.DAY_OF_WEEK, 7);    
  224.         date = c.getTime();    
  225.         return date;    
  226.     }    
  227.     
  228.     /**    
  229.      * 获得指定日期所在当月第一天    
  230.      *     
  231.      * @param date    
  232.      * @return    
  233.      */    
  234.     public static Date getStartDayOfMonth(Date date) {    
  235.         Calendar c = Calendar.getInstance();    
  236.         c.setTime(date);    
  237.         c.set(Calendar.DAY_OF_MONTH, 1);    
  238.         date = c.getTime();    
  239.         return date;    
  240.     }    
  241.     
  242.     /**    
  243.      * 获得指定日期所在当月最后一天    
  244.      *     
  245.      * @param date    
  246.      * @return    
  247.      */    
  248.     public static Date getLastDayOfMonth(Date date) {    
  249.         Calendar c = Calendar.getInstance();    
  250.         c.setTime(date);    
  251.         c.set(Calendar.DATE, 1);    
  252.         c.add(Calendar.MONTH, 1);    
  253.         c.add(Calendar.DATE, -1);    
  254.         date = c.getTime();    
  255.         return date;    
  256.     }    
  257.     
  258.     /**    
  259.      * 获得指定日期的下一个月的第一天    
  260.      *     
  261.      * @param date    
  262.      * @return    
  263.      */    
  264.     public static Date getStartDayOfNextMonth(Date date) {    
  265.         Calendar c = Calendar.getInstance();    
  266.         c.setTime(date);    
  267.         c.add(Calendar.MONTH, 1);    
  268.         c.set(Calendar.DAY_OF_MONTH, 1);    
  269.         date = c.getTime();    
  270.         return date;    
  271.     }    
  272.     
  273.     /**    
  274.      * 获得指定日期的下一个月的最后一天    
  275.      *     
  276.      * @param date    
  277.      * @return    
  278.      */    
  279.     public static Date getLastDayOfNextMonth(Date date) {    
  280.         Calendar c = Calendar.getInstance();    
  281.         c.setTime(date);    
  282.         c.set(Calendar.DATE, 1);    
  283.         c.add(Calendar.MONTH, 2);    
  284.         c.add(Calendar.DATE, -1);    
  285.         date = c.getTime();    
  286.         return date;    
  287.     }    
  288.     
  289.     /**    
  290.      *     
  291.      * 求某一个时间向前多少秒的时间(currentTimeToBefer)---OK    
  292.      *     
  293.      * @param givedTime    
  294.      *            给定的时间    
  295.      * @param interval    
  296.      *            间隔时间的毫秒数;计算方式 :n(天)*24(小时)*60(分钟)*60(秒)(类型)    
  297.      * @param format_Date_Sign    
  298.      *            输出日期的格式;如yyyy-MM-dd、yyyyMMdd等;    
  299.      */    
  300.     public static String givedTimeToBefer(String givedTime, long interval,    
  301.             String format_Date_Sign) {    
  302.         String tomorrow = null;    
  303.         try {    
  304.             SimpleDateFormat sdf = new SimpleDateFormat(format_Date_Sign);    
  305.             Date gDate = sdf.parse(givedTime);    
  306.             long current = gDate.getTime(); // 将Calendar表示的时间转换成毫秒    
  307.             long beforeOrAfter = current - interval * 1000L; // 将Calendar表示的时间转换成毫秒    
  308.             Date date = new Date(beforeOrAfter); // 用timeTwo作参数构造date2    
  309.             tomorrow = new SimpleDateFormat(format_Date_Sign).format(date);    
  310.         } catch (ParseException e) {    
  311.             e.printStackTrace();    
  312.         }    
  313.         return tomorrow;    
  314.     }    
  315.     /**    
  316.      * 把String 日期转换成long型日期;---OK    
  317.      *     
  318.      * @param date    
  319.      *            String 型日期;    
  320.      * @param format    
  321.      *            日期格式;    
  322.      * @return    
  323.      */    
  324.     public static long stringToLong(String date, String format) {    
  325.         SimpleDateFormat sdf = new SimpleDateFormat(format);    
  326.         Date dt2 = null;    
  327.         long lTime = 0;    
  328.         try {    
  329.             dt2 = sdf.parse(date);    
  330.             // 继续转换得到秒数的long型    
  331.             lTime = dt2.getTime() / 1000;    
  332.         } catch (ParseException e) {    
  333.             e.printStackTrace();    
  334.         }    
  335.     
  336.         return lTime;    
  337.     }    
  338.     
  339.     /**    
  340.      * 得到二个日期间的间隔日期;    
  341.      *     
  342.      * @param endTime    
  343.      *            结束时间    
  344.      * @param beginTime    
  345.      *            开始时间    
  346.      * @param isEndTime    
  347.      *            是否包含结束日期;    
  348.      * @return    
  349.      */    
  350.     public static Map<String, String> getTwoDay(String endTime,    
  351.             String beginTime, boolean isEndTime) {    
  352.         Map<String, String> result = new HashMap<String, String>();    
  353.         if ((endTime == null || endTime.equals("") || (beginTime == null || beginTime    
  354.                 .equals(""))))    
  355.             return null;    
  356.         try {    
  357.             java.util.Date date = ymdSDF.parse(endTime);    
  358.             endTime = ymdSDF.format(date);    
  359.             java.util.Date mydate = ymdSDF.parse(beginTime);    
  360.             long day = (date.getTime() - mydate.getTime())    
  361.                     / (24 * 60 * 60 * 1000);    
  362.             result = getDate(endTime, Integer.parseInt(day + ""), isEndTime);    
  363.         } catch (Exception e) {    
  364.         }    
  365.         return result;    
  366.     }    
  367.     
  368.     /**    
  369.      * 得到二个日期间的间隔日期;    
  370.      *     
  371.      * @param endTime    
  372.      *            结束时间    
  373.      * @param beginTime    
  374.      *            开始时间    
  375.      * @param isEndTime    
  376.      *            是否包含结束日期;    
  377.      * @return    
  378.      */    
  379.     public static Integer getTwoDayInterval(String endTime, String beginTime,    
  380.             boolean isEndTime) {    
  381.         if ((endTime == null || endTime.equals("") || (beginTime == null || beginTime    
  382.                 .equals(""))))    
  383.             return 0;    
  384.         long day = 0l;    
  385.         try {    
  386.             java.util.Date date = ymdSDF.parse(endTime);    
  387.             endTime = ymdSDF.format(date);    
  388.             java.util.Date mydate = ymdSDF.parse(beginTime);    
  389.             day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);    
  390.         } catch (Exception e) {    
  391.             return 0 ;    
  392.         }    
  393.         return Integer.parseInt(day + "");    
  394.     }    
  395.     
  396.     /**    
  397.      * 根据结束时间以及间隔差值,求符合要求的日期集合;    
  398.      *     
  399.      * @param endTime    
  400.      * @param interval    
  401.      * @param isEndTime    
  402.      * @return    
  403.      */    
  404.     public static Map<String, String> getDate(String endTime, Integer interval,    
  405.             boolean isEndTime) {    
  406.         Map<String, String> result = new HashMap<String, String>();    
  407.         if (interval == 0 || isEndTime) {    
  408.             if (isEndTime)    
  409.                 result.put(endTime, endTime);    
  410.         }    
  411.         if (interval > 0) {    
  412.             int begin = 0;    
  413.             for (int i = begin; i < interval; i++) {    
  414.                 endTime = givedTimeToBefer(endTime, DATEMM, ymd);    
  415.                 result.put(endTime, endTime);    
  416.             }    
  417.         }    
  418.         return result;    
  419.     }    
  420. }  
目录
相关文章
|
7月前
localdatetime 比较相等
localdatetime 比较相等
171 2
|
7天前
|
Java
java日期工具类(DateUtil)
java日期工具类(DateUtil)
|
17天前
|
Java Unix API
LocalDate、 LocalTime、 LocalDateTime以及ZonedDate、 ZonedTime、 ZonedDateTime相关操作
LocalDate、 LocalTime、 LocalDateTime以及ZonedDate、 ZonedTime、 ZonedDateTime相关操作
|
2月前
DateUtil工具类
DateUtil工具类
12 0
|
11月前
|
Java
Java:Date和LocalDateTime获取当前时间
Java:Date和LocalDateTime获取当前时间
188 0
|
Java 数据库连接 mybatis
LocalDateTime、Date时间工具类
LocalDateTime、Date时间工具类
199 0
DateTimeFormatter 和 LocalDateTime 日期转换
将字符串转换为Date类型,Date转换为字符串。
2hutool源码分析:DateUtil(时间工具类)-常用的时间类型Date,DateTime,Calendar和TemporalAccessor(LocalDateTime)转换
2hutool源码分析:DateUtil(时间工具类)-常用的时间类型Date,DateTime,Calendar和TemporalAccessor(LocalDateTime)转换
152 0
2hutool源码分析:DateUtil(时间工具类)-常用的时间类型Date,DateTime,Calendar和TemporalAccessor(LocalDateTime)转换
|
Java API
4hutool实战:DateUtil-格式化时间
4hutool实战:DateUtil-格式化时间
336 0
4hutool实战:DateUtil-格式化时间
|
Java
7hutool实战:DateUtil(时间工具类)-日期计算
7hutool实战:DateUtil(时间工具类)-日期计算
1970 0
7hutool实战:DateUtil(时间工具类)-日期计算