I have an app that read a rss from a website (Tue, 17 Mar 2015 12:41:41 +0000). I get this format date (Tue Mar 17 12:41:41 GMT 2015) and I want to obtain this: Tuesday 17 March in spanish Martes, 17 Marzo.
I'm trying differents forms, but I can't parse de date.
This is my code:
DateFormat formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy kk:mm:ss Z", Locale.ENGLISH);
The porcion code where I get the data of the xml parse:
else if(name.equalsIgnoreCase("pubDate")){
noticia.setFecha(formatter.parse(""+property.getFirstChild().getNodeValue()));
The following code snippet parses the English date Tue, 17 Mar 2015 12:41:41 +0000 and outputs it in a Spanish representation Martes, 17 Marzo
try {
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd MMMM yyyy kk:mm:ss", Locale.ENGLISH);
Date date = sdf.parse("Tue, 17 Mar 2015 12:41:41 +0000");
SimpleDateFormat sdf2 = new SimpleDateFormat("EEEE, dd MMMM", new Locale("es","ES"));
System.out.println("Date: " + sdf2.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Related
I want to convert : Wed Apr 06 09:37:00 GMT+03:00 2016 to 02/02/2012.
I did
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = sdf.parse(mydate);
but its give error Unparseable date: "Wed Apr 06 09:37:00 GMT+03:00 2016" (at offset 0) any idea how i can parse it ?
Try this:
String mydate = "Wed Apr 06 09:37:00 GMT+03:00 2016";
SimpleDateFormat src = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
SimpleDateFormat dest = new SimpleDateFormat("dd/MM/yyyy");
Date date = null;
try {
date = src.parse(mydate);
} catch (ParseException e) {
//handle exception
}
String result = dest.format(date);
Output :
06/04/2016
PS:
Z
time zone (RFC 822)
(Time Zone)
Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00
I try to parse date like: Tue, 08 Sep 2015 12:32:55 +0300 and appy next pattern:SimpleDateFormat inputFormat = new SimpleDateFormat("EEE, d MMM yyyy hh:mm:ss zzzz");
But I catch exception java.text.ParseException: Unparseable date: "Sun, 13 Sep 2015 17:25:26 +0300" (at offset 0)
Try changing the code to
SimpleDateFormat inputFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzzz", Locale.US);
Edit:
Additional amendment I have changed hh in your code to HH for 24 hours format
"Fri, 12 Sep 2014 05:00:23 GMT",what's wrong with SimpleDateFormat?
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.parse(dateString);
format: "EEE, dd MMM yyyy HH:mm:ss 'GMT'"
dateString:"Fri, 12 Sep 2014 05:00:23 GMT"
what's wrong ?
Assuming your problem is that the date is coming out wrong, the SimpleDateFormat conversion specifier for timezone is z rather than 'GMT' (although you can use Z and X for the other two variants, RFC822 and ISO8601 respectively).
With your specifier, I get a local time of 5am. If I use the correct specifier in my code, it works fine:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main (String[] args) {
String format = "EEE, dd MMM yyyy HH:mm:ss z";
String date = "Fri, 12 Sep 2014 05:00:23 GMT";
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date dt = sdf.parse(date);
System.out.println(dt);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The output of that program (for Perth, Western Australia, which is UTC+8) is, as expected:
Fri Sep 12 13:00:23 WST 2014
See the SimpleDateFormat online documentation for more detail.
If you still have troubles, try removing the individual items from your format and date strings until it starts working. Once you've established the problematic format specifier, it will be easier to track down.
Use "z" in your pattern because GMT is not just a literal but has to be interpreted as timezone offset UTC+00:00. And set the locale to English because you have english abbreviations of day-of-week and month:
String format = "EEE, dd MMM yyyy HH:mm:ss z";
String date = "Fri, 12 Sep 2014 05:00:23 GMT";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
I have a string variable which contains date and time in the following format
EDIT:
String newdate="Tue Mar 04 16:58:00 GMT+05:30 2014";
I need to covert and parse it into this date format using simpledateformat as below:
Date nd=04-03-2014 16:58:00
But I don't know the string pattern for converting using simpledateformat.
I have given as
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-dd-MM hh:mm:ss");
But it gave me wrong output.
Is the string pattern for simpledateformat sdf correct? If I'm wrong can someone please correct me in converting newdate to date nd format.
Try to use
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
But before conversion replace "." on ":" in "GMT+05.30"
Example:
String newdate="Tue Mar 04 16:58:00 GMT+05:30 2014";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
try {
Date d = sdf.parse(newdate);
} catch (ParseException e) {
e.printStackTrace();
}
Try
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-YYYY hh:mm:ss");
Your date format is not correct.
Try:
"Tue Mar 04 16:58:00 GMT+05:30 2014";
instead of:
"Tue Mar 04 16:58:00 GMT+05.30 2014";
It seems that SimplaDateFormat this GMT+05:30 part works only with ':'.
Than you can parse it like that:
String newdate="Tue Mar 04 16:58:00 GMT+05:30 2014";
SimpleDateFormat sdf=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy" );
Date nd = sdf.parse(newdate);
First of all, your date string is not correct, which should be
String newdate="Tue Mar 04 16:58:00 GMT+05:30 2014";
not this...
String newdate="Tue Mar 04 16:58:00 GMT+05.30 2014";
you placed Dot (.) in GMT+05.30 instead of Colon (:) as GMT+05:30.
Now, Use this format...
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
For Exmaple:
Date date;
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
date = formatter.parse(newdate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String convertedDate = new SimpleDateFormat("dd-MM-yyyy HH:mm").format(date);
Log.i("ConvertedDate", convertedDate);
OutPut:
03-04 19:39:49.959: I/ConvertedDate(310): 04-03-2014 17:28
I have a sample date format Tue, 23 Aug 2011 18:33:43 +0000
In Android, I am using this statement String now = new Date().toGMTString();
And I am receiving this 23 Aug 2011 18:03:25 GMT
What I need to do to get this format Tue, 23 Aug 2011 18:33:43 +0000?
You can use SimpleDateFormat to parse and format dates.
Use SimpleDateFormat.
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
String formattedDate = dateFormat.format(yourDateInstance);
Follow #MByD, except try this:
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT");
String formattedDate = dateFormat.format(yourDateInstance);