有关日期时间戳的工具类

有关日期时间戳的工具类

/**
     * 获取字符串时间戳(10位)
     * @return
     */
    public static String queryTime(){
        //获取当前时间的时间戳(毫秒)
        long time = System.currentTimeMillis();
        return String.valueOf(time/1000);
    }

    /**
     * 时间格式的截取
     * 示例:2020-04-12 14:12:20   结果:20200412141220
     * @param time
     * @return
     */
    public static String times(String time){
        String replace = time.replace("-", "").replace(":", "").replace(" ", "");
        return replace;
    }

    /**
     * 字符串格式的日期转换为字符串格式的时间戳
     * 示例:2020-04-12 14:12:20   结果:1586671940
     * @param date
     * @return
     */
    public static String timeStr(String date){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long time = sf.parse(date).getTime();
            return String.valueOf(time/1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "转换失败";
    }
	/**
     * 时间戳转换为日期格式
     * @param time
     * @return
     */
    public static String formatDate(String time){
        if (time == null || time.isEmpty() || time.equals("null")) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date(Long.valueOf(time + "000")));
    }

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

有需要请留言!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值