Parsing date using SimpleDateFormat to get Time - android

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

Related

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

reset time zone offset android

I have a date like
datewithGMT >>
Tue Oct 28 07:06:54 GMT+02:00 2014
and I want to reset it to
Tue Oct 28 07:06:54 2014
Using the following
SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
df.setTimeZone(TimeZone.getDefault());
Date airDate = null;
try {
airDate = df.parse(datewithGMT);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I again get the date with +2 GMT how can this offset be removed
Your pattern does not correspond to the input string. You need to change your df to SimpleDateFormat df = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy");
You also need a second SimpleDateFormat. The completed implementation is below:
SimpleDateFormat df = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy");
df.setTimeZone(TimeZone.getDefault());
SimpleDateFormat df2 = new SimpleDateFormat("EEE dd MMM HH:mm:ss yyyy");
df2.setTimeZone(TimeZone.getDefault());
String datewithGMT = "Tue Oct 28 07:06:54 GMT+02:00 2014";
Date airDate = null;
try {
airDate = df.parse(datewithGMT);
Log.v(df2.format(airDate)); //it will print Tue Oct 28 07:06:54 2014
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

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

SimpleDateFormat parsing troubles

I'm having trouble parsing a date that I'm trying to reformat. The SimpleDateFormat is giving me a real headache.
I'm getting this date from a news feed:
Wed, 06 Mar 2013 09:22:00 +0100
And I need to format it to this:
06.03.2013
I could just use a hashmap with all the months but I would like to use the SimpleDateFormat, since that's what it's for.
But I can't seem to find the right pattern.
Try adding following code snippet:--
//SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss +SSSS");
SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss +SSSS", Locale.ENGLISH);
Date date = null;
try {
date = fmt.parse("Wed, 06 Mar 2013 09:22:00 +0100");//Hardcoded for your case...
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat fmtOut = new SimpleDateFormat("dd.MM.yyyy");
System.out.println("Date :-- " +fmtOut.format(date));

convert String to Date in android

anyone know how to convert the following string to Date?
"Fri Jul 30 16:19:36 GMT+02.00 2021"
i tried:
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
try {String temp = value2[i].trim();
expiry = formatter.parse(temp);
} catch (Exception e) {
e.printStackTrace();
}
but,it doesn't work.
z expects the format GMT+02:00 not GMT+02.00 replace the . and it should work.
Worked for me with the following code:
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
try {
String temp = "Fri Jul 30 16:19:36 GMT+02:00 2021";
Date expiry = formatter.parse(temp);
} catch (Exception e) {
e.printStackTrace();
}
Date cDate = new Date(String);
String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);

Categories

Resources