zoukankan      html  css  js  c++  java
  • DateUtil(日期工具类)

    /**
         * 把日期对象根据生成指定格式的字符串
         * @param date
         * @param format
         * @return
         */
        public static String formatDate(Date date,String format){
            String result="";
            SimpleDateFormat sdf=new SimpleDateFormat(format);
            if(date!=null){
                result=sdf.format(date);
            }
            return result;
        }
        
        /**
         * 把日期字符串生成指定格式的日期对象
         * @param str
         * @param format
         * @return
         * @throws Exception
         */
        public static Date formatString(String str,String format) throws Exception{
            if(StringUtil.isEmpty(str)){
                return null;
            }
            SimpleDateFormat sdf=new SimpleDateFormat(format);
            return sdf.parse(str);
        }
        
        /**
         * 生成当前年月日字符串
         * @return
         * @throws Exception
         */
        public static String getCurrentDateStr()throws Exception{
            Date date=new Date();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
            return sdf.format(date);
        }
        
        /**
         * 获取指定范围内的日期集合
         * @param before
         * @param end
         * @return
         * @throws Exception
         */
        public static List<String> getRangeDates(String before,String end)throws Exception{
            List<String> datas=new ArrayList<String>();
            Calendar cb = Calendar.getInstance();
            Calendar ce = Calendar.getInstance();
            cb.setTime(formatString(before,"yyyy-MM-dd"));
            ce.setTime(formatString(end,"yyyy-MM-dd"));
            datas.add(formatDate(cb.getTime(),"yyyy-MM-dd"));
            while(cb.before(ce)){
                cb.add(Calendar.DAY_OF_MONTH, 1);
                datas.add(formatDate(cb.getTime(),"yyyy-MM-dd"));
            }
            return datas;
        }
        
        /**
         * 获取指定范围内的月份集合
         * @param before
         * @param end
         * @return
         * @throws Exception
         */
        public static List<String> getRangeMonth(String before,String end)throws Exception{
            List<String> months=new ArrayList<String>();
            Calendar cb = Calendar.getInstance();
            Calendar ce = Calendar.getInstance();
            cb.setTime(formatString(before,"yyyy-MM"));
            ce.setTime(formatString(end,"yyyy-MM"));
            months.add(formatDate(cb.getTime(),"yyyy-MM"));
            while(cb.before(ce)){
                cb.add(Calendar.MONTH, 1);
                months.add(formatDate(cb.getTime(),"yyyy-MM"));
            }
            return months;
        }
        
        
        public static void main(String[] args) throws Exception{
            /*List<String> datas=getRangeDatas("2017-10-28","2017-11-02");
            for(String data:datas){
                System.out.println(data);
            }*/
            List<String> months=getRangeMonth("2017-09","2018-12");
            for(String month:months){
                System.out.println(month);
            }
        }
  • 相关阅读:
    asp.net Ajax调用Aspx后台方法
    JS 通过字符串取得对应对象
    nginx js、css、图片 及 一些静态文件中出现 http://upstreamname:port 导致部分网页样式显示不正常
    jexus手动跨域设置
    HTTP Error 400. The request hostname is invalid
    at MySql.Data.MySqlClient.MySqlStream.ReadPacket 或 FUNCTION account.AddMinutes does not exist
    sql xml 入门
    Jexus .Net at System.Net.Sockets.Socket.Connect (System.Net.IPAddress[] addresses, System.Int32 port)
    关于SQL SERVER中的FLOAT转换为VARCHAR
    JS倒计时
  • 原文地址:https://www.cnblogs.com/429lirui/p/13694724.html
Copyright © 2011-2022 走看看