I have time stamp in String format as \"Thu, 23 Feb 2017 04:56:38 GMT\". So, how can convert this string time stamp into \"2017-02-23 04:56:38.0\" format. I tried link java
I am aware that you have got it to work now. Please allow me to mention anyway: Your format is built-in as java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME
. So parsing your string is a one-liner:
Instant i = OffsetDateTime.parse(str, DateTimeFormatter.RFC_1123_DATE_TIME).toInstant();
Since you were asking for a java.sql.Timestamp
in your title, I was thinking that you might need this for your database? A modern JDBC driver (version 4.2 or newer) should be able to accept an Instance
directly. If you are not that lucky, it’s still easy:
Timestamp ts = Timestamp.from(i);
I am using some of the classes in the java.time
package (with subpackage java.time.format
). The above is just a very simple example of where these are more programmer-friendly than the old classes Timestamp
and SimpleDateFormat
(from Java 1.0 or 1.1, I think). I keep meeting examples every time I use them (and certainly when I answer quetions on Stack Overflow about tricky bugs in using the old classes).
java.time
is standard in Java 8. If you want to use the classes in Java 6 or 7, get them in the ThreeTen Backport.
Just change the sdf1 format
SimpleDateFormat sdf1 = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z");
Here z
means
Date or Time Component Presentation Examples
Time zone General time zone Pacific Standard Time; PST; GMT-08:00