新增日期工具类

master
yinq 2 years ago
parent ad96cfd928
commit f4cdd9b7ff

@ -4,6 +4,11 @@ import org.apache.commons.lang.time.DateUtils;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.*; import java.util.*;
/** /**
@ -19,7 +24,7 @@ public class DateReportUtils {
* *
* *
* @param start * @param start
* @param end * @param end
* @return week start end * @return week start end
*/ */
public static List<Map<String, String>> getWeekOfDate(String start, String end) { public static List<Map<String, String>> getWeekOfDate(String start, String end) {
@ -51,16 +56,16 @@ public class DateReportUtils {
if (i == 1) {//第一周以 start 开始 if (i == 1) {//第一周以 start 开始
map.put("start", dateFormat(startDate, sdf2)); map.put("start", dateFormat(startDate, sdf2));
}else { } else {
map.put("start",dateFormat(cal.getTime(),sdf2)); map.put("start", dateFormat(cal.getTime(), sdf2));
} }
if (i == weekNum) {//最后一周以 end 结束 if (i == weekNum) {//最后一周以 end 结束
map.put("end", dateFormat(endDate, sdf2)); map.put("end", dateFormat(endDate, sdf2));
}else { } else {
//设置这周的周日日期1代表周日取值范围1~7设置1~7之外会从周日开始往前后推算负前正后DAY_OF_WEEK的日期变更范围只会是在当前日期的周 //设置这周的周日日期1代表周日取值范围1~7设置1~7之外会从周日开始往前后推算负前正后DAY_OF_WEEK的日期变更范围只会是在当前日期的周
cal.set(Calendar.DAY_OF_WEEK,1); cal.set(Calendar.DAY_OF_WEEK, 1);
map.put("end",dateFormat(cal.getTime(),sdf2)); map.put("end", dateFormat(cal.getTime(), sdf2));
} }
list.add(map); list.add(map);
@ -101,16 +106,16 @@ public class DateReportUtils {
if (i == 1) {//第一周以 start 开始 if (i == 1) {//第一周以 start 开始
map.put("start", dateFormat(startDate, sdf3)); map.put("start", dateFormat(startDate, sdf3));
}else { } else {
map.put("start",dateFormat(cal.getTime(),sdf3)); map.put("start", dateFormat(cal.getTime(), sdf3));
} }
if (i == weekNum) {//最后一周以 end 结束 if (i == weekNum) {//最后一周以 end 结束
map.put("end", dateFormat(endDate, sdf3)); map.put("end", dateFormat(endDate, sdf3));
}else { } else {
//设置这周的周日日期1代表周日取值范围1~7设置1~7之外会从周日开始往前后推算负前正后DAY_OF_WEEK的日期变更范围只会是在当前日期的周 //设置这周的周日日期1代表周日取值范围1~7设置1~7之外会从周日开始往前后推算负前正后DAY_OF_WEEK的日期变更范围只会是在当前日期的周
cal.set(Calendar.DAY_OF_WEEK,1); cal.set(Calendar.DAY_OF_WEEK, 1);
map.put("end",dateFormat(cal.getTime(),sdf3)); map.put("end", dateFormat(cal.getTime(), sdf3));
} }
list.add(map); list.add(map);
@ -147,7 +152,7 @@ public class DateReportUtils {
* @throws ParseException * @throws ParseException
*/ */
public static Date stringToDate(String str, String date) { public static Date stringToDate(String str, String date) {
if (str.indexOf("null") != -1){ if (str.indexOf("null") != -1) {
return null; return null;
} }
try { try {
@ -190,5 +195,78 @@ public class DateReportUtils {
} }
} }
/**
*
* @return
*/
public static Integer getMonthRemainingDays() {
// Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:OO")); //这是获得东八区时间,也就是北京时间
// int day = c.get(Calendar.DAY_OF_MONTH); //获取当前天数
// int last = c.getActualMaximum(c.DAY_OF_MONTH); //获取本月最大天数
Integer result = 0;
LocalDate localDate = LocalDate.now();
LocalDate lastDay = localDate.with(TemporalAdjusters.lastDayOfMonth());
List<Date> dateList = getBetweenDates(localDate2Date(localDate), localDate2Date(lastDay));
for (Date date : dateList) {
if (!getWeekOfDate(date).equals("星期日")){
result += 1;
}
}
return result;
}
/**
* LocalDateDate
* @param localDate
* @return
*/
public static Date localDate2Date(LocalDate localDate) {
if(null == localDate) {
return null;
}
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
return Date.from(zonedDateTime.toInstant());
}
/**
* <br>
*
* @param date
* @return
*/
public static String getWeekOfDate(Date date) {
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* List
* @param start
* @param end
* @return
*/
public static List<Date> getBetweenDates(Date start, Date end) {
List<Date> result = new ArrayList<Date>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
result.add(tempStart.getTime());
tempStart.add(Calendar.DAY_OF_YEAR, 1);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
while (tempStart.before(tempEnd)) {
result.add(tempStart.getTime());
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
result.add(tempEnd.getTime());
return result;
}
} }

Loading…
Cancel
Save