Java — CommonUtil

Posted 一个小机灵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java — CommonUtil相关的知识,希望对你有一定的参考价值。

一些Java的公用方法:

1:获取当前时间
2:判断当前时间是否在时间date2之前
3:比较时间大小
4:获取某个时间的前n个小时
5:返回某个字符串时间的Calendar对象
6:判断两个时间段是否有重叠
7:获取前后n周的周x
8:获取过去12个月份yyyy-MM

 

1.获取当前时间

    public static String getToday() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String today = formatter.format(date);
        return today;
    }

 

2.判断当前时间是否在时间date2之前

     /**
      * 时间格式 2018-4-21 16:16:34
      * @param date2
      * @return
      */
    public static boolean isDateBefore(String date2) {
        try {
            Date date1 = new Date();
            DateFormat df = DateFormat.getDateTimeInstance();
            return date1.before(df.parse(date2));
        } catch (ParseException e) {
            return false;
        }
    }

 

3.比较时间大小

    // 比较时间大小  date1<date2 true
     public static boolean compareMinDate(String date1, String date2)
             throws ParseException {
         Date d1 = convertToCalendar(date1).getTime();
         Date d2 = convertToCalendar(date2).getTime();
         return d1.before(d2);
     }

 

 4.获取某个时间的前n个小时

    // 获取某个时间的前n小时
    public static String getBeforeNHour(int n, String nowTime, String pattern) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        date = sdf.parse(nowTime);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, n);
        return sdf.format(cal.getTime());
    }

 

5.返回某个字符串时间的Calendar对象

     // 返回某个字符串时间的Calendar对象
     public static Calendar convertToCalendar(String date) throws ParseException {
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         Date d = sdf.parse(date);
         GregorianCalendar calendar = new GregorianCalendar();
         calendar.setTime(d);
         return calendar;
     }

 

6.判断两个时间段是否有重叠

2个时间段:
begin2, end2
beginTime, endTime

公式:
begin2 <= endTime and end2 >= beginTime


if (compareHours(begin2, endTime)>0 && compareHours(beginTime, end2)>0) {
    errorMsg = "重叠了";
    break;
}

 

7.获取前后n周的周x

    private String getLastMonday() {
        Calendar cal = Calendar.getInstance();
        // n为推迟的周数,1本周,-1向前推迟一周,2下周,依次类推
        int n = -1;
        String monday;
        cal.add(Calendar.DATE, n * 7);
        // 想周几,这里就传几Calendar.MONDAY(TUESDAY...)
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return monday;
    }

 

8.获取过去12个月份yyyy-MM

    private String[] getLast12Months(){  
    DecimalFormat df = new DecimalFormat("00");
        String[] last12Months = new String[12];  
        Calendar cal = Calendar.getInstance();  
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+1); //先+1,才能把本月进去
        for(int i=0; i<12; i++){  
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1); //逐次往前推1个月  
            last12Months[i] = cal.get(Calendar.YEAR)+ "-" + df.format(cal.get(Calendar.MONTH)+1);  
        }  
        return last12Months;  
    }

 







以上是关于Java — CommonUtil的主要内容,如果未能解决你的问题,请参考以下文章

java Examples-src-main-java-com-groupdocs-signature-examples-CommonUtilities-applyLicense.java

java Examples-src-main-java-com-groupdocs-signature-examples-CommonUtilities-applyLicense.java

java Examples-src-main-java-com-groupdocs-signature-examples-CommonUtilities-commonutilities.java

java Examples-src-main-java-com-groupdocs-signature-examples-CommonUtilities-commonutilities.java

java Examples-src-main-java-com-groupdocs-signature-examples-CommonUtilities-commonutilities.java

java Examples-src-main-java-com-groupdocs-signature-examples-CommonUtilities-getConfiguration.java