非法的日期格式Java



我正在尝试向API发送一个参数,该参数需要>=2014-01-02T08:12:32Z之类的日期时间(来自文档(。

要有这种格式,就我所知,我必须使用这样的东西:

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

但我从API收到一个错误,说日期无效。

生成日期如下:

2021-05-21T20:08:14+02

就我所见,唯一的格式差异是后面的Z,我认为它是时区。。。我错过了什么?我该怎么做才能得到那个拖尾的";Z〃;?

tl;dr

java.time.Instant         // Represent a moment as seen with an offset-from-UTC of zero hours-minutes-seconds.
.now()                    // Capture the current moment.
.truncatedTo(             // Lop off any fractional second.
ChronoUnit.SECONDS    // An enum specifying the granularity of truncation.
)                         // Returns another `Instant` object rather than altering the original, per immutable-objects pattern.
.toString()               // Generate text representing the content of this object, using standard ISO 8601 format.

2021-05-25T01:05:03Z

详细信息

避免使用传统日期时间类

您使用的是糟糕的日期-时间类,这些类在几年前被现代的java.time类所取代。

java.time

要捕捉UTC中的当前时刻,请使用Instant.now

因此:

Instant.now().toString()

…就是你所需要的。

请在IdeOne.com上实时查看此代码。

2021-05-25T01:05:03.208937Z

末尾的Z表示与UTC的偏移量为零小时分秒。发音为"祖鲁"。相当于+00:00

截断

如果你只想要整秒,去掉小数秒,就截断。

Instant.now().truncatedTo( ChronoUnit.SECONDS ).toString() 

查看IdeOne.com上实时运行的代码。

2021-05-25T01:05:03Z

Z时区,但收件人可能只支持Z。这并非闻所未闻。

要调整时间,只需在格式化程序中指定所需的时区。

// Simulate question's time zone
TimeZone.setDefault(TimeZone.getTimeZone("GMT+02"));
// Set `when` and show it for proof
Date when = Date.from(OffsetDateTime.parse("2021-05-21T20:08:14+02").toInstant());
System.out.println(when);
// Output: Fri May 21 20:08:14 GMT+02:00 2021
// See that question code generates same output
System.out.println(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX").format(when));
// Output: 2021-05-21T20:08:14+02
// Add desired time zone to formatter
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(fmt.format(when));
// Output: 2021-05-21T18:08:14Z   (note how hour was changed from 20 to 18)

但是我建议您开始使用Java 8中添加的Time API。

// Set `when` and show it for proof
OffsetDateTime when = OffsetDateTime.parse("2021-05-21T20:08:14+02");
System.out.println(when);
// Output: 2021-05-21T20:08:14+02:00
// See same output as question
System.out.println(when.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX")));
// Output: 2021-05-21T20:08:14+02
// Option 1: Add desired time zone to formatter
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX")
.withZone(ZoneOffset.UTC);
System.out.println(fmt.format(when));
// Output: 2021-05-21T18:08:14Z
// Option 2: Change time zone of value
OffsetDateTime whenUTC = when.atZoneSameInstant(ZoneOffset.UTC).toOffsetDateTime();
System.out.println(whenUTC);
// Output: 2021-05-21T18:08:14Z

您可以在这里看到一个示例https://howtodoinjava.com/java/date-time/java-date-formatting/

import java.text.SimpleDateFormat;
import java.util.Date;

public class JavaDateValidations 
{
public static final String TIMESTAMP_PATTERN 
= "yyyy-MM-ddTHH:mm:ss"; 

public static void main(String[] args) 
{
SimpleDateFormat sdf = new SimpleDateFormat(TIMESTAMP_PATTERN);

Date date = new Date();

String formattedDate = sdf.format(date);
System.out.println(formattedDate);      //2020-05-09T00:32:28
}
}