Simple date format. Date parsing - android

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

Related

Android dateformat to string issue

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

ParseException: Unparseable date: "Wed, 22 Apr 2015 05:45:42 GMT"

I'm tryint to transform "Wed, 22 Apr 2015 05:45:42 GMT" to "yyyy MM dd - HH:mm"
But that string does not fit in all these simpledateformats, it gives exception in all three
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'Z'");
I can reproduce your problem since I am sitting in a non-english-speaking country, too. The solution is to specify English as language because your input contains an English word/abbreviation (look at the day of week "Wed"!). Following solution works:
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
Date d = sdf.parse("Wed, 22 Apr 2015 05:45:42 GMT");
SimpleDateFormat out = new SimpleDateFormat("yyyy MM dd - HH:mm", Locale.ROOT);
System.out.println(out.format(d)); // output in the system timezone
It does not matter what your app is for (worldwide or not). The only fact which matters is the language of your text input. If this is varying in language then and only then you have first to determine the language of the device in question (probably Locale.getDefault()) and set it on your SimpleDateFormat-object.
Use small z in date format
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'z'");
I have checked it with below code, it is working.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
try {
Date date =simpleDateFormat.parse("Wed, 22 Apr 2015 05:45:42 GMT");
Log.e(MainActivity.class.getSimpleName(),"date : "+date.toString());
} catch (ParseException e) {
e.printStackTrace();
}

Parse SimpleDateFormat of RSS

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();
}

Converting String value date into Date using simple date format in android

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

Android Date and Time Format

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);

Categories

Resources