Android date to string conversion issue - android

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

Related

Date parsing issue in 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);

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

Parsing date using SimpleDateFormat to get Time

I am working on an Android application in which I need to parse my date to get my time. I am getting parsing exception when I am using my below code.
Here is the code:
SimpleDateFormat parseFormat = new SimpleDateFormat("dd MM hh:mm:ss yyyy");
SimpleDateFormat printFormat = new SimpleDateFormat("h:mm:ss");
Date date;
try { //selectedDateTime = Fri Jan 15 09:30:44 GMT+04:00 2016
date = parseFormat.parse(selectedDateTime+"");
System.out.println(printFormat.format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here is the exception:
java.text.ParseException: Unparseable date: "Fri Jan 15 09:30:00 GMT+04:00 2016" (at offset 0)
SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Your formatting is off, try this.
Your Strings are wrong. The correct ones are:
SimpleDateFormat parseFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat printFormat = new SimpleDateFormat("HH:mm:ss");
I guess your initial date format is wrong. Can you share the value of selectedDateTime.
I guess it in different format (as per your comment) like Fri Jan 15 09:30:44 GMT+04:00 2016
If so please try this code
SimpleDateFormat parseFormat = new SimpleDateFormat("E MM dd hh:mm:ss z yyyy");
SimpleDateFormat printFormat = new SimpleDateFormat("h:mm:ss");
Date date;
try { //selectedDateTime = Fri Jan 15 09:30:44 GMT+04:00 2016
date = parseFormat.parse(selectedDateTime+"");
System.out.println(printFormat.format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
update
try {
String dateTest = "Fri Jan 15 09:30:00 GMT+04:00 2016";
SimpleDateFormat formatter = new SimpleDateFormat(
"EEE MMM dd hh:mm:ss zzz yyyy");
java.util.Date d = (java.util.Date) formatter.parse(dateTest);
System.out.println("" + d);
SimpleDateFormat print = new SimpleDateFormat("hh:mm:ss");
System.out.println(print.format(d));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
output
Fri Jan 15 11:00:00 IST 2016
11:00:00
Following code can help you understand how exactly formatting syntax change for date formatting
Calendar calendar = Calendar.getInstance();
System.out.println("Date: \t"+calendar.getTimeInMillis());
String dateAsText_ = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(Long.parseLong(""+calendar.getTimeInMillis())));
Date date = new Date(Long.parseLong(""+calendar.getTimeInMillis()));
System.out.println(""+dateAsText_);
SimpleDateFormat ft = new SimpleDateFormat ("dd-MMM-yyyy");
System.out.println(""+ft.format(date));
SimpleDateFormat time_ft = new SimpleDateFormat ("hh:mm aa");
System.out.println(""+time_ft.format(date));
Output
Date: 1452766759531
2016-01-14 15:49:19
14-Jan-2016
03:49 PM

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