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

    package com.manage.utils;
    
    import org.apache.commons.lang.StringUtils;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class DateUtil {
        private static String defaultDatePattern = "yyyy-MM-dd ";
    
        /**
         * 获得默认的 date pattern
         */
        public static String getDatePattern()
        {
            return defaultDatePattern;
        }
    
        /**
         * 返回预设Format的当前日期字符串
         */
        public static String getToday()
        {
            Date today = new Date();
            return format(today);
        }
    
        /**
         * 使用预设Format格式化Date成字符串
         */
        public static String format(Date date)
        {
            return date == null ? " " : format(date, getDatePattern());
        }
    
        /**
         * 使用参数Format格式化Date成字符串
         */
        public static String format(Date date, String pattern)
        {
            return date == null ? " " : new SimpleDateFormat(pattern).format(date);
        }
    
        /**
         * 使用预设格式将字符串转为Date
         */
        public static Date parse(String strDate) throws ParseException
        {
            return StringUtils.isBlank(strDate) ? null : parse(strDate,
                    getDatePattern());
        }
    
        /**
         * 使用参数Format将字符串转为Date
         */
        public static Date parse(String strDate, String pattern)
                throws ParseException
        {
            return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat(
                    pattern).parse(strDate);
        }
    
        /**
         * 在日期上增加数个整月
         */
        public static Date addMonth(Date date, int n)
        {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.MONTH, n);
            return cal.getTime();
        }
    
        public static String getLastDayOfMonth(String year, String month)
        {
            Calendar cal = Calendar.getInstance();
            //
            cal.set(Calendar.YEAR, Integer.parseInt(year));
            // 月,因为Calendar里的月是从0开始,所以要-1
            // cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
            // 日,设为一号
            cal.set(Calendar.DATE, 1);
            // 月份加一,得到下个月的一号
            cal.add(Calendar.MONTH, 1);
            // 下一个月减一为本月最后一天
            cal.add(Calendar.DATE, -1);
            return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));// 获得月末是几号
        }
    
        public static Date getDate(String year, String month, String day)
                throws ParseException
        {
            String result = year + "- "
                    + (month.length() == 1 ? ("0 " + month) : month) + "- "
                    + (day.length() == 1 ? ("0 " + day) : day);
            return parse(result);
        }
    }
  • 相关阅读:
    iOS 应用内付费(IAP)开发步骤
    国内银行CNAPS CODE 查询 苹果开发者,应用内购,需要填写税务相关信息必须的
    untiy 插件工具: 游戏中 策划数据Excel 导出到项目中
    大陆 Google play 开发者注册(2016)
    unity UGUI动态字体显示模糊
    Vue--webpack实时重新加载
    Vue--webpack打包css、image资源
    Vue--webpack打包js文件
    Vue--axios
    Vue--生命周期
  • 原文地址:https://www.cnblogs.com/git-niu/p/8796580.html
Copyright © 2011-2022 走看看