如何将多个日期格式字符串转换为时区格式的时间戳
How to convert multiple string of date formats to timestamp with timezone format
我想将多个字符串日期格式转换成时间戳
例如,我有如下日期字符串
String date1 = "2020-01-01 12:23:30.345"
String date2 = "2020-01-01 12:23:30"
String date3 = "2020-01-01 12:23"
String date4 = "2020-01-01 12"
我想把上面所有的字符串格式转换成时间戳,比如,
For date1, Timestamp should be 2020-01-01 12:23:30.345 UTC
For date2, Timestamp should be 2020-01-01 12:23:30.000 UTC
For date3, Timestamp should be 2020-01-01 12:23:00.000 UTC
For date4, Timestamp should be 2020-01-01 12:00:00.000 UTC
你能帮我解决这个问题吗?任何信息都会非常有帮助。我无法为解析器定义格式化的单个字符串,因为在我的场景中字符串格式是不可预测的。
您可以使用两个格式化程序,一个将 String
转换为 LocalDateTime
,另一个将 LocalDateTime
转换为预期的 String
:
String[] dates = {date1, date2, date3, date4};
DateTimeFormatter formatterIn = DateTimeFormatter.ofPattern("yyyy-MM-dd HH[:mm][:ss][.SSS]");
DateTimeFormatter formatterOut = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
for (String date : dates) {
LocalDateTime ldtIn = LocalDateTime.parse(date, formatterIn);
String ldtOut = ldtIn.format(formatterOut);
System.out.println(ldtOut);
}
产出
2020-01-01 12:23:30.345
2020-01-01 12:23:30.000
2020-01-01 12:23:00.000
2020-01-01 12:00:00.000
您可以为缺失的部分使用可选模式(在方括号内)。您还可以使用 DateTimeFormatterBuilder#parseDefaulting
将缺少的时间部分默认为 0
或任何其他值。
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtfForFormating = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
// Tests
System.out.println(getLocalDateTime("2020-01-01 12:23:30.345"));
System.out.println(getLocalDateTime("2020-01-01 12:23:30.345").format(dtfForFormating));
System.out.println(getLocalDateTime("2020-01-01 12:23:30"));
System.out.println(getLocalDateTime("2020-01-01 12:23:30").format(dtfForFormating));
System.out.println(getLocalDateTime("2020-01-01 12:23"));
System.out.println(getLocalDateTime("2020-01-01 12:23").format(dtfForFormating));
System.out.println(getLocalDateTime("2020-01-01 12"));
System.out.println(getLocalDateTime("2020-01-01 12").format(dtfForFormating));
}
static LocalDateTime getLocalDateTime(String text) {
DateTimeFormatter multiFormatter = new DateTimeFormatterBuilder()
.appendPattern("uuuu-MM-dd HH[:mm][:ss][.SSS]")
.parseDefaulting(ChronoField.NANO_OF_SECOND, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.toFormatter(Locale.ENGLISH);
return LocalDateTime.parse(text, multiFormatter);
}
}
输出:
2020-01-01T12:23:30.345
2020-01-01 12:23:30.345
2020-01-01T12:23:30
2020-01-01 12:23:30.000
2020-01-01T12:23
2020-01-01 12:23:00.000
2020-01-01T12:00
2020-01-01 12:00:00.000
更新
这是根据 OP 要求将时区设置为日期时间的更新。
LocalDateTime
没有时区信息。为了获得时区信息,您需要将其转换为 ZonedDateTime
.
演示:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtfForFormating = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS z", Locale.ENGLISH);
// Tests
System.out.println(getZonedDateTime("2020-01-01 12:23:30.345"));
System.out.println(getZonedDateTime("2020-01-01 12:23:30.345").format(dtfForFormating));
System.out.println(getZonedDateTime("2020-01-01 12:23:30"));
System.out.println(getZonedDateTime("2020-01-01 12:23:30").format(dtfForFormating));
System.out.println(getZonedDateTime("2020-01-01 12:23"));
System.out.println(getZonedDateTime("2020-01-01 12:23").format(dtfForFormating));
System.out.println(getZonedDateTime("2020-01-01 12"));
System.out.println(getZonedDateTime("2020-01-01 12").format(dtfForFormating));
}
static ZonedDateTime getZonedDateTime(String text) {
DateTimeFormatter multiFormatter = new DateTimeFormatterBuilder()
.appendPattern("uuuu-MM-dd HH[:mm][:ss][.SSS]")
.parseDefaulting(ChronoField.NANO_OF_SECOND, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.toFormatter(Locale.ENGLISH);
return LocalDateTime.parse(text, multiFormatter).atZone(ZoneId.of("Etc/UTC"));
}
}
输出:
2020-01-01T12:23:30.345Z[Etc/UTC]
2020-01-01 12:23:30.345 UTC
2020-01-01T12:23:30Z[Etc/UTC]
2020-01-01 12:23:30.000 UTC
2020-01-01T12:23Z[Etc/UTC]
2020-01-01 12:23:00.000 UTC
2020-01-01T12:00Z[Etc/UTC]
2020-01-01 12:00:00.000 UTC
我想将多个字符串日期格式转换成时间戳
例如,我有如下日期字符串
String date1 = "2020-01-01 12:23:30.345"
String date2 = "2020-01-01 12:23:30"
String date3 = "2020-01-01 12:23"
String date4 = "2020-01-01 12"
我想把上面所有的字符串格式转换成时间戳,比如,
For date1, Timestamp should be 2020-01-01 12:23:30.345 UTC
For date2, Timestamp should be 2020-01-01 12:23:30.000 UTC
For date3, Timestamp should be 2020-01-01 12:23:00.000 UTC
For date4, Timestamp should be 2020-01-01 12:00:00.000 UTC
你能帮我解决这个问题吗?任何信息都会非常有帮助。我无法为解析器定义格式化的单个字符串,因为在我的场景中字符串格式是不可预测的。
您可以使用两个格式化程序,一个将 String
转换为 LocalDateTime
,另一个将 LocalDateTime
转换为预期的 String
:
String[] dates = {date1, date2, date3, date4};
DateTimeFormatter formatterIn = DateTimeFormatter.ofPattern("yyyy-MM-dd HH[:mm][:ss][.SSS]");
DateTimeFormatter formatterOut = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
for (String date : dates) {
LocalDateTime ldtIn = LocalDateTime.parse(date, formatterIn);
String ldtOut = ldtIn.format(formatterOut);
System.out.println(ldtOut);
}
产出
2020-01-01 12:23:30.345
2020-01-01 12:23:30.000
2020-01-01 12:23:00.000
2020-01-01 12:00:00.000
您可以为缺失的部分使用可选模式(在方括号内)。您还可以使用 DateTimeFormatterBuilder#parseDefaulting
将缺少的时间部分默认为 0
或任何其他值。
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtfForFormating = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
// Tests
System.out.println(getLocalDateTime("2020-01-01 12:23:30.345"));
System.out.println(getLocalDateTime("2020-01-01 12:23:30.345").format(dtfForFormating));
System.out.println(getLocalDateTime("2020-01-01 12:23:30"));
System.out.println(getLocalDateTime("2020-01-01 12:23:30").format(dtfForFormating));
System.out.println(getLocalDateTime("2020-01-01 12:23"));
System.out.println(getLocalDateTime("2020-01-01 12:23").format(dtfForFormating));
System.out.println(getLocalDateTime("2020-01-01 12"));
System.out.println(getLocalDateTime("2020-01-01 12").format(dtfForFormating));
}
static LocalDateTime getLocalDateTime(String text) {
DateTimeFormatter multiFormatter = new DateTimeFormatterBuilder()
.appendPattern("uuuu-MM-dd HH[:mm][:ss][.SSS]")
.parseDefaulting(ChronoField.NANO_OF_SECOND, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.toFormatter(Locale.ENGLISH);
return LocalDateTime.parse(text, multiFormatter);
}
}
输出:
2020-01-01T12:23:30.345
2020-01-01 12:23:30.345
2020-01-01T12:23:30
2020-01-01 12:23:30.000
2020-01-01T12:23
2020-01-01 12:23:00.000
2020-01-01T12:00
2020-01-01 12:00:00.000
更新
这是根据 OP 要求将时区设置为日期时间的更新。
LocalDateTime
没有时区信息。为了获得时区信息,您需要将其转换为 ZonedDateTime
.
演示:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtfForFormating = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS z", Locale.ENGLISH);
// Tests
System.out.println(getZonedDateTime("2020-01-01 12:23:30.345"));
System.out.println(getZonedDateTime("2020-01-01 12:23:30.345").format(dtfForFormating));
System.out.println(getZonedDateTime("2020-01-01 12:23:30"));
System.out.println(getZonedDateTime("2020-01-01 12:23:30").format(dtfForFormating));
System.out.println(getZonedDateTime("2020-01-01 12:23"));
System.out.println(getZonedDateTime("2020-01-01 12:23").format(dtfForFormating));
System.out.println(getZonedDateTime("2020-01-01 12"));
System.out.println(getZonedDateTime("2020-01-01 12").format(dtfForFormating));
}
static ZonedDateTime getZonedDateTime(String text) {
DateTimeFormatter multiFormatter = new DateTimeFormatterBuilder()
.appendPattern("uuuu-MM-dd HH[:mm][:ss][.SSS]")
.parseDefaulting(ChronoField.NANO_OF_SECOND, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.toFormatter(Locale.ENGLISH);
return LocalDateTime.parse(text, multiFormatter).atZone(ZoneId.of("Etc/UTC"));
}
}
输出:
2020-01-01T12:23:30.345Z[Etc/UTC]
2020-01-01 12:23:30.345 UTC
2020-01-01T12:23:30Z[Etc/UTC]
2020-01-01 12:23:30.000 UTC
2020-01-01T12:23Z[Etc/UTC]
2020-01-01 12:23:00.000 UTC
2020-01-01T12:00Z[Etc/UTC]
2020-01-01 12:00:00.000 UTC