我想将多个字符串日期格式转换为时间戳
例如,我有以下日期字符串,如下所示
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
你能帮我这个忙吗?任何信息都将非常有帮助。我无法定义解析器格式的单个字符串,因为在我的方案中字符串格式是不可预测的。
您可以为缺少的部分使用可选的图案(在方括号内)。您还可以使用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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句