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

日期工具类

时间:2016-06-02 20:00:29      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

public class DataUtil {

    //获取每个月的第一天
    public static String getFisrtDayOfMonth(String str) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(str);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(1);
        int month = cal.get(2) + 1;

        cal.set(1, year);

        cal.set(2, month - 1);

        int firstDay = cal.getActualMinimum(5);

        cal.set(5, firstDay);

        String firstDayOfMonth = sdf.format(cal.getTime());

        return firstDayOfMonth;
    }

    /**
     * 获取str对应的日期是第几周
     * @param str 格式:2016-06-13
     * @return
     * @throws Exception
     */
    public static int getWeek(String str) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(str);
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(date);

        int week = calendar.get(4);

        int day = calendar.get(7);

        return week;
    }

    /**
     * 获取currentDate(指定日期)是否是yearMonth(指定年月份)的前7天
     * @param yearMonth  格式:2016-06
     * @param currentDate  格式:2016-06-13
     * @return 1表示是前七天,0表示不是
     * @throws Exception
     */
    public static int getWeek2(String yearMonth,String currentDate) throws Exception {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        sdf1.setLenient(false);
        Date date1 = sdf1.parse(currentDate);
        for (int i = 1; i < 8; i++)
            try {
                Date date = sdf1.parse(yearMonth + "-" + i);
                if (date1.getTime() <= date.getTime()) {
                    return 1;
                }
            } catch (ParseException localParseException) {
            }
        return 0;
    }

    /**
     * 获取指定年月每周对应的日期,结果如下:
     * {星期三=[‘2016-06-01‘, ‘2016-06-08‘, ‘2016-06-15‘, ‘2016-06-22‘, ‘2016-06-29‘],
     * 星期四=[‘2016-06-02‘, ‘2016-06-09‘, ‘2016-06-16‘, ‘2016-06-23‘, ‘2016-06-30‘],
     * 星期五=[‘2016-06-03‘, ‘2016-06-10‘, ‘2016-06-17‘, ‘2016-06-24‘],
     * 星期六=[‘2016-06-04‘, ‘2016-06-11‘, ‘2016-06-18‘, ‘2016-06-25‘],
     * 星期日=[‘2016-06-05‘, ‘2016-06-12‘, ‘2016-06-19‘, ‘2016-06-26‘],
     * 星期一=[‘2016-06-06‘, ‘2016-06-13‘, ‘2016-06-20‘, ‘2016-06-27‘],
     * 星期二=[‘2016-06-07‘, ‘2016-06-14‘, ‘2016-06-21‘, ‘2016-06-28‘]}
     * @param yearMonth
     * @return
     */
    public static Map<String, List<String>> getDateWeek(String yearMonth) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        sdf1.setLenient(false);
        SimpleDateFormat sdf2 = new SimpleDateFormat("EEE");
        Map map = new LinkedHashMap();
        for (int i = 1; i < 32; i++)
            try {
                Date date = sdf1.parse(yearMonth + "-" + i);
                List dayDate;
                if ((dayDate = (List) map.get(sdf2.format(date))) == null) {
                    dayDate = new ArrayList();
                    map.put(sdf2.format(date), dayDate);
                }
                dayDate.add("‘" + sdf1.format(date) + "‘");
            } catch (ParseException localParseException) {
            }
        return map;
    }

    /**
     * 获取当月已经过去或者未过去的周一到周日的集合
     * @param yearMonth
     * @param currentDate
     * @param flag 已经过去为gone,未过去为left
     * @return
     */
    public static Map<String, List<String>> getLeftOrGoneDateWeek(String yearMonth,String currentDate,String flag) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        sdf1.setLenient(false);
        SimpleDateFormat sdf2 = new SimpleDateFormat("EEE");
        Map map = new LinkedHashMap();
        for (int i = 1; i < 32; i++)
            try {
                Date date = sdf1.parse(yearMonth + "-" + i);
                List dayDate;
                if ((dayDate = (List) map.get(sdf2.format(date))) == null) {
                    dayDate = new ArrayList();
                    map.put(sdf2.format(date), dayDate);
                }
                Date date1 = sdf1.parse(currentDate);
                if ("left".equalsIgnoreCase(flag)) {
                    if (date.getTime() > date1.getTime()){
                        dayDate.add("‘" + sdf1.format(date) + "‘");
                    }
                }else if ("gone".equalsIgnoreCase(flag)) {
                    if (date.getTime() <= date1.getTime()){
                        dayDate.add("‘" + sdf1.format(date) + "‘");
                    }
                }
            } catch (ParseException localParseException) {
            }
        return map;
    }


    public static void main(String[] args) throws Exception {
        System.out.println(getLeftOrGoneDateWeek("2016-06","2016-06-02","left"));
    }

}

日期工具类

标签:

原文地址:http://www.cnblogs.com/misssimple/p/5554131.html

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