I have a date in the format:Thu- Mar 22 2012.Its is obtained in a string variable date1.I need to convert the date in string variable to date format.I tried the below ccode but failed to parse the date;Please help.
DateFormat formatter;
Date formatted_date= null;
formatter = new SimpleDateFormat("EEE-MM d yyyy");
try {
formatted_date= (Date) formatter.parse(date1);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You need three Ms 'MMM' to parse a months abbreviation like 'Mar'. And please check the whitespace after 'EEE-'.
Change it to following format
EEE- MMM d yyyy
Note space after - and MMM
Thu- Mar 22 2012
EEE- MMM dd yyyy
I think you need something like this
UPD: for date formatting:
SimpleDateFormat toStringFormatter = new SimpleDateFormat("EEE MMM dd yyyy");
String formattedDate = toStringFormatter.format(date);
so parse() is for String -> Date, and format() is for Date -> String
If the already posted answers doesn't work then you surely have a problem with the locale. Try the following SimpleDateFormat:
formatter = new SimpleDateFormat("EEE- MMM d yyyy", Locale.ENGLISH);
I thank With the given mode and given the locale's default date format symbols constructor SimpleDateFormat. Note: This constructor may not support all locales. To cover all the language environment
public SimpleDateFormat(String pattern,Locale locale)
//local:Locale.ENGLISH
//default is not English
formatter = new SimpleDateFormat("EEE, MMM d, ''yy", Locale.ENGLISH);
try {
formatted_date= (Date) formatter.parse("Wed, Jul 4, '01");
} catch (ParseException e) {
e.printStackTrace();
}
Related
How to convert date fromat from "dd MMM yyyy" to "yyyy-MM-dd"?
I know I have to use SimpleDatFormat but it doesn't work, neither does any solution from similar questions.
I have a date "18 Dec 2015" that I am trying to format but I get this
java.text.ParseException: Unparseable date: "18 Dec 2015"
Here's my code:
public String parseDate(String d) {
String result = null;
Date dateObject = null;
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMM yyyy");
try {
dateObject = dateFormatter.parse(d);
dateFormatter.applyPattern("yyyy-MM-dd");
result = dateFormatter.format(dateObject);
} catch (ParseException e) {
System.out.println(e);
}
return result;
}
Did you try
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy");
instead of
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMM yyyy");
(note the hyphens, since your pattern doesn't match your input)
Also helpful: using Locale.US as recommended by #ZouZou
You are passing input as "18-Dec-2015" instead of the form "dd MMM yyyy". Try and pass input like 18 Dec 2015 and it should work.
Take a look at my code:
try {
// String date = "30Jul2013";
SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH);
Date d = sdf.parse(date);
SimpleDateFormat nsdf = new SimpleDateFormat("MMMM dd, yyyy h:mm a", Locale.ENGLISH);
String nd = nsdf.format(d);
System.out.println(nd);
return nd;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Im am getting a error:
java.text.ParseException: Unparseable date: "2013-07-30 10:58:55.171"
at java.text.DateFormat.parse(DateFormat.java:337)
I would like to have an output of July 30, 2013 11:10 AM from the simpledateformat. There's LOCALE in my code. So what else should I do?
Thanks in advance!
try {
// String date = "30Jul2013";
SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH);
Date d = sdf.parse(date);
Your date String variable line is commented out, so who's to know what String you're parsing? -- the JVM that's who.
As Robert Harvey points out, the String that you're actually trying to parse is printed for you in the exception message. If you print that String before you parse you'll also see that it's not what you expect it is and that the compiler's right.
In sum, you are somehow expecting that your sdf SimpleDateFormat object is formatting a String of a format similar to "30Jul2013", but the JVM is telling you that this simply is not so, that the String you are trying to parse in fact looks nothing like this, but rather is "2013-07-30 10:58:55.171".
I'm having a tough time parsing this date its the +0 at the end that is causing a problem, does anyone know whats wrong with my format string?? If I remove the +0 from the date string and the Z from the format string it works fine, unfortunately for my application that isn't an option.
public class Main {
/**
* #param args
*/
public static void main(String[] args) {
SimpleDateFormat dateFormater = new SimpleDateFormat("E, dd MMM yyyy kk:mm:ss zZ");
try {
Date d = dateFormater.parse("Sun, 04 Dec 2011 18:40:22 GMT+0");
System.out.println(d.toLocaleString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
One approach is to use normal string-manipulation techniques to translate your string from a form that you're expecting to a form that SimpleDateFormat will understand. You haven't said exactly what range of time-zone formats are acceptable, but one possibility is something like this:
private static Date parse(String dateString) throws ParseException
{
final SimpleDateFormat dateFormat =
new SimpleDateFormat("E, dd MMM yyyy kk:mm:ss Z");
dateString = dateString.replaceAll("(GMT[+-])(\\d)$", "$1\\0$2");
dateString = dateString.replaceAll("(GMT[+-]\\d\\d)$", "$1:00");
return dateFormat.parse(dateString);
}
That would support GMT plus-or-minus a one-or-two-digit hour offset, in addition to still supporting anything already supported by SimpleDateFormat, such as EST or GMT+1030.
Alternatively, if you know it will always be GMT, then you can just set the time-zone on the formatter, and ignore the time-zone in the string:
private static Date parse(String dateString) throws ParseException
{
final SimpleDateFormat dateFormat =
new SimpleDateFormat("E, dd MMM yyyy kk:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
return dateFormat.parse(dateString);
}
You can also split the difference. I notice that the time-zone format in your string matches what's expected by TimeZone.getTimeZone(). Is that intentional? If so, you can grab that time-zone format out of the string, pass it to dateFormat.setTimeZone beforehand, and then ignore it during actual parsing:
private static Date parse(final String dateString) throws ParseException
{
final SimpleDateFormat dateFormat =
new SimpleDateFormat("E, dd MMM yyyy kk:mm:ss");
if(dateString.indexOf("GMT") > 0)
dateFormat.setTimeZone
(
TimeZone.getTimeZone
(dateString.substring(dateString.indexOf("GMT")))
);
return dateFormat.parse(dateString);
}
If the format is that consistent, you could append 0:00 to the date string.
String dateString = "Sun, 04 Dec 2011 18:40:22 GMT+0";
SimpleDateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy kk:mm:ss z", Locale.ENGLISH);
Date date = sdf.parse(dateString + "0:00");
System.out.println(date);
(note that I fixed the SimpleDateFormat construction to explicitly specify the locale which would be used to parse the day of week and month names, otherwise it may fail on platforms which does not use English as default locale; I also wonder if you don't actually need HH instead of kk, but that aside)
I have a list with many Date Strings such as "Fri, 08 Apr 2011 22:28:00 -0400" and need to parse them to a proper format (Friday, 08. April 2011).
My problem is that the device needs a very very long time parsing > 10 date objects and occasionally runs out of memory. Is there a more efficient way parsing dates as:
SimpleDateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
// later in code
try {
Date date = sdf.parse(myDateString);
return DateFormat.format("dd. MMMM yyyy", date).toString();
} catch (java.text.ParseException e) {
}
How can I parse many date Strings very fast?
Try this : Alternatives to FastDateFormat for efficient date parsing?
One thing you can do is to store the output DateFormat, like you do with the input DateFormat, so that you don't have to create a new one every time:
DateFormat parser = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
DateFormat formatter = new SimpleDateFormat("dd. MMMM yyyy");
// later in code
try {
Date date = parser.parse(myDateString);
return formatter.format(date);
} catch (java.text.ParseException e) {}
If possible, assign this task to your database (assuming SQLite) and see if its Date and Time function are useful in your case.
I want to convert the date string in a Twitter response to a Date object, but I always get a ParseException and I cannot see the error!?!
Input string: Thu Dec 23 18:26:07 +0000 2010
SimpleDateFormat Pattern:
EEE MMM dd HH:mm:ss ZZZZZ yyyy
Method:
public static Date getTwitterDate(String date) {
SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
sf.setLenient(true);
Date twitterDate = null;
try {
twitterDate = sf.parse(date);
} catch (Exception e) {}
return twitterDate;
}
I also tried this: http://friendpaste.com/2IaKdlT3Zat4ANwdAhxAmZ but that gives the same result.
I use Java 1.6 on Mac OS X.
Cheers,
Andi
Your format string works for me, see:
public static Date getTwitterDate(String date) throws ParseException {
final String TWITTER="EEE MMM dd HH:mm:ss ZZZZZ yyyy";
SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
sf.setLenient(true);
return sf.parse(date);
}
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(getTwitterDate("Thu Dec 3 18:26:07 +0000 2010"));
}
Output:
Fri Dec 03 18:26:07 GMT 2010
UPDATE
Roland Illig is right: SimpleDateFormat is Locale dependent, so
just use an explicit english Locale:
SimpleDateFormat sf = new SimpleDateFormat(TWITTER,Locale.ENGLISH);
This works for me ;)
public static Date getTwitterDate(String date) throws ParseException
{
final String TWITTER = "EEE, dd MMM yyyy HH:mm:ss Z";
SimpleDateFormat sf = new SimpleDateFormat(TWITTER, Locale.ENGLISH);
sf.setLenient(true);
return sf.parse(date);
}
Maybe you are in a locale where ‘Tue‘ is not a recognized day of week, for example German. Try to use the ‘SimpleDateFormat‘ constructor that accepts a ‘Locale‘ as a parameter, and pass it ‘Locale.ROOT‘.
You should not have ZZZZZ but only Z for the timezone.
See samples in http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html for more information.
EEE, d MMM yyyy HH:mm:ss Z > Wed, 4 Jul 2001 12:08:56 -0700
SimpleDateFormat is not thread safe. "EEE MMM dd HH:mm:ss ZZZZZ yyyy" was working in our application, but failing in a small percentage of cases. We finally realized that the issue was coming from multiple threads using the same instance of SimpleDateFormat.
Here is one workaround: http://www.codefutures.com/weblog/andygrove/2007/10/simpledateformat-and-thread-safety.html
Function for convert Twitter Date :
String old_date="Thu Jul 05 22:15:04 GMT+05:30 2012";
private String Convert_Twitter_Date(String old_date) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
SimpleDateFormat old = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy",Locale.ENGLISH);
old.setLenient(true);
Date date = null;
try {
date = old.parse(old_date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sdf.format(date);
}
The output format like : 05-Jul-2012 11:54:30