一些接口为了防止重放攻击,会增加时间戳参数timestamp,格式化显示:2024-01-01T00:00:00Z。

以为是简单的时间戳格式化就行,结果试了几次。。。

测试结果如下:只有最后一种输出满足条件

SimpleDateFormat simpleDateFormat2= new SimpleDateFormat("yyyy-MM-ddTHH:mm:ssZ");
        System.out.println(simpleDateFormat2.format(new Date())); // 会报错  Illegal pattern character 'T'

        SimpleDateFormat simpleDateFormat1= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        System.out.println(simpleDateFormat1.format(new Date())); // 输出 2025-10-21T12:06:44+0800

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        System.out.println(simpleDateFormat.format(new Date())); // 2025-10-21T12:06:44Z

注意:T一定要用单引号;后面的Z也需要用单引号。

那这里的T和Z又是什么意思呢?

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐