将当前时间转换为此格式:"2017-04-25T17:12:42+01:00"为此、格式、时间、quot

2023-09-03 12:47:39 作者:爱情已不完美

如何将当前时间转换为此格式:

 "2017-04-25T17:12:42+01:00"

我能得到的最接近的是:

"2017-05-16T19:58:21+0100" 
格式怎么转化

使用以下格式:

SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZ").format(new Date())

谢谢

推荐答案

您应该使用现代的Java 8 java.time包,更具体地说,DateTimeFormatterISO_OFFSET_DATE_TIME。

来自文档:

公共静态最终日期格式ISO_OFFSET_DATE_TIME

ISO日期时间格式化程序,它使用 偏移量,如‘2011-12-03T10:15:30+01:00’。

工作代码:

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main
{
  public static void main(String[] args)
  {
    ZonedDateTime date = ZonedDateTime.now();
    String text = date.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    System.out.println(text);
  }
}

结果:

> run Main
2017-05-19T13:03:16.167+02:00