码迷,mamicode.com
首页 > 其他好文 > 详细

常用日期函数

时间:2016-06-02 18:19:07      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

public class DateUtil {
    private static Logger logger = Logger.getLogger(DateUtil.class);
    
    public static String getCurrDate() {
        return DateFormatUtils.format(new Date(), "yyyy-MM-dd");
    }

    public static String getCurrTime() {
        return DateFormatUtils.format(new Date(), "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());
    }
    
    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转string
     * 
     * @param date
     * @return
     */
    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;
        }
    }

    /**
     * string转date
     * 
     * @param str
     * @return
     * @throws ParseException
     */
    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;
    }

    /**
     * 得到月份月初 格式为:yyyy-MM-dd HH:mm:ss
     * @param str
     * @return
     */
    public static String getMonthStart(String str){
        return str+"-01 00:00:00";
    }
    
    
    /**
     * 得到月份月底 格式为:yyyy-MM-dd HH:mm:ss
     * @param str (yyyy-MM)
     * @return
     */
    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";

    }
    
    
    /**
     * 功能:得到当前月份月初 格式为:xxxx-yy-zz (eg: 2007-12-01)
     * @return String
     * @author pure
    */
    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";
    }

    
    /**
     * 功能:得到当前月份月底 格式为:xxxx-yy-zz (eg: 2007-12-31)
     * @return String
     * @author pure
    **/
    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;
    }
    
    
    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;
    }
}

 

常用日期函数

标签:

原文地址:http://www.cnblogs.com/Mr-kevin/p/5553670.html

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