美文网首页
Java 关于String,Date,LocalDate,时间戳

Java 关于String,Date,LocalDate,时间戳

作者: BestFei | 来源:发表于2020-06-22 16:42 被阅读0次

String 转 Date

    public Date string2Date(String date){
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try{
            return format.parse(date);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

13位时间戳 转 String

    public String timeStamp2String(String timeStamp,String format) {
        if(timeStamp == null || timeStamp.isEmpty() || timeStamp.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(timeStamp)));
    }

10位时间戳 转 String

    public String timeStamp2String(String timeStamp,String format) {
        if(timeStamp == null || timeStamp.isEmpty() || timeStamp.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(timeStamp+"000")));
    }

Date 转 LocalDate

    public LocalDate date2LocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalDate();
    }

时间戳 转 LocalDate

    public LocalDate timeStamp2LocalDate(String timeStamp,String format){
        return date2LocalDate(string2Date(timeStamp2String(timeStamp,format)));
    }

相关文章

网友评论

      本文标题:Java 关于String,Date,LocalDate,时间戳

      本文链接:https://www.haomeiwen.com/subject/xlfffktx.html