![](/img/trans.png)
[英]How to resolvejava.text.ParseException: Unparseable date: “Sat Sep 14 2019 00:00:00 GMT+0530 (India Standard Time)”?
[英]how to parse the following date format Mon Nov 09 2015 00:00:00 GMT+0530 (India Standard Time) in java
我得到的日期格式為Mon Nov 09 2015 00:00:00 GMT+0530
(印度標准時間)。 我必須將其轉換為dd/MM/yyyy
格式的 sql 日期。 請幫忙。
我試過下面的代碼但沒有幫助
Date date = new java.sql.Date(
(new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zz (zzzz)").parse("Mon Nov 09 2015 00:00:00 GMT+0530 (India Standard Time)"))
.getTime());
類似於下面的內容,也可以參考這里的Java SimpleDateFormat
public static void main(String[] args) throws ParseException {
String time = "Mon Nov 09 2015 00:00:00 GMT+0530 (India Standard Time)";
DateFormat inputFormat = new SimpleDateFormat(
"E MMM dd yyyy HH:mm:ss 'GMT'z", Locale.ENGLISH);
Date date = inputFormat.parse(time);
System.out.println(date);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
System.out.println(formatter.format(date));
}
輸出
Mon Nov 09 00:00:00 IST 2015
09/11/2015
java.util
日期時間 API 及其格式化 API SimpleDateFormat
已過時且容易出錯。 建議完全停止使用它們並切換到現代日期時間 API * 。
由於您的日期時間字符串具有時區信息, ZonedDateTime
其解析為ZonedDateTime
,您可以將其轉換為其他java.time類型,並在需要時將其格式化為所需的格式。
演示:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.TextStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "Mon Nov 09 2015 00:00:00 GMT+0530 (India Standard Time)";
DateTimeFormatter dtfInput = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("E MMM d uuuu H:m:s")
.appendLiteral(" ")
.appendZoneId()
.appendPattern("X")
.appendLiteral(" ")
.appendLiteral("(")
.appendGenericZoneText(TextStyle.FULL)
.appendLiteral(')')
.toFormatter(Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse(strDateTime, dtfInput);
OffsetDateTime odt = zdt.toOffsetDateTime();
System.out.println(odt);
// To LocalDate, LocalDateTime etc.
LocalDate localDate = odt.toLocalDate();
LocalDateTime localDateTime = odt.toLocalDateTime();
System.out.println(localDate);
System.out.println(localDateTime);
// Get the formatted string
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("dd/MM/uuuu", Locale.ENGLISH);
String formatted = dtfOutput.format(localDate);
System.out.println(formatted);
// In UTC
odt = odt.withOffsetSameInstant(ZoneOffset.UTC);
System.out.println(odt);
}
}
輸出:
2015-11-09T00:00+05:30
2015-11-09
2015-11-09T00:00
09/11/2015
2015-11-08T18:30Z
從Trail: Date Time 中了解有關現代日期時間 API 的更多信息。
OffsetDateTime
支持: 從 JDBC 4.2 開始,您可以直接在代碼中使用OffsetDateTime
例如
PreparedStatement st = conn.prepareStatement("INSERT INTO mytable (columnfoo) VALUES (?)");
st.setObject(1, odt);
st.executeUpdate();
st.close();
JDBC Java SE 8 (JDBC 4.2) 中對java.time API 的支持不需要任何公共 JDBC API 更改。 setObject
和getObject
方法根據以下映射支持java.time類型:
PostgreSQL™ 注意事項:不支持ZonedDateTime
、 Instant
和OffsetTime
/ TIME WITH TIME ZONE
。 此外, OffsetDateTime
實例需要采用 UTC(偏移量為 0)。
* 出於任何原因,如果您必須堅持使用 Java 6 或 Java 7,您可以使用ThreeTen-Backport,它將大部分java.time功能向后移植到 Java 6 和 7。如果您正在為 Android 項目和您的 Android API 工作級別仍然不符合 Java-8,請檢查 通過 desugaring和How to use ThreeTenABP in Android Project 可用的 Java 8+ APIs 。
我現在已經處理過這樣的事情,這是我的解決方案:
String dateString = new SimpleDateFormat("dd/MM/yyyy").format(timestamp.toDate()).toString();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.