我有這個代碼:
public static void main(String[] args) throws ParseException {
String fecha = "2022-11-08 10:28:04.282551-06";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
Date e = simpleDateFormat.parse(fecha);
SimpleDateFormat newSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
System.out.println(newSimpleDateFormat.format(e));
}
而這個錯誤:
Exception in thread "main" java.text.ParseException: Unparseable date: "2022-11-08 10:28:04.282551-06"
at java.text.DateFormat.parse(DateFormat.java:366)
at Ejemplos.main(Ejemplos.java:11)
我希望我的新日期是這樣的:2022-11-08 10:28:04.282551 這個點后的值 *282551 *有時可能會更小,例如:*282 *或2825有時沒有點,例如:2022 -11-08 10:28:04-06
uj5u.com熱心網友回復:
強烈建議停止使用過時的Date, Calendar, SimpleDateFormat, ... 類 - 這些已被java.time包和子包中的類替換,如LocalDateTime, ZonedDateTime, DateTimeFormatter, ...
由于我們有一個可變大小的小數部分,我們需要DateTimeFormatterBuilder創建相應的格式化程式來決議字串。在其中,我們正在重用現有的ISO_LOCAL_TIME,它通過接受從無分數到 9 位小數秒的分數來處理要求。
private static final DateTimeFormatter PARSE_FORMATTER =
new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral(' ')
.append(DateTimeFormatter.ISO_LOCAL_TIME) // accepts presence or absence of fraction
.appendOffset(" HHmm", " 00") // accepts -06 and 0530
.toFormatter(Locale.ROOT); // replace as needed
public static OffsetDateTime convert(String str) {
return OffsetDateTime.parse(str, PARSE_FORMATTER);
}
要獲得類似“2022-11-08 10:28:04.282551”格式的字串,我們需要一個額外的格式化程式:
private static final DateTimeFormatter FORMAT_FORMATTER =
DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSS"); // time zone is IGNORED!!
public static String convertToString(OffsetDateTime dateTime) {
return dateTime.format(FORMAT_FORMATTER);
}
前面的兩種方法可以(顯然)在一個方法中合并在一起,但最好使用日期時間物件(ZonedDateTime, OffsetDateTime, LocalDateTime, ...)而不是字串 - 字串只能用于輸入和輸出!
上面的格式化程式在結果中不包括時區資訊,這可能會非常令人困惑(恕我直言) - 請參閱下面的測驗輸出的最后日期。請確保確實需要這樣!
否則,我們可能會將時區資訊轉換OffsetDateTme為ZonedDateTime類似 dateTime.atZoneSameInstant(ZoneId.systemDefault()).format(FORMAT_FORMATTER);
或在輸出格式化程式中包含時區資訊,如底部所示。
測驗上述方法:
public static void main(String[] args) {
// just for testing
Locale.setDefault(Category.DISPLAY, Locale.GERMANY);
Locale.setDefault(Category.FORMAT, Locale.GERMANY);
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
var data = """
2022-11-08 10:28:04.282551-06
2022-11-08 10:28:04.282-06
2022-11-08 10:28:04-06
2022-11-08 10:28:04 02
""".split("\n");
for (var str : data) {
test(str);
}
}
private static void test(String str) {
try {
System.out.printf("%-30s -> %s%n", str, convertToString(convert(str)));
} catch (Exception ex) {
System.err.printf("%-30s -> %s%n", str, ex);
}
}
輸出:
openjdk version "20-ea" 2023-03-21 OpenJDK Runtime Environment (build 20-ea 12-790) OpenJDK 64-Bit Server VM (build 20-ea 12-790, mixed mode, sharing) 2022-11-08 10:28:04.282551-06 -> 2022-11-08 10:28:04.282551 2022-11-08 10:28:04.282-06 -> 2022-11-08 10:28:04.282000 2022-11-08 10:28:04-06 -> 2022-11-08 10:28:04.000000 2022-11-08 10:28:04 02 -> 2022-11-08 10:28:04.000000
如果我們想確保輸出是某個特定的時區,我們可以例如在格式格式化程式上指定它,如下所示:
private static final DateTimeFormatter FORMAT_FORMATTER =
DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSS")
.withZone(ZoneId.of("Europe/Berlin"));
現在輸出將是:
2022-11-08 10:28:04.282551-06 -> 2022-11-08 17:28:04.282551 2022-11-08 10:28:04.282-06 -> 2022-11-08 17:28:04.282000 2022-11-08 10:28:04-06 -> 2022-11-08 17:28:04.000000 2022-11-08 10:28:04 02 -> 2022-11-08 09:28:04.000000
編輯:這個答案中首先出現的決議格式化程式的版本是:
private static final DateTimeFormatter PARSE_FORMATTER =
new DateTimeFormatterBuilder()
.appendPattern("uuuu-MM-dd HH:mm:ss") // date time
.optionalStart()
.appendFraction(ChronoField.MICRO_OF_SECOND, 3, 6, true) // optional fractions
.optionalEnd()
.appendPattern("x") // time zone offset
.toFormatter()
.withLocale(Locale.ROOT); // replace as needed
uj5u.com熱心網友回復:
我找到了解決您問題的方法。首先你需要分割你的字串,然后你可以格式化它。嘗試這個
public static void main(String[] args) throws ParseException {
String fecha = "2022-11-08 10:28:04.282551-06";
String[] parts = fecha.split("\\.");
String part1 = parts[0];
String part2 = parts[1];
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(simpleDateFormat.parse(part1));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/532785.html
