/**
* 将侯转换为时间
*
* @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;
}