将当前时间转换为此格式:"2017-04-25T17:12:42+01:00" [英] Converting current time to this format: "2017-04-25T17:12:42+01:00"

查看:0
本文介绍了将当前时间转换为此格式:"2017-04-25T17:12:42+01:00"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 "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

这篇关于将当前时间转换为此格式:"2017-04-25T17:12:42+01:00"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆