zoukankan      html  css  js  c++  java
  • Java获取指定时间段的年份(开始、结束时间)、月份(开始、结束时间)、天数(开始、结束时间)

    package test;
    
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.TreeMap;
    
    public class DateTest {
    
        public static void main(String[] args) {
            System.out.println(getYears("2016-01-01 00:00:00","2018-07-01 00:00:00"));
            System.out.println(getMonths("2018-01-01 00:00:00","2018-07-01 00:00:00"));
            System.out.println(getDays("2018-06-01 00:00:00","2018-07-01 00:00:00"));
        }
        
        /***
         * 获取两个时间段的年份/年第一天/年最后一天
         * @param startTime
         * @param endTime
         * @return
         */
        public static List<Map> getYears(String startTime, String endTime) {
            List<Map> res=new ArrayList<Map>();
            DateFormat dateFormat = new SimpleDateFormat("yyyy");
            DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date start = dateFormat.parse(startTime);
                Date end = dateFormat.parse(endTime);
                Calendar tempStart = Calendar.getInstance();
                tempStart.setTime(start);
                Calendar tempEnd = Calendar.getInstance();
                tempEnd.setTime(end);
                tempEnd.add(Calendar.YEAR,1);// 日期加1(包含结束)
                while (tempStart.before(tempEnd)) {
                    String year=dateFormat.format(tempStart.getTime());
                    String first=year+"-01-01 00:00:00";
                    String last=year+"-12-31 23:59:59";
                    Map<String,Object> map=new HashMap<String,Object>();
                    map.put("year", year);
                    map.put("first", dateFormat2.parse(first));
                    map.put("last", dateFormat2.parse(last));
                    res.add(map);
                    tempStart.add(Calendar.YEAR,1);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return res;
        }
        
        /***
         * 获取两个时间段的年份-月份/月第一天/月最后一天
         * @param startTime
         * @param endTime
         * @return
         */
        public static List<Map> getMonths(String startTime, String endTime) {
            List<Map> res=new ArrayList<Map>();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
            DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DateFormat dateFormat3 = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date start = dateFormat.parse(startTime);
                Date end = dateFormat.parse(endTime);
                Calendar tempStart = Calendar.getInstance();
                tempStart.setTime(start);
                Calendar tempEnd = Calendar.getInstance();
                tempEnd.setTime(end);
                tempEnd.add(Calendar.MONTH,1);// 日期加1(包含结束)
                while (tempStart.before(tempEnd)) {
                    String month=dateFormat.format(tempStart.getTime());
                    tempStart.set(Calendar.DAY_OF_MONTH, 1);
                    String first=dateFormat3.format(tempStart.getTime());
                    tempStart.set(Calendar.DAY_OF_MONTH, tempStart.getActualMaximum(Calendar.DAY_OF_MONTH));  
                    Map<String,Object> map=new HashMap<String,Object>();
                    map.put("month", month);
                    map.put("first", dateFormat2.parse(first+" 00:00:00"));
                    map.put("last", dateFormat2.parse(first+" 23:59:59"));
                    res.add(map);
                    tempStart.add(Calendar.MONTH,1);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return res;
        }
    
        /***
         * 获取两个时间段的天数/开始时间/结束时间
         * @param startTime
         * @param endTime
         * @return
         */
        public static List<Map> getDays(String startTime, String endTime) {
            // 返回的日期集合
            List<Map> res=new ArrayList<Map>();
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date start = dateFormat.parse(startTime);
                Date end = dateFormat.parse(endTime);
                Calendar tempStart = Calendar.getInstance();
                tempStart.setTime(start);
                Calendar tempEnd = Calendar.getInstance();
                tempEnd.setTime(end);
                tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
                while (tempStart.before(tempEnd)) {
                    String day=dateFormat.format(tempStart.getTime());
                    Map<String,Object> map=new HashMap<String,Object>();
                    map.put("day", day);
                    map.put("first", dateFormat2.parse(day+" 00:00:00"));
                    map.put("last", dateFormat2.parse(day+" 23:59:59"));
                    res.add(map);
                    tempStart.add(Calendar.DAY_OF_YEAR, 1);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return res;
        }
    }
  • 相关阅读:
    数组的应用:一。冒泡排序二。折半查找!二维数组的学习。
    break与continue,while 循环和一维数组的学习及作业
    for循环的应用:迭代法和穷举法
    循环
    称体重
    js js弹出框、对话框、提示框、弹窗总结
    windows 服务器开设端口
    SQL Server 数据库分离与附加(图文教程)
    ASP.NET MVC5 PagedList分页示例
    mvc 连接数据库但单复值得问题
  • 原文地址:https://www.cnblogs.com/xmqa/p/10255353.html
Copyright © 2011-2022 走看看