为了账号安全,请及时绑定邮箱和手机立即绑定

DateUtil

标签:
Java

/** * @Title: DateUtil.java * @Package com.niewj.util * @Description: 日期工具 * @author niewj * @date 2016年8月22日 下午1:48:11 * @version V1.0 */import java.text.DecimalFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.*;/** * @author niewj * @ClassName: com.niewj.util.DateUtil * @Description: 日期工具类 * @date 2016年8月22日 下午1:48:11 */public class DateUtil {    private static final String dtFormat = "yyyy-MM-dd";    /**     * 计算当前月的天数     *     * @Title: currentMonthDays     * @Description: 计算当前月的天数     * @Since: 2016年8月22日 下午2:28:25     * @Author: niewj     */    public static int currentMonthDays() {        Calendar cal = Calendar.getInstance();        cal.setTime(new Date());        return cal.getActualMaximum(Calendar.DATE);    }    /**     * 计算回溯count个月的月份列表;     * 比如当前为9月,count=12, 会返回:["201608",........"201509"]     *     * @param count 月份数-往前推     * @return     * @throws     * @Title: getMonthsDateList     * @Description: 计算当前月的天数     * @Since: 2016年9月21日 下午2:28:25     * @Author: niewj     */    public static List<String> getMonthsDateList(int count) {        List<String> list = new ArrayList<String>();        Calendar cal = Calendar.getInstance();        int year = cal.get(Calendar.YEAR);        // #1. 从上一个月开始        int month = cal.get(Calendar.MONTH); // JANUARY is 0[1月 == 0],所以不用减一        String monthString = null;        for (int i = 1; i <= count; i++) {            // #2. 如果月份小于1 [年减一 && 定位到12月]            if (month < 1) {                year--;                month = 12;            }            // # 月份格式为2位的.            if (month < 10) {                list.add(year + "0" + month);            } else {                list.add(year + "" + month);            }            // # 3. 计算上一个月的;            month--;        }        return list;    }    /**     * 计算N个月后的全月日期表示列表; 参数接受负数,表示几个月前     * 比如当前为9月,会返回:["2016-09-01",........"2016-09-30"]     *     * @param nMonthsLater     * @return     * @throws     * @Title: currentMonthDates     * @Description: 计算当前月的天数     * @Since: 2016年9月21日 下午2:28:25     * @Author: niewj     */    public static List<String> getAllMonthDateList(int nMonthsLater) {        final String format = "yyyy-MM-";        Calendar cal = Calendar.getInstance();        cal.add(Calendar.MONTH, nMonthsLater);        int daysCurMonth = cal.getActualMaximum(Calendar.DATE); // 当月最大日期        String dt = new SimpleDateFormat(format).format(cal.getTime());        List<String> dtList = new ArrayList<String>(daysCurMonth);        for (int i = 1; i <= daysCurMonth; i++) {            if (i < 10) {                dtList.add(dt + "0" + i);            } else {                dtList.add(dt + i);            }        }        return dtList;    }    /**     * 计算当月日期表示列表; 截止天数- 举例[+2, 表示截止两天后, -1表示截止昨天]     * 比如当前为9月,会返回:["2016-09-01",........"2016-09-30"]     *     * @param afterDays 从1号截止到afterDays天后那天的日期     * @Title: getCurrMonthDateList     * @Description: 计算当前月的天数     * @Since: 2016年9月21日 下午2:28:25     * @Author: niewj     */    public static List<String> getCurrMonthDateList(int afterDays) {        final String format = "yyyy-MM-";        Calendar cal = Calendar.getInstance();//        int daysCurMonth = cal.getActualMaximum(Calendar.DATE); // 当月最大日期        cal.add(Calendar.DATE, afterDays);        int currDay = cal.get(Calendar.DATE);        String dt = new SimpleDateFormat(format).format(cal.getTime());        List<String> dtList = new ArrayList<String>(currDay);        for (int i = 1; i <= currDay; i++) {            if (i < 10) {                dtList.add(dt + "0" + i);            } else {                dtList.add(dt + i);            }        }        return dtList;    }    /**     * 获取本月第N天是周几     *     * @Title: getDayOfWeekMonth     * @Description: 获取上个月第N天是周几(1=周一;7=周日)     * @Since: 2016年9月18日 下午8:39:13     * @Author: niewj     * @Param days 上月第几天     */    public static int getDayOfWeekMonth(int days) {        Calendar calendar = Calendar.getInstance();        calendar.add(Calendar.MONTH, 0);        calendar.set(Calendar.DAY_OF_MONTH, days); // 当月第几天        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);        // 把周日(1)定义为第7天        if (dayOfWeek == 1) {            return ConstUtil.SUNDAY_NUM;        } else {            return dayOfWeek - 1;        }    }    /**     * 获取days天后是是周几-接受负数表示-1表示昨天     * @Title: getWeekdayAfterDays     * @Description: 获取昨天是周几(1=周一;7=周日)     * @Since: 2016年9月20日 下午8:39:13     * @Author: niewj     * @Param days 上月第几天     */    public static int getWeekdayAfterDays(int days) {        long timeMillis = new Date().getTime() + days * ConstUtil.ONE_DAY_MILLIS;        Calendar calendar = Calendar.getInstance();        calendar.setTimeInMillis(timeMillis);        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);        // 把周日(1)定义为第7天        if (dayOfWeek == 1) {            return ConstUtil.SUNDAY_NUM;        } else {            return dayOfWeek - 1;        }    }    /**     * 计算本月周几有几天:<周一, 4天>     */    public static Map<Integer, Integer> getWeekdayCountMap() {        int daysThisMonth = currentMonthDays();        Map<Integer, Integer> weekdayCountMap = new HashMap<Integer, Integer>();        for (int i = 1; i <= daysThisMonth; i++) {            int weekday = getDayOfWeekMonth(i); // 计算本月第i天是周几            // 如果包含周i, 就+1            if (weekdayCountMap.containsKey(weekday)) {                weekdayCountMap.put(weekday, weekdayCountMap.get(weekday) + 1);            } else {                weekdayCountMap.put(weekday, 1);            }        }        return weekdayCountMap;    }    /**     * 获取当前日期已经占当前月的时间进度-今天:days=0;昨天days=-1;明天days=2     *     * @param days 计算days天日期(1,表示明天进度; -1表示昨天)     * @Title: getRateOfMonth     * @Description: 获取当前日期已经占当前月的时间进度     * @Since: 2016年8月22日 下午2:39:13     * @Author: niewj     */    public static String getRateProgressOfMonth(int days) {        long timeMillis = new Date().getTime() + days * ConstUtil.ONE_DAY_MILLIS;        // 1. 得到当前日期: 当前月的第几天        Calendar c = Calendar.getInstance();        c.setTime(new Date(timeMillis));        int date = c.get(Calendar.DAY_OF_MONTH);        // 2. 当前月的天数        int daysOfMonth = currentMonthDays();        // 3. 计算: 保留两位小数, 四舍五入        DecimalFormat df = new DecimalFormat("#.00");        String dbStr = df.format(date * 100.0 / daysOfMonth);        return dbStr;    }    /**     * 获取上个月第N天的日期,如当前是"2016-09-18", 上月第2天返回:"2016-08-02"     * @Title: getDateOfLastMonth     * @Description: 获取上个月第N天的日期     * @Since: 2016年9月18日 下午8:39:13     * @Author: niewj     * @Param days 上月第几天     */    public static String getDateOfLastMonth(int days) {        Calendar calendar = Calendar.getInstance();        calendar.add(Calendar.MONTH, -1);        calendar.set(Calendar.DAY_OF_MONTH, days);        SimpleDateFormat sdf = new SimpleDateFormat(dtFormat);        return sdf.format(calendar.getTime());    }    /**     * 计算指定的日期是周几     * @Title: getWeekdayOfDate     * @Description: 计算指定的日期是周几     * @Since: 2016年9月21日 下午16:39:13     * @Author: niewj     * @Param dt 日期     */    public static int getWeekdayOfDate(String dt) {        Date date = null;        try {            date = new SimpleDateFormat(dtFormat).parse(dt);        } catch (ParseException e) {            e.printStackTrace();        }        Calendar calendar = Calendar.getInstance();        calendar.setTime(date);        int weekday = calendar.get(Calendar.DAY_OF_WEEK);        // 把周日(1)定义为第7天        if (weekday == 1) {            return ConstUtil.SUNDAY_NUM;        } else {            return weekday - 1;        }    }    /**     * 获取上个月第N天是周几-     * @Title: getDateOfLastMonth     * @Description: 获取上个月第N天是周几(1=周一;7=周日)     * @Since: 2016年9月18日 下午8:39:13     * @Author: niewj     * @Param days 上月第几天     */    public static int getDayOfWeekLastMonth(int days) {        Calendar calendar = Calendar.getInstance();        calendar.add(Calendar.MONTH, -1);        calendar.set(Calendar.DAY_OF_MONTH, days); // 当月第几天        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);        if (dayOfWeek == 1) {            return ConstUtil.SUNDAY_NUM;        } else {            return dayOfWeek - 1;        }    }    /**     * 求指定日期的上月日期 yyyymm格式:     * 举例: 如给定日期是201701的Date对象,则返回"201612"     *     * @param date 给定的日期     * @return 给定日期的上月日期的字符串表示yyyymm     */    public static String getYyyyMmLastMonth(Date date) {        if (date == null) {            return null;        }        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");        Calendar calendar = Calendar.getInstance();        // 设置为当前时间        calendar.setTime(date);        // 设置为上一个月        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);        date = calendar.getTime();        return dateFormat.format(date);    }    public static void main(String[] args) {        System.out.println(getWeekdayAfterDays(-8));        System.out.println(getAllMonthDateList(0));        System.out.println(getCurrMonthDateList(-1));        System.out.println(getWeekdayOfDate("2016-09-21"));        System.out.println(getMonthsDateList(24));//        Map<Integer, Integer> weekdayCountMap = getWeekdayCountMap();////        for(Map.Entry<Integer, Integer> entry:weekdayCountMap.entrySet()){//            System.out.println(entry.getKey()+"\t = "+entry.getValue());//        }////        System.out.println(getRateProgressOfMonth(-1));//        System.out.println(getDateOfLastMonth(1));//        System.out.println(getDateOfLastMonth(5));//        System.out.println(getDayOfWeekLastMonth(28)); // 上月28号是周(7)    }}

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消