zoukankan      html  css  js  c++  java
  • 常用日期处理方法工具类

    此文章是基于  搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台

    功能:日期的获取与转换

      DateUtil.java

    package com.ims.common;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.commons.lang3.StringUtils;
    import org.apache.commons.lang3.time.DateFormatUtils;
    import org.apache.log4j.Logger;
    
    /**
     * 常用日期处理方法工具类
     */
    public class DateUtil {
        private static Logger logger = Logger.getLogger(DateUtil.class);
        
        /**
         * 取得当前日期字符串
         * @return 格式:yyyy-MM-dd 
         */
        public static String getCurrDate() {
            return DateFormatUtils.format(new Date(), "yyyy-MM-dd");
        }
    
        /**
         * 取得当前时间字符串
         * @return 格式:yyyy-MM-dd HH:mm:ss
         */
        public static String getCurrTime() {
            return DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        }
        
        /**
         * 取得离现在n天的时间字符串
         * @param afterDay 可往前(负数)、往后(正数)n天
         * @return 格式:yyyy-MM-dd HH:mm:ss
         */
        public static String getAfterTime(int afterDay){
            Calendar calendar = new GregorianCalendar(); 
            calendar.setTime(new Date()); 
            calendar.add(Calendar.DATE,afterDay);
            return dateToString(calendar.getTime());
        }
        
        /**
         * 取得当前年份
         * @return 格式:yyyy
         */
        public static String getCurrYear() {
            return DateFormatUtils.format(System.currentTimeMillis(), "yyyy");
        }
    
        /**
         * 将字符串List转化为字符串,以分隔符间隔.
         * @param list 需要处理的List.
         * @param separator 分隔符.
         * @return 
         */
        public static String toString(List<String> list, String separator) {
            StringBuffer stringBuffer = new StringBuffer();
            for (String str : list) {
                stringBuffer.append(separator + str);
            }
            stringBuffer.deleteCharAt(0);
            return stringBuffer.toString();
        }
    
        /**
         * 日期date转换为字符串
         * @param date 日期
         * @return 格式:yyyy-MM-dd HH:mm:ss
         */
        public static String dateToString(Date date) {
            if (date != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return sdf.format(date);
            } else {
                return null;
            }
        }
    
        /**
         * 日期字符串转换为日期date
         * @param str 日期字符串
         * @return 
         */
        public static Date stringToDate(String str) {
            try {
                if (str != null) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");                
                    return sdf.parse(str);                
                }
            } catch (ParseException e) {
                logger.error("日期转换出错");            
            }
            return null;
        }
    
        /**
         * 得到月份月初
         * @param str 格式:yyyy-MM
         * @return 格式:yyyy-MM-dd HH:mm:ss
         */
        public static String getMonthStart(String str){
            return str+"-01 00:00:00";
        }
            
        /**
         * 得到月份月底
         * @param str 格式:yyyy-MM
         * @return 格式:yyyy-MM-dd HH:mm:ss
         */
        public static String getMonthEnd(String str) {
            String strZ = null;
            int x = Integer.valueOf(str.split("-")[0]);
            int y = Integer.valueOf(str.split("-")[1]);
            boolean leap = false;
            if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {
                strZ ="31";
            }
            if (y == 4 || y == 6 || y == 9 || y == 11) {
                strZ ="30";
            }
            if (y == 2) {
                leap = leapYear(x);
                if (leap) {
                    strZ ="29";
                }else {
                    strZ ="28";
                }
            }
            return str+"-"+strZ+" 23:59:59";
    
        }
        
        
        /**
         * 得到当前月份月初
         * @return 格式:yyyy-MM-dd (eg: 2007-12-01)
         */
        public static String thisMonthStart() {
            String strY = null;
            int x = Calendar.getInstance().get(Calendar.YEAR);
            int y = Calendar.getInstance().get(Calendar.MONTH) + 1;
            strY = y >= 10 ? String.valueOf(y) : ("0"+ y);return x +"-"+ strY +"-01";
        }
    
        
        /**
         * 得到当前月份月底
         * @return 格式:yyyy-MM-dd (eg: 2007-12-31)
         */
        public static String thisMonthEnd() {
            String strY = null;
            String strZ = null;
            boolean leap = false;
    
            int x = Calendar.getInstance().get(Calendar.YEAR);
            int y = Calendar.getInstance().get(Calendar.MONTH) + 1;
            if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {
                strZ ="31";
            }
            if (y == 4 || y == 6 || y == 9 || y == 11) {
                strZ ="30";
            }
            if (y == 2) {
                leap = leapYear(x);
                if (leap) {
                    strZ ="29";
                }else {
                    strZ ="28";
                }
            }
            strY = y >= 10 ? String.valueOf(y) : ("0"+ y);
            return x +"-"+ strY +"-"+ strZ;
        }
        
        
        /**
         * 判断输入年份是否为闰年
         * @param year 年份
         * @return
         */
        public static boolean leapYear(int year) {
            boolean leap;
            if (year % 4 == 0) {
                if (year % 100 == 0) {
                    if (year % 400 == 0){
                        leap = true;
                    }else{
                        leap = false;
                    }
                }else{
                    leap = true;
                }
            }else{
                leap = false;
            }
            return leap;
        }
        
        /**
         * 取得开始与结束时间字符串
         * @param date 日期对象,如:(2005-12-01到2005-12-30)或者(2005-12-01)
         * @return 开始时间key:startDate,value:如2005-12-01 00:00:00
         *                结束时间key:endDate,value:如2005-12-30 23:59:59
         */
        public static Map<String, String> getStartEndDate(Object date){
            Map<String, String> result = new HashMap<String, String>();
            if(date!=null && StringUtils.isNotBlank(date.toString())){
                String dateStr = date.toString();
                if(dateStr.indexOf("到")!=-1){
                    if(StringUtils.isNotBlank(dateStr.substring(0, dateStr.indexOf("到")))){
                        result.put("startDate", dateStr.substring(0, dateStr.indexOf("到")).trim()+" 00:00:00");
                    }
                    if(StringUtils.isNotBlank(dateStr.substring(dateStr.indexOf("到")+1, dateStr.length()))){
                        result.put("endDate", dateStr.substring(dateStr.indexOf("到")+1, dateStr.length()).trim()+" 23:59:59");
                    }        
                }else{
                    result.put("startDate", dateStr.trim()+" 00:00:00");
                    result.put("endDate", dateStr.trim()+" 23:59:59");
                }
            }
            return result;
        }
    }
    View Code
  • 相关阅读:
    Git push 出错 [The remote end hung up unexpectedly]
    [Git高级教程(二)] 远程仓库版本回退方法
    git分支与版本管理、版本回退、冲突解决记录
    上传本地代码到gitHub过程详解
    如何用git将项目代码上传到github
    Git pull 强制覆盖本地文件
    Git忽略规则.gitignore梳理
    composer本地安装文档
    服务器通过微信公众号Token验证测试的代码(Python版)
    转载自lanceyan: 一致性hash和solr千万级数据分布式搜索引擎中的应用
  • 原文地址:https://www.cnblogs.com/Mr-kevin/p/5553670.html
Copyright © 2011-2022 走看看