java中时间戳和Date相互转换

日期–>时间戳

    @Test
    public void Date2TimeStamp() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        try {
            String valueOf = String.valueOf(sdf.parse(format).getTime() / 1000);
            System.err.println(valueOf);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

时间戳—>日期字符串—>日期类型

    @Test
    public void TimeStamp2Date() {
        String timestampString = "1556169710";
        String formats = "yyyy-MM-dd HH:mm:ss";
        Long timestamp = Long.parseLong(timestampString) * 1000;
        //日期格式字符串
        String dateStr = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
        System.err.println(dateStr);
        Date date = null;
        SimpleDateFormat formater = new SimpleDateFormat();
        formater.applyPattern("yyyy-MM-dd HH:mm:ss");
        try {
            date = formater.parse(dateStr);
            System.err.println(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(java技术,时间戳转换日期)