侯,旬,月,季,年转换为时间


    /**
     * 将侯转换为时间
     *
     * @param year     年份
     * @param startHou 开始侯
     * @param endHou   结束侯
     * @return
     */
    public static String[] parseDateHou(Integer year, Integer startHou, Integer endHou) {

        //默认侯的时间为一侯
        String[] result = new String[]{"2021-01-01", "2021-01-05"};
        if (startHou > endHou) {
            return result;
        }
        String startMonth = Arith.autoGenericCode((int) (Math.ceil(Double.valueOf(startHou) / 6.0)) + "".trim(), 2);
        String endMonth = Arith.autoGenericCode((int) (Math.ceil(Double.valueOf(endHou) / 6.0)) + "".trim(), 2);
        String startDay = Arith.autoGenericCode(((startHou - 1) * 5) + 1 - ((Integer.valueOf(startMonth) - 1) * 30) + "".trim(), 2);
        double v = Double.valueOf(endHou) % 6 * 5;
        String endDay = Arith.autoGenericCode((int) (v) + "".trim(), 2);
        if (v == 0) {
            endDay = parseLeapYearByDay(year + "".trim(), endMonth);

        }
        result[0] = year + "-" + startMonth + "-" + startDay;
        result[1] = year + "-" + endMonth + "-" + endDay;
        return result;
    }

    /**
     * 将季度转换为时间
     */
    public static String[] yearQuarterList(Integer year, Integer startQuarter, Integer endQuzrter ) {
        String[] result = new String[]{"2021-01-01", "2021-03-31"};
        String startDay = "";
        String endDay = "";
        switch (startQuarter){
            case 1:
                startDay = "01-01";
                break;
            case 2:
                startDay = "04-01";

                break;
            case 3:
                startDay = "07-01";

                break;
            case 4:
                startDay = "10-01";
                break;
        }
        switch (endQuzrter){
            case 1:
                endDay = "03-31";
                break;
            case 2:
                endDay = "06-30";
                break;
            case 3:
                endDay = "09-30";
                break;
            case 4:
                endDay = "12-31";
                break;
        }
        result[0] = year + "-" + startDay ;
        result[1] = year + "-"  + endDay;
        return result;
    }
/**
     * 将月份转换为时间
     *
     * @param year
     * @param startMonth
     * @param endMonth
     * @return
     */
    public static String[] parseDateMonth(Integer year, Integer startMonth, Integer endMonth) {
        String[] result = new String[]{"2021-01-01", "2021-01-31"};
        if (startMonth > endMonth) {
            return result;
        }
        result[0] = year + "-" + startMonth + "-" + "01";
        result[1] = year + "-" + endMonth + "-" + parseLeapYearByDay(year + "".trim(), endMonth + "".trim());
        return result;
    }
 /**
     * 返回天数
     *
     * @param year
     * @param endMonth
     * @return
     */
    public static String parseLeapYearByDay(String year, String endMonth) {
        String data = "01,03,05,07,08,10,12";
        String day = "30";
        if (data.contains(Arith.autoGenericCode(endMonth + "".trim(), 2))) {
            day = "31";
        }
        if ("02".equals(Arith.autoGenericCode(endMonth + "".trim(), 2))) {
            if ((Integer.valueOf(year) % 4 == 0) && (Integer.valueOf(year) % 100 != 0) || (Integer.valueOf(year) % 400 == 0)) {
                day = "29";
            } else {
                day = "28";
            }
        }
        return day;
    }
 /**
     * 将旬转换为时间
     *
     * @return
     */
    public static List<Map<String, String>> parseDateTenDay(String startTenDay, String endTenDay) {

        try {
            List<Map<String, String>> mapList = new LinkedList<>();
            String[] startTen = startTenDay.split("-");
            String startTenDay_day = "01";
            if ("02".equals(startTen[2])) {
                startTenDay_day = "11";
            }
            if ("03".equals(startTen[2])) {
                startTenDay_day = "21";
            }
            //开始时间
            String startTime = startTen[0] + "-" + startTen[1] + "-" + startTenDay_day;

            String[] endTen = endTenDay.split("-");
            String endTenTenDay_day = "10";
            if ("02".equals(endTen[2])) {
                endTenTenDay_day = "20";
            }
            if ("03".equals(endTen[2])) {
                endTenTenDay_day = parseLeapYearByDay(endTen[0], endTen[1]);
            }
            //结束时间
            String endTime = endTen[0] + "-" + endTen[1] + "-" + endTenTenDay_day;

            System.out.println(startTime + "  ==  " + endTime);
            Date endPlusTime =  getAddOrDeleteDay(shortSdf.parse(startTime),getEndXunDay(startTime));
            String endTime1 = shortSdf.format(endPlusTime);
            while (true){
                Map<String,String> map = new HashMap<>();
                System.out.println(getTenDayName(startTime) +" ==== "+ startTime + " == " + endTime1);
                map.put("startTime",startTime);
                map.put("endTime",endTime1);
                map.put("tenDayName",getTenDayName(startTime));
                mapList.add(map);
                Date plusTime =  getAddOrDeleteDay(shortSdf.parse(startTime),getStartXunDay(startTime));
                startTime = shortSdf.format(plusTime);
                Date plusTime1 =  getAddOrDeleteDay(shortSdf.parse(startTime),getEndXunDay(startTime));
                endTime1 = shortSdf.format(plusTime1);
                if(plusTime.getTime() > shortSdf.parse(endTime).getTime()){
                    break;
                }
            }
            return mapList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String getTenDayName(String time){
        String  day = time.split("-")[0] + "年" + time.split("-")[1] + "月";
        if("01".equals(time.split("-")[2])){
            day += "上旬";
        }
        if("11".equals(time.split("-")[2])){
            day += "中旬";
        }
        if("21".equals(time.split("-")[2])){
            day += "下旬";
        }
        return day;
    }

    private static Integer getStartXunDay(String time){
        int numDay =10;
        String day = parseLeapYearByDay(time.split("-")[0],time.split("-")[1]);
        if("31".equals(day) && "21".equals(time.split("-")[2])){
            numDay =11;
        }
        if("28".equals(day) && "21".equals(time.split("-")[2])){
            numDay =8;
        }
        if("29".equals(day) && "21".equals(time.split("-")[2])){
            numDay =9;
        }
        return numDay;
    }

    private static Integer getEndXunDay(String time){
        int numDay =9;
        String day = parseLeapYearByDay(time.split("-")[0],time.split("-")[1]);
        if("31".equals(day) && "21".equals(time.split("-")[2])){
            numDay =10;
        }
        if("28".equals(day) && "21".equals(time.split("-")[2])){
            numDay =7;
        }
        if("29".equals(day) && "21".equals(time.split("-")[2])){
            numDay =8;
        }
        return numDay;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨迹嘿嘿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值