Date parsing issue in android - android

I am trying to parse a date in android using the following code
String dateString = "Sun Apr 15 13:37:33 CEST 2012";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
System.out.println(sdf.parse(dateString));
The code above works fine with java, but in Android it gives ParseException.
java.text.ParseException: Unparseable date: "Sun Apr 15 13:37:33 CEST 2012" (at offset 20)
What could be the possible reason?
I am running it on Samsung S4, lollipop, system language is French, and system timezone is GMT+2:00.

try this code:
String mytime="Jan 17, 2012";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"MMM dd, yyyy");
Date myDate = null;
try {
myDate = dateFormat.parse(mytime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss
zzz yyyy");
String finalDate = timeFormat.format(myDate);
TextView t = (TextView) findViewById(R.id.txt);
t.setText(finalDate);

Related

Android Convert DateTime to EEE, d MMM yyyy

I am trying to convert the following data time to EEE, d MMM yyyy format but kept getting Unparsable date format. Can some one please help. I couldn't find anything on the web that helped.
Here is the code
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
Date convertedDateStart = new Date();
try {
convertedDateStart = dateFormat.parse(datestr);
camp_new.startdate = convertedDateStart;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Any help wound be greatly appreciated. Thanks in advance
The date string "2017-01-12T00:00:00Z" cannot be parsed with your format "EEE, d MMM yyyy HH:mm:ss Z"; the fields are the wrong type or in the wrong place.
The format for your datestr "2017-01-12T00:00:00Z" should be:
yyyy-MM-dd'T'HH:mm:ssZ
You need to initialize the SimpleDateFormat with this format to parse datestr.
The format "EEE, d MMM yyyy HH:mm:ss Z" would parse a date that looks like
Thu, 1 Jan 2017 00:00:00 Z
Read the docs to understand the difference between parse() and format().
You have to parse it first, store it as a Date object and then use another SimpleDateFormat to format in the desired way.
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);
SimpleDateFormat dateFormat2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
Date date = dateFormat.parse(datestr);
String convertedDateStart = dateFormat2.format(date);
Well This will Work For you if you want to FORMAT and get a Date String with that format..
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
try {
date = dateFormat.parse(datestr);
} catch (ParseException e) {
e.printStackTrace();
}
dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
datestr = dateFormat.format(date);
System.out.println(datestr);
This will give you Thu, 12 Jan 2017 00:00:00 +0430 Date String.

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

Android date to string conversion issue

I want to convert : Wed Apr 06 09:37:00 GMT+03:00 2016 to 02/02/2012.
What i tried
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = sdf.parse(mydate);
and
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) {
Log.d("deneme",e.getMessage());
}
String result = dest.format(date);
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 ?
I think you are trying to format an english locale date but your system locale is not english. So when you create the SimpleDateFormat object specify the Locale explicity.
Try this code,
String mydate = "Wed Apr 06 09:37:00 GMT+03:00 2016";
SimpleDateFormat src = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);
SimpleDateFormat dest = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
Date date = null;
try {
date = src.parse(mydate);
} catch (ParseException e) {
Log.d("Exception",e.getMessage());
}
String result = dest.format(date);
Log.d("result", result);

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

Convert String to date in android

How to convert the string "Thu Jul 18 13:20:12 GMT+05:30 2013" in date in android? Using DateFormatter and SimpleDateFormatter throw an exception saying cannot parse the date format while using it to convert into date.
try {
String dateString = "Thu Jul 18 13:20:12 GMT+05:30 2013";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
Date date = sdf.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
try {
String dateString = "Thu Jul 18 13:20:12 GMT+05:30 2013";
Date date = formatter.parse(dateString);
} catch (Exception e) {
e.printStackTrace();
}
this is how we doing in android..but if you can post your code then can help you precisely
This is how you can do:
try {
String dateString = "Thu Jul 18 13:20:12 GMT+05:30 2013";
Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse(dateString );
}
catch (ParseException e)
{
e.printStackTrace();
}
You can also do this:
SimpleDateFormat dateFormat = new SimpleDateFormat("DD-MM-YYYY");
Date dd = dateFormat.parse("string")

Categories

Resources