Haresh Chaudhary
Haresh Chaudhary

Reputation: 4400

How to convert this type of Date Format in Android

I am receiving this type of Date Wed, 14 Nov 2012 19:26:23 +0000 as a String.

I had tried to convert it as Wed, 14 Nov 2012 using different ways like that of using SimpleDateFormat type of changing the format of the Date but got Parsing Errors.

How would I convert this type of Date so that after converting,I would get
only Wed, 14 Nov 2012 type of Date.

Thanks.

Code that I have tried:

String stringDate="Wed, 14 Nov 2012 19:26:23 +0000";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date parseDate = sdf.parse(stringDate);
String conDateinString = sdf.format(date1);
String lastDate=DateFormat.format("EEE, dd MMM yyyy", new Date(Long.parseLong(conDateinString))).toString();  

stringDate--My first date that is to be parsed.
conDateinString -- converted date in String Format.
lastDate -- final date that I tried to Obtain.

Upvotes: 0

Views: 602

Answers (1)

kamituel
kamituel

Reputation: 35960

Use this format of SimpleDateFormat:

String t = "Wed, 14 Nov 2012 19:26:23 +0000";

// To parse input string
SimpleDateFormat from = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss +0000", Locale.US);

// To format output string
SimpleDateFormat to = new SimpleDateFormat("EEE, dd MMM yyyy", Locale.US);

System.out.println(to.format(from.parse(t)));

The output of this code would be:

Wed, 14 Nov 2012

Upvotes: 2

Related Questions