编程语言
首页 > 编程语言> > java获取未来一个月日期字符串,java获取未来7天工作日日期

java获取未来一个月日期字符串,java获取未来7天工作日日期

作者:互联网

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MyDate {

    /**
     * 获取未来31天的日期字符串
     *
     * @return
     */
    public String getDates() {
        Date d = new Date();
        long dTime = d.getTime();
        long dTimes[] = new long[31];
        for (int i = 0; i < dTimes.length; i++) {
            if (i == 0) {
                dTimes[i] = dTime + 86400000;
            } else {
                dTimes[i] = dTimes[i - 1] + 86400000;
            }
        }
        String dates = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        for (int i = 0; i < dTimes.length; i++) {
            dates += (i > 0) ? "," : "";
            dates += sdf.format(new Date(Long.parseLong(String.valueOf(dTimes[i]))));
        }
        return dates;
    }

    /**
     * 获取未来7天工作日的日期
     *
     * @return
     * @throws Exception
     */
    public static List<String> getDateList() throws Exception {
        List<String> dateList = new ArrayList();
        MyDate myDate = new MyDate();
        String dates = myDate.getDates();
        String apiUrl = "https://api.apihubs.cn/holiday/get?date=" + dates + "&order_by=1&cn=1&size=31";
        String result = MyHttp.doGet(apiUrl);
        JSONObject res = JSONObject.parseObject(result);
        String msg = res.getString("msg");
        if (msg == null) {
            return dateList;
        }
        JSONObject data = res.getJSONObject("data");
        JSONArray list = data.getJSONArray("list");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < list.size(); i++) {
            JSONObject item = list.getJSONObject(i);
            if (item.getInteger("workday") == 1 && dateList.size() < 7) {
                Date date = sdf.parse(item.getString("date_cn"));
                String date1 = sdf1.format(new Date(Long.parseLong(String.valueOf(date.getTime()))));
                dateList.add(date1);
            }
        }
        return dateList;
    }

}

以上类中使用到的MyHttp类在以下链接中:MyHttp类

标签:dates,java,String,SimpleDateFormat,获取,日期,import,dTimes,new
来源: https://www.cnblogs.com/phpyangbo/p/15881869.html