java - 判断时间范围区间

JSONObject respObj = new JSONObject(s);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String startTimeStr = "2024-01-01";
String endTimeStr = "2024-01-31";

Date startTimDate = sdf.parse(startTimeStr);  //string 日期 转换为 date
Date endTimDate = sdf.parse(endTimeStr); //string 日期 转换为 date


JSONObject dataObj = respObj.getJSONObject("data");
JSONArray list = dataObj.getJSONArray("list");
for (Object o : list) {
    JSONObject listItem = (JSONObject) o;
    long createTime = listItem.getLong("createTime");
    Date createTimeDate = new Date(createTime);   //时间戳转换为date
    if (!createTimeDate.after(startTimDate) || !createTimeDate.before(endTimDate)){  //判断时间范围
        System.out.println(createTimeDate);
    }else{
        System.out.println("");
    }
}

https://blog.51cto.com/u_16175452/6827109
https://blog.51cto.com/u_16175430/7837429

posted @ 2024-02-02 17:07  chuangzhou  阅读(139)  评论(0编辑  收藏  举报