java常用工具类

目录

    • 读文件
    • 网络连接
    • Date工具类

读文件

 public static String readFileContent(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }
fileName:传入文件所在路径及文件名称,

网络连接

 public static boolean connect(String host, int port)  {
        Socket socket = new Socket();
        boolean flag = false;
        try {
            socket.connect(new InetSocketAddress(host, port),2000);
            flag = socket.isConnected();
            return flag;
        } catch (IOException e) {
            e.getMessage();
            return flag;
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.getMessage();
            }
        }
    }
fileName:传入文件所在路径及文件名称,

Date工具类

private final static String DEFAULT_FORMAT_STR = "yyyy-MM-dd HH:mm:ss";

    public static Date string2Date(String dateStr, String formatStr) throws ParseException {
        if (formatStr == null) {
            formatStr = DEFAULT_FORMAT_STR;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        Date date = sdf.parse(dateStr);
        return date;
    }

    public static String date2Str(Date date, String formatStr) {
        if (date == null) {
            return null;
        }
        if (formatStr == null) {
            formatStr = DEFAULT_FORMAT_STR;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        String dateStr = sdf.format(date);
        return dateStr;
    }

    public static String seconds2Duration(int seconds) {
        int minute = seconds / 60;
        String minuteStr = "";
        if (minute < 9) {
            minuteStr = "0" + String.valueOf(minute);
        } else {
            minuteStr = minute + "";
        }
        int second = seconds % 60;
        String secondStr = "";
        if (second < 9) {
            secondStr = "0" + String.valueOf(second);
        } else {
            secondStr = second + "";
        }
        String duration = String.format("%s:%s", minuteStr, secondStr);
        return duration;
    }

    /**
     * date是否早于date2
     *
     * @param date1
     * @param date2
     * @return
     * @throws Exception
     */
    public static boolean compareDate(Date date1, Date date2) throws Exception {
        if (date1 == null || date2 == null) {
            throw new Exception("the two date cannot be null");
        }
        return date1.before(date2);
    }

    public static Date getStartTimeOfDate(Date date) {
        Calendar startTime = Calendar.getInstance();
        startTime.setTime(date);
        startTime.set(Calendar.HOUR_OF_DAY, 0);
        startTime.set(Calendar.MINUTE, 0);
        startTime.set(Calendar.SECOND, 0);
        startTime.set(Calendar.MILLISECOND, 0);
        return startTime.getTime();
    }

    public static Date getEndTimeOfAfterDate(Date date, Integer retentionDay) {
        Calendar endTime = Calendar.getInstance();
        endTime.setTime(date);
        endTime.add(Calendar.DATE, retentionDay - 1);
        endTime.set(Calendar.HOUR_OF_DAY, 23);
        endTime.set(Calendar.MINUTE, 59);
        endTime.set(Calendar.SECOND, 59);
        endTime.set(Calendar.MILLISECOND, 999);
        return endTime.getTime();
    }

    public static String booleanIsNull(String s) {
        return s == null ? "" : s;
    }


    public static Date localToUTC(String localTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date localDate = null;
        try {
            localDate = sdf.parse(localTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return localDateToUTC(localDate);
    }

    public static Date localDateToUTC(Date date) {
        long localTimeInMillis = date.getTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(localTimeInMillis);
        int zoneOffset = calendar.get(java.util.Calendar.ZONE_OFFSET);
        int dstOffset = calendar.get(java.util.Calendar.DST_OFFSET);
        calendar.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
        Date utcDate = new Date(calendar.getTimeInMillis());
        return utcDate;
    }
未完待续

你可能感兴趣的:(Java)