java常用工具类

读文件

 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();
            }
        }
    }
传入IP和Port,

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;
    }
	/**
	*  获取文件创建时间
	*/
	  private Long getFileCreateTime(String filePath){
	    File file = new File(filePath);
	    try {
	        Path path= Paths.get(filePath);
	        BasicFileAttributeView basicview= Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS );
	        BasicFileAttributes attr = basicview.readAttributes();
	        return attr.creationTime().toMillis();
	    } catch (Exception e) {
	        e.printStackTrace();
	        return file.lastModified();
	    }
	}
	
	/**
	* UTC时间转标准时间
	*/
	private static String UTCToLocalTime(String utc ){
		String formatPattern = "yyyy-MM-dd HH:mm:ss";
	    ZonedDateTime zdt  = ZonedDateTime.parse(utc);
	    LocalDateTime localDateTime = zdt.toLocalDateTime();
	    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatPattern);
	    return formatter.format(localDateTime.plusHours(8));
	}

未完待续
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值