I have date String as below:
Fri, 19 Jul 2013 01:30:22 GMT
Thu, 12 Sep 2013 19:19:18 GMT
And I want to convert to below format:
2013/07/19 01:30:22
2013/09/12 19:19:18
At first, I use below code to parse date:
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Date date = dateFormat.parse("Fri, 23 Aug 2013 01:10:35 GMT");
But it show below exception on line Date date = dateFormat.parse("Fri, 23 Aug 2013 01:10:35 GMT");:
java.text.ParseException: Unparseable date: "Fri, 23 Aug 2013 01:10:35 GMT" (at offset 0)
How can I modify it?
try this
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
your default language is probably not English and does not accept Fri and Aug
Use this code
String dateString = "Fri, 19 Jul 2013 01:30:22 GMT";
SimpleDateFormat simple = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date;
try {
date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z")
.parse(dateString);
Log.e("result", "Date:" + simple.format(date));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try this
String DateStr="Fri, 19 Jul 2013 01:30:22 GMT";
SimpleDateFormat sim=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date d = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z").parse(DateStr);
System.out.println(sim.format(d));
You can make like this.
String newDate = date.getYear()+"/"+date.getMonth()+"/"+date.getDay() and ect.
or you can write your own convert method
String myConvert(Date date)
{
return date.getYear()+"/"+date.getMonth()+"/"+date.getDay()+ect.
}
You can try this
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Date date = dateFormat.parse("Fri, 23 Aug 2013 01:10:35 GMT");
String formattedString=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").
format(date).toString();
System.out.println(formattedString);
Try
String dateStr = "Fri, 19 Jul 2013 01:30:22 GMT";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z").parse(dateStr);
System.out.println(sdf.format(date));
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
date.parse("Fri, 19 Jul 2013 01:30:22 GMT");
String newDate = simpleDateFormat.format(date);
Try with "format" :
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
String date = "";
try {
date = dateFormat.format("Fri, 23 Aug 2013 01:10:35 GMT");
} catch(Exception e){
e.printStackTrace();
}
Related
I am trying to convert the following json string: "Mon Apr 04 00:00:00 CEST 2016" to a new date object by a simpleDateFormat. But i dont see why it wont work hope some one can help me.
String date = "Mon Apr 04 00:00:00 CEST 2016";
I get the following error:
(java.text.ParseException) java.text.ParseException: Unparseable
date: "Mon Apr 04 00:00:00 CEST 2016"
public Date parseDate(String date)
{
try
{
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date returnDate = formatter.parse(date);
return returnDate;
}
catch (ParseException e)
{
e.printStackTrace();
return null;
}
}
you need to parse with the locale:
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
"EEE MMM ddHH:mm:ss z yyyy"
looks you forgot to put the space after dd:
"EEE MMM dd HH:mm:ss z yyyy"
Here is code to parser "Wed, 01 Jul 2015 17:32:41 EDT", but it still doesn't work.
Does anyone can help me figure it out? many thanks.
SimpleDateFormat formatter = new SimpleDateFormat("EEE dd MMM yyyy HH:mm:ss zzz");
try {
return formatter.parse(text);
} catch (ParseException e) {
e.printStackTrace();
}
Try this
public static void main(String args[]) {
SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
try {
System.out.println(formatter.parse("Wed, 01 July 2015 17:32:41 EDT"));
} catch (Exception e) {
e.printStackTrace();
}}
Output : Thu Jul 02 03:02:41 IST 2015
You have a , after Wed... You need to account for that as well in the format...
Use "EEE, dd MMM yyyy HH:mm:ss zzz"
Use comma AND locale (Thu is english):
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
Try this.
try {
String s = "Wed, 01 Jul 2015 17:32:41 EDT";
SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); //Or Locale.ENGLISH
df.setTimeZone(TimeZone.getTimeZone("EDT"));
//Or set timeZone to GMT-4:00 because EDT is GMT-4:00
//df.setTimeZone(TimeZone.getTimeZone("GMT-04:00"));
Date time = null;
time = df.parse(s);
System.out.println("Time = " + s);
String parsed = df.format(time);
} catch (Exception e) {
e.printStackTrace();
}
Output is:
Old = Wed, 01 Jul 2015 17:32:41 EDT
I want to parse this date "Mon Mar 09 2015 00:00:00 GMT+0530 (India Standard Time)" in java SimpleDateFormat class as "MM-dd-yyyy", is this a valid date format? If yes, how can I do that?
String inputDate = "Wed Sep 17 2014 00:00:00 GMT+0530 (India Standard Time)";
//SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss"); -- Not working
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss z"); // Not Working
Date date = sdf.parse(inputDate);
sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(date));
Tried following piece of code:
String inputDate = "Wed Sep 17 2014 00:00:00 GMT+0530 (India Standard Time)";
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'z");
Date date = sdf.parse(inputDate);
sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(date));
Output:
2014-09-17
Try by using this format "E MMM dd yyyy HH:mm:ss z" with SimpleDateFormat class.
How do I convert date string 'Fri Mar 14 09:44:31 IST 2014' to millisec?
I tried with this Java code:
String dateStr = "Fri Mar 14 09:44:31 IST 2014";
SimpleDateFormat sdf = new SimpleDateFormat("MM dd HH:MM:ss");
try {
System.out.println(sdf.parse(dateStr).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
your SimpleDateFormat is not correct.
Try this
SimpleDateFormat sd = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try following code.
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 9 years ago.
How to parse the following string to date
Mon Oct 22 03:00:26 +0000 2012
I tried MMM dd HH:mm:ss yyyy, but it is not working. I know I am missing something but couldn't find out that.
String b="Mon Oct 22 03:00:26 +0000 2012";
DateFormat a = new SimpleDateFormat("MMM dd HH:mm:ss yyyy");
Date d=(Date)a.parse(b)
I'd recommend EEE MMM dd HH:mm:ss Z yyyy instead of HH:mm:ss yyyy.
Edit:
Specifically, your code would be:
String b="Mon Oct 22 03:00:26 +0000 2012";
DateFormat a = new SimpleDateFormat("MMM dd HH:mm:ss Z yyyy");
Date d=(Date)a.parse(b)
Edit after comment:
String b="Mon Oct 22 03:00:26 +0000 2012";
DateFormat a = new SimpleDateFormat("MMM dd HH:mm:ss Z yyyy", Locale.getDefault());
Date d=(Date)a.parse(b)
Try to do something like this:
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
String dateInString = "Friday, Jun 7, 2013 12:10:56 PM";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Ref: How to Convert String to Date in Java