|
@@ -14,7 +14,6 @@ import java.util.*;
|
|
|
*/
|
|
|
public class DateUtil {
|
|
|
|
|
|
-
|
|
|
private static final String CHINESE_DATA_FORMAT = "yyyy年MM月dd日 HH:mm:ss";
|
|
|
|
|
|
public static final String DEFAULT_PATTERN = "yyyy-MM-dd";
|
|
@@ -885,14 +884,14 @@ public class DateUtil {
|
|
|
* @param weekNum 周几
|
|
|
* @return java.util.List<java.util.Date>
|
|
|
*/
|
|
|
- public static List<Date> getWeekDays(Date startTime, Date endTime, int weekNum){
|
|
|
+ public static List<Date> getWeekDays(Date startTime, Date endTime, int weekNum) {
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
- List<Date> dates=new ArrayList<>();
|
|
|
- for(Date x = startTime; x.compareTo(endTime) <= 0;){
|
|
|
+ List<Date> dates = new ArrayList<>();
|
|
|
+ for (Date x = startTime; x.compareTo(endTime) <= 0;) {
|
|
|
calendar.setTime(x);
|
|
|
calendar.add(Calendar.DATE, 1);
|
|
|
x = calendar.getTime();
|
|
|
- if(calendar.get(Calendar.DAY_OF_WEEK) == weekNum){
|
|
|
+ if (calendar.get(Calendar.DAY_OF_WEEK) == weekNum) {
|
|
|
dates.add(x);
|
|
|
}
|
|
|
}
|
|
@@ -909,7 +908,7 @@ public class DateUtil {
|
|
|
public static Date getWeekMondayWithDate(Date date) {
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
try {
|
|
|
- date=new SimpleDateFormat(DEFAULT_PATTERN).parse(new SimpleDateFormat(DEFAULT_PATTERN).format(date));
|
|
|
+ date = new SimpleDateFormat(DEFAULT_PATTERN).parse(new SimpleDateFormat(DEFAULT_PATTERN).format(date));
|
|
|
} catch (ParseException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -960,14 +959,12 @@ public class DateUtil {
|
|
|
* @param days: 天数
|
|
|
* @return java.util.Date
|
|
|
*/
|
|
|
- public static Date addDays1(Date date,int days) {
|
|
|
+ public static Date addDays1(Date date, int days) {
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
cal.setTime(date);
|
|
|
cal.add(Calendar.DATE, days);
|
|
|
try {
|
|
|
- return new SimpleDateFormat(DateUtil.DEFAULT_PATTERN)
|
|
|
- .parse(new SimpleDateFormat(DateUtil.DEFAULT_PATTERN)
|
|
|
- .format(cal.getTime()));
|
|
|
+ return new SimpleDateFormat(DateUtil.DEFAULT_PATTERN).parse(new SimpleDateFormat(DateUtil.DEFAULT_PATTERN).format(cal.getTime()));
|
|
|
} catch (ParseException e) {
|
|
|
return cal.getTime();
|
|
|
}
|
|
@@ -978,7 +975,7 @@ public class DateUtil {
|
|
|
* @param date
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String date2ChineseDate(Date date){
|
|
|
+ public static String date2ChineseDate(Date date) {
|
|
|
return new SimpleDateFormat(CHINESE_DATA_FORMAT).format(date);
|
|
|
}
|
|
|
|
|
@@ -989,13 +986,13 @@ public class DateUtil {
|
|
|
* @param dateZones:
|
|
|
* @return boolean
|
|
|
*/
|
|
|
- public static boolean checkDateZoneOverlap(List<DateZone> dateZones){
|
|
|
+ public static boolean checkDateZoneOverlap(List<DateZone> dateZones) {
|
|
|
dateZones.sort(Comparator.comparing(DateZone::getStartTime));
|
|
|
- if(dateZones.size()<=1){
|
|
|
+ if (dateZones.size() <= 1) {
|
|
|
return false;
|
|
|
- }else{
|
|
|
- for (int i=0;i<dateZones.size()-1;i++){
|
|
|
- if(dateZones.get(i+1).getStartTime().before(dateZones.get(i).getEndTime())){
|
|
|
+ } else {
|
|
|
+ for (int i = 0; i < dateZones.size() - 1; i++) {
|
|
|
+ if (dateZones.get(i + 1).getStartTime().before(dateZones.get(i).getEndTime())) {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
@@ -1009,7 +1006,7 @@ public class DateUtil {
|
|
|
* @date 2019/10/27
|
|
|
* @return
|
|
|
*/
|
|
|
- public static class DateZone{
|
|
|
+ public static class DateZone {
|
|
|
private Date startTime;
|
|
|
private Date endTime;
|
|
|
|
|
@@ -1050,32 +1047,49 @@ public class DateUtil {
|
|
|
calendar.setTime(date);
|
|
|
int i = calendar.get(Calendar.DAY_OF_WEEK);
|
|
|
switch (i) {
|
|
|
- case 1:
|
|
|
- return "星期日";
|
|
|
- case 2:
|
|
|
- return "星期一";
|
|
|
- case 3:
|
|
|
- return "星期二";
|
|
|
- case 4:
|
|
|
- return "星期三";
|
|
|
- case 5:
|
|
|
- return "星期四";
|
|
|
- case 6:
|
|
|
- return "星期五";
|
|
|
- case 7:
|
|
|
- return "星期六";
|
|
|
- default:
|
|
|
- return "";
|
|
|
+ case 1:
|
|
|
+ return "星期日";
|
|
|
+ case 2:
|
|
|
+ return "星期一";
|
|
|
+ case 3:
|
|
|
+ return "星期二";
|
|
|
+ case 4:
|
|
|
+ return "星期三";
|
|
|
+ case 5:
|
|
|
+ return "星期四";
|
|
|
+ case 6:
|
|
|
+ return "星期五";
|
|
|
+ case 7:
|
|
|
+ return "星期六";
|
|
|
+ default:
|
|
|
+ return "";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取当前时间到0点的毫秒数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static long getTomorrowZeroSeconds() {
|
|
|
+ long current = System.currentTimeMillis();// 当前时间毫秒数
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ long tomorrowzero = calendar.getTimeInMillis();
|
|
|
+ long tomorrowzeroSeconds = (tomorrowzero - current) / 1000;
|
|
|
+ return tomorrowzeroSeconds;
|
|
|
+ }
|
|
|
+
|
|
|
public static void main(String[] args) throws ParseException {
|
|
|
-// DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ // DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
// DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
-// System.out.println(daysBetween(df.parse("2017-07-20 10:07:42"), df.parse(df.format(new Date()))));
|
|
|
+ // System.out.println(daysBetween(df.parse("2017-07-20 10:07:42"), df.parse(df.format(new Date()))));
|
|
|
System.out.println(getWeekMondayWithDate(new Date()));
|
|
|
System.out.println(getNextWeekMonday(new Date()));
|
|
|
System.out.println(getNextWeekSunday(new Date()));
|
|
|
- System.out.println(dayEnd(new Date()));
|
|
|
+ System.out.println(dayEnd(new Date()));
|
|
|
}
|
|
|
}
|