使用 java 从 API 回复中删除秒数
Remove seconds from an API reply using java
In the code below. String str; is a reply from an API. So I cant change it. But I want to remove the seconds part from that. Otherwise I'll have to send a request to that API every single second to keep my Time widget updated. I also contacted the API developers if there is any parameter to get timestamp in a specific format. They said no.
Main() {
//assume that timestamp reply from the API is being stored in 'str'
//now we've to remove :55 from str
String str = "Mon, 18 Mar 2019 03:43:55 GMT";
Date date=null;
try {
date=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm z").parse(str);
}
catch (ParseException e) {
e.printStackTrace();
}
}
Getting an exception: Unparseable date
java.text.ParseException: Unparseable date: "Mon, 18 Mar 2019 03:43:55 GMT"
at java.text.DateFormat.parse(Unknown Source)
at Main.<init>(Main.java:9)
at Main.main(Main.java:17)
API's Support team reply:
This isn't really a question about geocoding, but there are two ways you could parse the date.
1. use a regular expression.
2. you might have noticed in the "timestamp" section there is also a created_unix which contains a unixtime.
I have no idea which programming language you are using, but I am sure there are standard modules for converting unixtime to whatever format you would like
我不知道您的最终目标是实际生成一个删除了秒部分的日期对象,还是您只是想更改时间戳的表示以删除秒部分。对于后一个要求,一个简单的正则表达式替换可以在这里工作:
String str = "Mon, 18 Mar 2019 03:43:55 GMT";
str = str.replaceAll("(?<=\d):\d{2}(?=\s)", "");
System.out.println(str);
Mon, 18 Mar 2019 03:43 GMT
这应该适用于这个例子;我不知道您是否需要任何时间戳的通用版本——这是可能的,但有点复杂。
Main() {
//assume that timestamp reply from the API is being stored in 'str'
//now we've to remove :55 from str
String str = "Mon, 18 Mar 2019 03:43:55 GMT";
str = str.replace(":55", "");
Date date=null;
try {
date=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm z").parse(str);
}
catch (ParseException e) {
e.printStackTrace();
}
}
java.time 和正确的日期时间对象而不是字符串
在您的程序内部,不要将时间点表示为字符串。使用适当的日期时间对象。解析字符串得到一个:
String str = "Mon, 18 Mar 2019 03:43:55 GMT";
// Parse for internal use
Instant when = DateTimeFormatter.RFC_1123_DATE_TIME.parse(str, Instant::from);
System.out.println(when);
2019-03-18T03:43:55Z
您从 API 获得的字符串采用标准 RFC 1123 / RFC 822 格式(包括秒数,并且它们不接受违反标准是正确的(尽管 ISO 8601 格式可能更合适))。 java.time 中内置了一个 RFC 1123 / RFC 822 格式的解析器,使我们无需自己从格式模式字符串构建它。
我假设时间点是这里的重要信息,并丢弃了它在 GMT 中传输的信息(问题中的代码也发生了同样的情况)。如果偏移量值得保留,我们需要一个 OffsetDateTime
而不是 Instant
,并且相同的内置格式化程序仍然有效:
OffsetDateTime whenWithOffset
= OffsetDateTime.parse(str, DateTimeFormatter.RFC_1123_DATE_TIME);
System.out.println(whenWithOffset);
2019-03-18T03:43:55Z
输出中尾部的Z
表示UTC,实际上与GMT相同(理论差异小于一秒)。
我建议您询问的秒数不会对您的程序造成任何损害。我假设您只想避免将它们呈现给您的用户。万一我错了,当你知道如何删除秒数时很容易:
Instant when = DateTimeFormatter.RFC_1123_DATE_TIME.parse(str, Instant::from)
.truncatedTo(ChronoUnit.MINUTES);
2019-03-18T03:43:00Z
现在秒数设置为 0。truncatedTo
可以用与 OffsetDateTime
相同的方式使用。
以用户所在的时区和区域显示给用户,没有秒数
我建议您的用户希望在他或她自己的时区中以他或她的文化习惯的格式查看没有秒的日期和时间。例如:
DateTimeFormatter formatter
= DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
.withLocale(Locale.forLanguageTag("en-IN"));
ZonedDateTime whenInUsersTimeZone = when.atZone(ZoneId.of("Asia/Kolkata"));
String formatted = whenInUsersTimeZone.format(formatter);
System.out.println(formatted);
18/03/19, 9:13 AM
您注意到:
- 时间已从 03:43(格林威治标准时间)转换为 09:13(印度标准时间)。
- 即使你程序中的时间包含秒,也不会显示。
如果你想使用 JVM 的默认格式化语言环境,请使用 .withLocale(Locale.getDefault(Locale.Category.FORMAT))
(省略这部分也完全有效,但我更愿意明确表示我们依赖默认设置)。如果要使用默认时区,请使用 when.atZone(ZoneId.systemDefault())
.
避免日期和 SimpleDateFormat
您使用的日期时间 类 Date
和 SimpleDateFormat
设计不佳且早已过时。相反,我使用 java.time,现代 Java 日期和时间 API。我发现它更好用。
Link
Oracle tutorial: Date Time 解释如何使用 java.time.
In the code below. String str; is a reply from an API. So I cant change it. But I want to remove the seconds part from that. Otherwise I'll have to send a request to that API every single second to keep my Time widget updated. I also contacted the API developers if there is any parameter to get timestamp in a specific format. They said no.
Main() {
//assume that timestamp reply from the API is being stored in 'str'
//now we've to remove :55 from str
String str = "Mon, 18 Mar 2019 03:43:55 GMT";
Date date=null;
try {
date=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm z").parse(str);
}
catch (ParseException e) {
e.printStackTrace();
}
}
Getting an exception: Unparseable date
java.text.ParseException: Unparseable date: "Mon, 18 Mar 2019 03:43:55 GMT"
at java.text.DateFormat.parse(Unknown Source)
at Main.<init>(Main.java:9)
at Main.main(Main.java:17)
API's Support team reply:
This isn't really a question about geocoding, but there are two ways you could parse the date. 1. use a regular expression. 2. you might have noticed in the "timestamp" section there is also a created_unix which contains a unixtime. I have no idea which programming language you are using, but I am sure there are standard modules for converting unixtime to whatever format you would like
我不知道您的最终目标是实际生成一个删除了秒部分的日期对象,还是您只是想更改时间戳的表示以删除秒部分。对于后一个要求,一个简单的正则表达式替换可以在这里工作:
String str = "Mon, 18 Mar 2019 03:43:55 GMT";
str = str.replaceAll("(?<=\d):\d{2}(?=\s)", "");
System.out.println(str);
Mon, 18 Mar 2019 03:43 GMT
这应该适用于这个例子;我不知道您是否需要任何时间戳的通用版本——这是可能的,但有点复杂。
Main() {
//assume that timestamp reply from the API is being stored in 'str'
//now we've to remove :55 from str
String str = "Mon, 18 Mar 2019 03:43:55 GMT";
str = str.replace(":55", "");
Date date=null;
try {
date=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm z").parse(str);
}
catch (ParseException e) {
e.printStackTrace();
}
}
java.time 和正确的日期时间对象而不是字符串
在您的程序内部,不要将时间点表示为字符串。使用适当的日期时间对象。解析字符串得到一个:
String str = "Mon, 18 Mar 2019 03:43:55 GMT";
// Parse for internal use
Instant when = DateTimeFormatter.RFC_1123_DATE_TIME.parse(str, Instant::from);
System.out.println(when);
2019-03-18T03:43:55Z
您从 API 获得的字符串采用标准 RFC 1123 / RFC 822 格式(包括秒数,并且它们不接受违反标准是正确的(尽管 ISO 8601 格式可能更合适))。 java.time 中内置了一个 RFC 1123 / RFC 822 格式的解析器,使我们无需自己从格式模式字符串构建它。
我假设时间点是这里的重要信息,并丢弃了它在 GMT 中传输的信息(问题中的代码也发生了同样的情况)。如果偏移量值得保留,我们需要一个 OffsetDateTime
而不是 Instant
,并且相同的内置格式化程序仍然有效:
OffsetDateTime whenWithOffset
= OffsetDateTime.parse(str, DateTimeFormatter.RFC_1123_DATE_TIME);
System.out.println(whenWithOffset);
2019-03-18T03:43:55Z
输出中尾部的Z
表示UTC,实际上与GMT相同(理论差异小于一秒)。
我建议您询问的秒数不会对您的程序造成任何损害。我假设您只想避免将它们呈现给您的用户。万一我错了,当你知道如何删除秒数时很容易:
Instant when = DateTimeFormatter.RFC_1123_DATE_TIME.parse(str, Instant::from)
.truncatedTo(ChronoUnit.MINUTES);
2019-03-18T03:43:00Z
现在秒数设置为 0。truncatedTo
可以用与 OffsetDateTime
相同的方式使用。
以用户所在的时区和区域显示给用户,没有秒数
我建议您的用户希望在他或她自己的时区中以他或她的文化习惯的格式查看没有秒的日期和时间。例如:
DateTimeFormatter formatter
= DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
.withLocale(Locale.forLanguageTag("en-IN"));
ZonedDateTime whenInUsersTimeZone = when.atZone(ZoneId.of("Asia/Kolkata"));
String formatted = whenInUsersTimeZone.format(formatter);
System.out.println(formatted);
18/03/19, 9:13 AM
您注意到:
- 时间已从 03:43(格林威治标准时间)转换为 09:13(印度标准时间)。
- 即使你程序中的时间包含秒,也不会显示。
如果你想使用 JVM 的默认格式化语言环境,请使用 .withLocale(Locale.getDefault(Locale.Category.FORMAT))
(省略这部分也完全有效,但我更愿意明确表示我们依赖默认设置)。如果要使用默认时区,请使用 when.atZone(ZoneId.systemDefault())
.
避免日期和 SimpleDateFormat
您使用的日期时间 类 Date
和 SimpleDateFormat
设计不佳且早已过时。相反,我使用 java.time,现代 Java 日期和时间 API。我发现它更好用。
Link
Oracle tutorial: Date Time 解释如何使用 java.time.