Red Shaman
Red Shaman

Reputation: 139

Converting time to local time

I want to convert time from RSS feed into local time. Here is how it looks in RSS feed:

<pubDate>Sat, 16 Dec 2017 22:13:00 +0000</pubDate>

What I want to do is getting that 22:13 from there and converting it into local time. Here is the code:

if (current.getNodeName().equalsIgnoreCase("pubDate")) {
                                SimpleDateFormat inputFormat = new SimpleDateFormat("dd MMM yyyy", Locale.US);
                                Calendar cal = Calendar.getInstance();
                                cal.setTime(inputFormat.parse(currentTextContent.substring(5, 16)));
                                SimpleDateFormat outputFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.US); // 01-12
                                outputFormat.setTimeZone(TimeZone.getDefault());
                                String postTime = outputFormat.format(currentTextContent.substring(17, 22));
                                rssFeedModelItem.setPubDate(outputFormat.format(cal.getTime()));
                                System.out.println("Time"+" "+postTime);
                                rssFeedModelItem.setPubTime(postTime);
                            }

With this code app is giving no Internet connection error which is strange. After removing this:

String postTime = outputFormat.format(currentTextContent.substring(17, 22));

And changing

rssFeedModelItem.setPubTime(postTime);

into this:

rssFeedModelItem.setPubTime(currentTextContent.substring(17, 22));

the app is working but showing RSS time. I also tried this solution but it's not worked. Any help is appreciated. Thanks!

Upvotes: 0

Views: 62

Answers (1)

elmorabea
elmorabea

Reputation: 3263

You can try to parse the whole date with this pattern

new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z")

Upvotes: 1

Related Questions