package com.clear.waterser.util;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class DateUtills {
public static void main(String[] args) {
System.err.println(getTwoDaysDay("2020-01-01 10:10:10", "2020-1-15 10:10:10","yyyy-MM-dd HH",Calendar.HOUR,1));
// t1();
// System.out.println(getFormatList(Calendar.MONTH, "yyyy-MM", "2020-1", "2020-9"));
System.out.println(getDataByFormat(Calendar.YEAR, "yyyy-MM", "2020-1"));
// System.err.println(getFormatList(Calendar.HOUR, "yyyy-MM-dd HH", "2020-1-1 00", "2020-9-15 00"));
}
private static void t1() {
String str = "W21001,W01014,W21001,W21011";
if (str.contains("W21001")) {
System.out.println("ssu");
}
}
// public static DateDto getDateSplit(String startTime,String endTime){
// DateDto dateDto = new DateDto();
// String[] split = startTime.split("-");
// dateDto.setYearStart(split[0]);
// dateDto.setMonStart(split[1]);
// dateDto.setDayStart(split[2]);
//
// String[] splitEnd = endTime.split("-");
// dateDto.setYearEnd(splitEnd[0]);
// dateDto.setMonEnd(splitEnd[1]);
// dateDto.setDayEnd(splitEnd[2]);
// return dateDto;
// }
//取一段时间的每一天
// ,String format,int type,int num
public static List<String> getTwoDaysDay(String dateStart, String dateEnd,String format,int type,int num) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
List<String> dateList = new ArrayList<String>();
try{
Date dateOne = sdf.parse(dateStart);
Date dateTwo = sdf.parse(dateEnd);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateOne);
dateList.add(sdf.format(calendar.getTime()));
while(calendar.getTime().before(dateTwo)){ //倒序时间,顺序after改before其他相应的改动。
calendar.add(type, num);
dateList.add(sdf.format(calendar.getTime()));
}
} catch(Exception e){
e.printStackTrace();
}
return dateList;
}
public static List<String> getFormatList(int type ,String format,String dateStart, String dateEnd) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
List<String> dateList = new ArrayList<String>();
try{
Date dateOne = sdf.parse(dateStart);
Date dateTwo = sdf.parse(dateEnd);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateOne);
dateList.add(sdf.format(sdf.parse(dateStart)));
while(calendar.getTime().before(dateTwo)){ //倒序时间,顺序after改before其他相应的改动。
calendar.add(type, 1);
dateList.add(sdf.format(calendar.getTime()));
}
String s = dateList.get(dateList.size() - 1);
Date datefin = sdf.parse(s);//最后的日期
Calendar calendarEnd = Calendar.getInstance();
calendarEnd.setTime(datefin);
if (calendarEnd.getTime().after(dateTwo)) {
dateList.remove(dateList.size() - 1);
dateList.add(dateEnd);
}
} catch(Exception e){
e.printStackTrace();
}
return dateList;
}
public static String getDataByFormat(int type ,String format,String dateStart) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
// List<String> dateList = new ArrayList<String>();
String res=null;
try{
Date dateOne = sdf.parse(dateStart);
// Date dateTwo = sdf.parse(dateEnd);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateOne);
// dateList.add(sdf.format(sdf.parse(dateStart)));
// while(calendar.getTime().before(dateTwo)){ //倒序时间,顺序after改before其他相应的改动。
calendar.add(type, -1);
res = sdf.format(calendar.getTime());
// }
// String s = dateList.get(dateList.size() - 1);
// Date datefin = sdf.parse(s);//最后的日期
// Calendar calendarEnd = Calendar.getInstance();
// calendarEnd.setTime(datefin);
// if (calendarEnd.getTime().after(dateTwo)) {
// dateList.remove(dateList.size() - 1);
// dateList.add(dateEnd);
// }
} catch(Exception e){
e.printStackTrace();
}
return res;
}
public static String getDataByFormatAdd(int type ,String format,String dateStart) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
// List<String> dateList = new ArrayList<String>();
String res=null;
try{
Date dateOne = sdf.parse(dateStart);
// Date dateTwo = sdf.parse(dateEnd);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateOne);
// dateList.add(sdf.format(sdf.parse(dateStart)));
// while(calendar.getTime().before(dateTwo)){ //倒序时间,顺序after改before其他相应的改动。
calendar.add(type, 1);
res = sdf.format(calendar.getTime());
// }
// String s = dateList.get(dateList.size() - 1);
// Date datefin = sdf.parse(s);//最后的日期
// Calendar calendarEnd = Calendar.getInstance();
// calendarEnd.setTime(datefin);
// if (calendarEnd.getTime().after(dateTwo)) {
// dateList.remove(dateList.size() - 1);
// dateList.add(dateEnd);
// }
} catch(Exception e){
e.printStackTrace();
}
return res;
}
}
获取日期区间的工具类,日期处理
最新推荐文章于 2022-08-02 16:32:25 发布