Ver código fonte

Merge remote-tracking branch 'origin/master'

Administrator 1 ano atrás
pai
commit
7be007e740

+ 117 - 0
ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/DateUtils.java

@@ -12,6 +12,7 @@ import java.time.LocalDateTime;
 import java.time.LocalTime;
 import java.time.ZoneId;
 import java.time.ZonedDateTime;
+import java.util.Calendar;
 import java.util.Date;
 
 /**
@@ -165,4 +166,120 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
         ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
         return Date.from(zdt.toInstant());
     }
+
+    /**
+     * 判断当前日期是星期几
+     *
+     * @param pTime 修要判断的时间
+     * @return dayForWeek 判断结果
+     * @Exception 发生异常
+     */
+
+    public static int dayForWeek(String pTime) throws Exception {
+
+        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
+
+        Calendar c = Calendar.getInstance();
+
+        c.setTime(format.parse(pTime));
+
+        int dayForWeek = 0;
+
+        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
+
+            dayForWeek = 7;
+
+        } else {
+
+            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
+
+        }
+
+        return dayForWeek;
+
+    }
+
+    /***
+     * 判断str3是否在str1与str2之间
+     * @param str1
+     * @param str2
+     * @param str3
+     * @param format 时间格式
+     * @return
+     * @throws Exception
+     */
+    public static Boolean isClass(String str1,String str2,String str3,String format) throws Exception {
+        //判断某个日期是否在两个日期范围之外
+        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
+        Date date1 = simpleDateFormat.parse(str1);
+        Date date2 = simpleDateFormat.parse(str2);
+        Date date3 = simpleDateFormat.parse(str3);
+        Boolean bl = true;
+        if (date1.getTime() > date3.getTime() || date2.getTime() < date3.getTime()) {
+            bl = false;
+        }
+        return bl;
+    }
+
+    public static String timeMonthFirst() {
+        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
+        Calendar cale = Calendar.getInstance();
+        cale.add(Calendar.MONTH, 0);
+        cale.set(Calendar.DAY_OF_MONTH, 1);
+        String firstday = format.format(cale.getTime());
+        return firstday;
+    }
+
+
+    /***
+     * 获取所传时间的周一时间
+     * @return
+     * @throws Exception
+     */
+    public static String getTimeIntervalOne(Date date) {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(date);
+        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
+        if (1 == dayWeek) {
+            cal.add(Calendar.DAY_OF_MONTH, 0);
+        }
+        cal.setFirstDayOfWeek(Calendar.MONDAY);
+        int day = cal.get(Calendar.DAY_OF_WEEK);
+        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
+        String imptimeBegin = sdf.format(cal.getTime());
+        return imptimeBegin ;
+    }
+
+    /***
+     * 获取所传时间的周五时间
+     * @return date 本周的第一天
+     * @throws Exception
+     */
+    public static String getTimeIntervalFive(String date) throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        Date da = sdf.parse(date);
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(da);
+        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
+        if (1 == dayWeek) {
+            cal.add(Calendar.DAY_OF_MONTH, 0);
+        }
+        cal.setFirstDayOfWeek(Calendar.MONDAY);
+        cal.add(Calendar.DATE, 4);
+        String imptimeEnd = sdf.format(cal.getTime());
+        return imptimeEnd;
+    }
+
+    public static int getYear() {
+        Calendar calendar = Calendar.getInstance();
+        int year = calendar.get(Calendar.YEAR);
+        return year;
+    }
+
+    public static int getMonth () {
+        Calendar calendar = Calendar.getInstance();
+        int month = calendar.get(Calendar.MONTH) + 1;
+        return month;
+    }
 }