获得ISO 8601格式的当前时刻UTC的最优雅的方式是什么?它看起来应该像:2010-10-12 t8: 50z。

例子:

String d = DateFormat.getDateTimeInstance(DateFormat.ISO_8601).format(date);

当前回答

如果您关心性能,我创建了一个库,它在处理iso8601格式的日期方面优于标准Java解析器和格式化器。DatetimeProcessor实现是线程安全的,可以缓存在并发映射或静态字段中。

<dependency>
  <groupId>com.axibase</groupId>
  <artifactId>date-processor</artifactId>
  <version>1.0.3</version>
</dependency>
import com.axibase.date.DatetimeProcessor;
import com.axibase.date.PatternResolver;
import org.junit.Before;
import org.junit.Test;

import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

public class DateFormatTest {
    private Clock clock;

    @Before
    public void prepare() {
        clock = Clock.fixed(Instant.ofEpochMilli(1571285405300L), ZoneId.of("Europe/Berlin"));
    }

    @Test
    public void testIsoMillis(){
        final DatetimeProcessor formatter = PatternResolver.createNewFormatter("iso");
        assertThat(formatter.print(clock.millis(), ZoneOffset.UTC), is("2019-10-17T04:10:05.300Z"));
    }

    @Test
    public void testIsoMillisLocalZone(){
        final DatetimeProcessor formatter = PatternResolver.createNewFormatter("iso");
        assertThat(formatter.print(clock.millis(), clock.getZone()), is("2019-10-17T06:10:05.300+02:00"));
    }

    @Test
    public void testIsoMinutes(){
        final DatetimeProcessor formatter = PatternResolver.createNewFormatter("yyyy-MM-ddTHH:mmXXX");
        assertThat(formatter.print(clock.millis(), ZoneOffset.UTC), is("2019-10-17T04:10Z"));
    }
}

其他回答

Apache common -lang3中的DateFormatUtils有一些有用的常量,例如:ISO_DATETIME_FORMAT

使用SimpleDateFormat格式化任何日期对象:

TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); // Quoted "Z" to indicate UTC, no timezone offset
df.setTimeZone(tz);
String nowAsISO = df.format(new Date());

如上所示,使用new Date()将格式化当前时间。

如果您不想包含Jodatime(尽管它很好)

javax.xml.bind.DatatypeConverter.printDateTime(
    Calendar.getInstance(TimeZone.getTimeZone("UTC"))
);

返回一个字符串:

2012-07-10T16:02:48.440Z

这与原始请求略有不同,但仍然是ISO-8601。

他们应该添加一些简单的方法来从日期到即时,还有一个叫做toISO8601的方法,这是很多人都在寻找的。 作为对其他答案的补充,从java.util.Date到ISO 8601格式:

Instant.ofEpochMilli(date.getTime()).toString();

当使用自动补全时,它是不可见的,但是: java.time.Instant.toString ():

使用ISO-8601表示该时刻的字符串

对于ISO 8601,您可以使用Java的SimpleDateFormat,格式如下:yyyy-MM-dd'T'HH:mm:ssXXX。

示例代码:(列出所有可用的时区)

for (String timeZone : TimeZone.getAvailableIDs())
{
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
    dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
    String formatted = dateFormat.format(new Date());
    System.out.print(formatted);

    if (formatted.endsWith("Z"))
    {
        // These time zone's have offset of '0' from GMT.
        System.out.print("\t(" + timeZone + ")");
    }

    System.out.println();
}

你可以用:

TimeZone.getDefault()

虚拟机默认时区。更多的在这里

你可能会注意到,很少有时区的日期时间是以Z结尾的。这些时区与GMT的偏移量为“0”。

更多信息可以在这里找到。