Reputation: 175
How can I parse a pubDate from a RSS feed to a Date object in java. The format is: Thu, 03 Mar 2016 15:30:00 +0200
I tried to do it by this:
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
//java.util.Date utilDate = new java.util.Date();
java.util.Date utilDate = new java.util.Date();
try {
utilDate = formatter.parse(date);
}
catch (java.text.ParseException p1)
{
p1.printStackTrace();
}
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
but the java.sql.Date only save the date without the time for example: "2016-02-06 00:00:00"
Upvotes: 2
Views: 840
Reputation: 338516
That format of your input string happens to be defined as a standard, RFC 1123.
That format has long been outmoded by the much more sensible formats defined by ISO 8601. If possible I strongly suggest migrating.
You are using old date-time classes that have been outmoded by the java.time framework built into Java 8 and later.
Your input’s format happens to be predefined as a constant in java.time. So no need to specify a formatting pattern.
String input = "Thu, 03 Mar 2016 15:30:00 +0200";
DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME ;
OffsetDateTime odt = OffsetDateTime.parse( input , formatter );
Someday the JDBC drivers will be updated to use the java.time types directly. Until then convert to the java.sql types, specifically java.sql.Timestamp
in our case here. To convert we need an Instant
, a moment on the timeline in UTC. We can extract one from our OffsetDateTime
.
Instant instant = odt.toInstant();
java.sql.Timestamp ts = java.sql.Timestamp.from( instant );
Upvotes: 1
Reputation: 20155
java.sql.Date doesn't have Time
You should probably use java.sql.Timestamp
You can use it like this
new java.sql.Timestamp(utilDate.getTime());
Upvotes: 1