自用工具类 - ToolUtils

自己写的一个工具类,后续更新

public class ToolUtils {

    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMM = "yyyyMM";

    public static String YYYYMMDD = "yyyyMMdd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYMMDDHHMMSS = "YYMMDDHHMMSS";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public final static String YYYYMMDD_HHMMSS_SSS = "yyyyMMdd_HHmmss_SSS";

    public static final String ZERO_HOURS=" 00:00:00";

    public static final String ZERO_ONE_HOURS = " 00:00:01";

    public static final String TWENTY_FOUR_HOURS=" 23:59:59";

    /**
     * 常用时间格式
     */
    private static final String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"
    };

    /**
     * 获取当前Date型日期 时间
     * @return
     */
    public static Date getCurrentTime(){
        return new Date();
    }

    /**
     * 获取当前时间  格式:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getTimeByYMDHMS(Date date){
        return formatDate("yyyy-MM-dd HH:mm:ss", date);
    }

    /**
     * 获取当前时间  格式:yyyy-MM-dd
     * @return
     */
    public static String getTimeByYMD(Date date){
        return formatDate("yyyy-MM-dd", date);
    }
    /**
     * 获取当前时间  日期路径:yyyy/MM/dd
     * @return
     */
    public static String getTimeByPath(Date date){
        return formatDate("yyyy/MM/dd", date);
    }

    /**
     * 解析时间格式
     * @param date
     * @return
     */
    public static Date parseDate(String date) {
        if (date == null) {
            return null;
        }
        return matchDateFormatter(parsePatterns, date);
    }

    /**
     * 格式化时间
     *
     * @param date
     * @return
     */
    public static String formatDate(String format, Date date) {
        if (date == null) {
            return null;
        }
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 解析常用的时间格式
     * @param parsePatterns
     * @param sDate
     * @return
     */
    public static Date matchDateFormatter(String[] parsePatterns, String sDate) {
        for (String pattern : parsePatterns) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            df.setLenient(false);//设置解析日期格式是否严格解析日期
            ParsePosition pos = new ParsePosition(0);
            Date date = df.parse(sDate, pos);
            if (date != null) {
                return date;
            }
        }
        return null;
    }

    /**
     * 获取服务器启动时间 (Date型)
     */
    public static Date getServerStartTime()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算相差天数
     */
    public static int calculateDifferentDays(Date date1, Date date2)
    {
        return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
    }

    /**
     * 计算两个时间差
     */
    public static String calculateTwoTimeDifferences(Date endDate, Date frontDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - frontDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * LocalDateTime ==> Date
     */
    public static Date localDateTimeToDate(LocalDateTime temporalAccessor)
    {
        ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     * LocalDate ==> Date
     */
    public static Date localDateToDate(LocalDate temporalAccessor)
    {
        LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
        ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /*
    ---------------------------------------分割--------------------------------------------
     */

    /**
     * 二分查找(int)
     *
     * @param arr
     * @param target
     * @return 返回目标值下标  -1:未找到
     */
    public static int BinarySearch(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int middle = (left + right) / 2;
            if (arr[middle] > target) {
                right = middle - 1;
            } else if (arr[middle] < target) {
                left = middle + 1;
            } else {
                return middle;
            }
        }
        return -1;
    }

    /**
     * 二分查找(double)
     *
     * @param arr
     * @param target
     * @return 返回目标值下标  -1:未找到
     */
    public static int BinarySearch(double[] arr, double target) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int middle = (left + right) / 2;
            if (arr[middle] > target) {
                right = middle - 1;
            } else if (arr[middle] < target) {
                left = middle + 1;
            } else {
                return middle;
            }
        }
        return -1;
    }

    /**
     * 冒泡排序(int)从大到小
     *
     * @param arr
     */
    public static void bubbleSort(int[] arr) {
        //冒泡排序
        for (int x = 0; x < arr.length; x++) {
            //内部比较
            for (int y = 0; y + 1 < arr.length - x; y++) {
                if (arr[y] < arr[y + 1]) {
                    int temp = arr[y];
                    arr[y] = arr[y + 1];
                    arr[y + 1] = temp;
                }
            }
        }
    }

    /**
     * 冒泡排序(double)从大到小
     *
     * @param arr
     */
    public static void bubbleSort(double[] arr) {
        //冒泡排序
        for (int x = 0; x < arr.length; x++) {
            //内部比较
            for (int y = 0; y + 1 < arr.length - x; y++) {
                if (arr[y] < arr[y + 1]) {
                    double temp = arr[y];
                    arr[y] = arr[y + 1];
                    arr[y + 1] = temp;
                }
            }
        }
    }

}

你可能感兴趣的:(java)