Java parsing date with timezone of string

I want to parse a timezone date from a string in a format "31-12-2014 18:09 +05:30". I tried parsing with simple-Date-Format using "d-MM-yyyy HH:mm ZZ"and "d-MM-yyyy HH:mm Z". But that gives me an exception of an exception date. How to do it? Please help me.

+4
source share
4 answers
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm XXX");
Date d = sdf.parse("31-12-2014 18:09 +05:30");
System.out.println(d);

Please note that you cannot use Xuntil SimpleDateFormat JDK7 , as it is an ISO 8601 time zone format.

Java 6 ZZZ, +05: 30, Z RFC 822

Java 6, : , ISO 8601, java.util.Date

+4

X Z ZZ, :

String str = "31-12-2014 18:09 +05:30";
DateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm X");
System.out.println(format.parse(str));
Output:
Wed Dec 31 18:39:00 IST 2014
+1

: -, d . -, X Z . X , . . . http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

"dd-MM-yyyy HH:mm X"
+1

Others have indicated an option Xin Java SE 7. If you, however, have an older version of Java, then you can change the time zone part to +0530, then it will work with Z(which is available even in Java SE 1.4).

+1
source

Source: https://habr.com/ru/post/1569546/


All Articles