ISO 8601 datetime现在 [英] ISO 8601 datetime now

查看:334
本文介绍了ISO 8601 datetime现在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这个格式在java中,在C#中是
DateTime.Now.ToString(o) DateTime.Now.ToString(o)的样本返回日期


2016-03-10T11:24:59.7862749 + 04:00


然后在 sql中插入为


2016-03-10 11:24:59.786


我试图从java插入相同的日期格式。我使用它:

  TimeZone tz = TimeZone.getTimeZone(UTC); 
DateFormat df = new SimpleDateFormat(yyyy-MM-dd'THH:mmZ);
df.setTimeZone(tz);
String nowAsISO = df.format(new Date());

并返回此


2016-03-10T07:29 + 0000


因为这种格式,所以它是错误的。如何将格式更改为我想要的格式?

解决方案

对于Java 7,您可以使用:

  SimpleDateFormat df = new SimpleDateFormat(yyyy-MM-dd'THH:mm:ss.SSSXXX); 
System.out.println(df.format(new Date()));

这将使用模式符号XXX,它也会在偏移量内打印冒号。但是,对于Java-6,此功能不提供。而且精度总是被限制在毫秒之内。



对于Java-8,您还可以使用:

  DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME; 
System.out.println(OffsetDateTime.now()。format(dtf)); // 20163-10T08:46:44.849 + 01:00

这样可以实现纳秒精度一个时钟可用(从Java-9开始)。



对于Java-6 可以应用基于 SimpleDateFormat 或使用外部库:

  // Java-6(SimpleDateFormat)
SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd'T'HH:mm:ss.SSSZ);
String text = sdf.format(new Date());
text = text.substring(0,text.length() - 2)+:+ text.substring(text.length() - 2);
System.out.println(text);

// Joda-Time
DateTime now = DateTime.now();
System.out.println(DateTimeFormat.forPattern(yyyy-MM-dd'THH:mm:ss.SSSZZ)。print(now));

// Time4J
现在时刻= SystemClock.currentMoment();
System.out.println(Iso8601Format.EXTENDED_DATE_TIME_OFFSET.withStdTimezone()。format(now));


I need exactly that format in java which in C# is DateTime.Now.ToString("o"). Sample returned date for DateTime.Now.ToString("o") is

2016-03-10T11:24:59.7862749+04:00

and then in sql it's inserted as

2016-03-10 11:24:59.786

I'm trying to insert same date format from java. I use that:

TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
df.setTimeZone(tz);
String nowAsISO = df.format(new Date());

and it returns this

2016-03-10T07:29+0000

Because of that format then it goes in error. How can I change format to be exactly which I want?

解决方案

For Java 7 you can use:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
System.out.println(df.format(new Date()));

This uses the pattern symbol XXX which will print the colon inside the offset, too. However, for Java-6 this feature is not offered. And the precision is always constrained to milliseconds.

For Java-8, you can also use:

DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
System.out.println(OffsetDateTime.now().format(dtf)); // 2016-03-10T08:46:44.849+01:00

This enables nanosecond precision if such a clock is available (starting with Java-9).

For Java-6 either apply a hack based on SimpleDateFormat or use external libraries:

// Java-6 (SimpleDateFormat)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String text = sdf.format(new Date());
text = text.substring(0, text.length() - 2) + ":" + text.substring(text.length() - 2);
System.out.println(text);

// Joda-Time
DateTime now = DateTime.now();
System.out.println(DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZ").print(now));

// Time4J
Moment now = SystemClock.currentMoment();
System.out.println(Iso8601Format.EXTENDED_DATE_TIME_OFFSET.withStdTimezone().format(now));

这篇关于ISO 8601 datetime现在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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