Consider this program that format the current date, and try to parse it again. It succeeds in French, but fails in English and I don't understand why.
import java.util.Locale;
import java.text.DateFormat;
import java.time.Instant;
import java.util.Date;
import java.text.SimpleDateFormat;
public class HelloWorld{
public static void main(String []args){
try{
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.ENGLISH);
DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT, Locale.ENGLISH).parse(formatter.format(new Date()));
System.out.println("English - success");
}catch(Exception ex){
System.out.println(ex);
}
try{
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.FRENCH);
DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT, Locale.FRENCH).parse(formatter.format(new Date()));
System.out.println("French - success");
}catch(Exception ex){
System.out.println(ex);
}
System.out.println(Locale.getDefault());
}
}
Output:
java.text.ParseException: Unparseable date: "11 Feb 2015 11:09:26"
French -success
en_US
Please look at http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#parse%28java.lang.String,%20java.text.ParsePosition%29 before telling me that I should use a pattern or anything else. This method is meant to parse a String without a pattern.
I suspect your input format string is wrong.
As per documentation Jun 30, 2009 7:03:47 AM would be a valid format for en_US on Default settings.
You could always check if your format is right by formatting a given Date first.
For example
System.out.println(DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT, Locale.ENGLISH).format(new Date())); gives Feb 11, 2015 12:34:48 PM, which doesn't fit 11 Feb 2015 11:09:26.
This should be your correct formatted string for en_US parsing: Feb 11, 2015 11:09:26 AM. Remember, this is AM / PM 12 hour time format, which can be annoying.
are you sure that dd MMM yyyy HH:mm:ss is a correct english format dont they use month before days like US format ?
The default format for English is like this:
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a", Locale.ENGLISH);
Related
How to make the date to have a GMT offset like mentioned here
import java.util.*;
import java.text.*;
import java.lang.*;
class TFTest
{
public static void main(String[] args)
{
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM, yyyy z");
Date dt = new Date();
System.out.println("\n\n\nparsed date:"+sdf.format(dt)+"\n\n");
}
}
the above program outputs the value as
parsed date:02 Aug, 2016 IST.
But I want the value to be parsed date:02 Aug, 2016 GMT +05:30
How to get in the specified format ..?
The pattern that should work is dd MMM, yyyy 'GMT' XXX indeed X is the timezone in ISO 8601 which seems to be what you are looking for.
Output:
parsed date:02 Aug, 2016 GMT +05:30
Try, for more documentation visit simpledateformat
"dd MMM, yyyy 'GTM' XXX"
This pattern "dd MMM, yyyy z ZZZZ" will print in the given format
Format formatter = new SimpleDateFormat("dd MMM, yyyy z ZZZZ");
Result like :
02 Aug, 2016 GMT +0000
I am having a problem using the parseDateTime method in joda time. When I try to parse the date below, the result is one day off. I know there is already a similar thread about this, and I know that if your dayOfWeek and dayOfMonth are mismatched, it prioritizes the dayOfWeek. But my date is valid -- I have checked that february 22 falls on a Friday. But when I parse it, I am getting thursday, february 21. Here is the code:
DateTimeFormatter NBSfmt = DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z");
DateTimeFormatter MYfmt = DateTimeFormat.forPattern("yyyy-MM-dd");
String date ="Fri, 22 Feb 2013 00:00:00 +0000";
DateTime datetime = NBSfmt.parseDateTime(date);
System.out.println(datetime.toString());
And here is the output:
2013-02-21T19:00:00.000-05:00
Anyone have any idea what is going on here? Any insight would be greatly appreciated.
Thanks,
Paul
This is caused by your timezone. You define it in +0000 but then you're viewing it in -05:00. That makes it appear one day before. If you normalize it to UTC, it should be the same.
Try this code, as evidence:
package com.sandbox;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class Sandbox {
public static void main(String[] args) {
DateTimeFormatter NBSfmt = DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z");
String date = "Fri, 22 Feb 2013 00:00:00 -0500";
DateTime datetime = NBSfmt.parseDateTime(date);
System.out.println(datetime.toString());
}
}
For you, this should show the "right day". But for me, it shows 2013-02-21T21:00:00.000-08:00 because I'm in a different timezone than you. The same situation is happening to you in your original code.
Here's how you can print the string out in UTC:
package com.sandbox;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class Sandbox {
public static void main(String[] args) {
DateTimeFormatter NBSfmt = DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z");
String date = "Fri, 22 Feb 2013 00:00:00 +0000";
DateTime datetime = NBSfmt.parseDateTime(date);
System.out.println(datetime.toDateTime(DateTimeZone.UTC).toString());
}
}
This prints 2013-02-22T00:00:00.000Z.
The timezone of yours is -5, and joda treats the input as UTC in the example. You can use withZone to get a new formatter if needed.
How can I parse this date format Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest) to this date format 05-14-2010 I mean mm-dd-yyyy
it's telling me this error :
java.text.ParseException: Unparseable date: "Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest)"
EDIT
SimpleDateFormat formatter = new SimpleDateFormat("M-d-yyyy");
newFirstDate = formatter.parse(""+vo.getFirstDate()); //here the error
Thanks in advance!
This code first adapts the string a bit and then goes on to parse it. It respects the timezone, just removes "GMT" because that's how SimpleDateFormat likes it.
final String date = "Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest)"
.replaceFirst("GMT", "");
System.out.println(
new SimpleDateFormat("MM-dd-yyyy").format(
new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z").parse(date)));
Prints:
05-14-2010
Bear in mind that the output is also timezone-sensitive. The instant defined by your input string is being interpreted in my timezone as belonging to the date that the program printed. If you just need to transform "May 14 2010" into "05-14-2010", that's another story and SimpleDateFormat is not well suited for that. The JodaTime library would handle that case much more cleanly.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test
{
public static void main( String args[] ) throws ParseException
{
// Remove GMT from date string.
String string = "Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest)".replace( "GMT" , "" );
// Parse string to date object.
Date date = new SimpleDateFormat( "EEE MMM dd yyyy HH:mm:ss Z" ).parse( string );
// Format date to new format
System.out.println( new SimpleDateFormat( "MM-dd-yyyy" ).format( date ) );
}
}
Outputs:
05-13-2010
DateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
sdf.parse("Sun Dec 13 10:00:00 UTC 2009")
result
java.text.ParseException: Unparseable date: Sun Dec 13 10:00:00 UTC
2009
Note: This code seems to work in a normal Java application but seems to fail on Android.
It doesn't for me - perhaps your default locale uses different month names? Specify the locale for the format.
// Will definitely work
DateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy",
Locale.US);
// Will definitely not work
DateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy",
Locale.FRANCE);
// Might work - depends on default locale
DateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy")
(The problem is the names of the days of the week and months of the year, which are obviously culture-specific. Date and time separators can vary too.)
EDIT: It's odd that you're still having problems. Just to check, please try to run the following short but complete program:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class Test {
public static void main(String[] args) throws Exception {
DateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy",
Locale.US);
sdf.parse("Sun Dec 13 10:00:00 UTC 2009");
}
}
If that doesn't work, try taking out the time zone part of both the pattern and the text. I wonder whether it's having problems with that.
EDIT: If the Android SimpleDateFormat implementation doesn't manage to parse the time zone, you can probably just use:
text = text.replace(" UTC ", " ");
Date parsed = sdf.parse(text);
... having set the time zone on the parser to UTC, of course. You probably want to check that it contains " UTC " first, just in case your data format changes.
Your format looks correct. Is it possible that you are not using an English Locale though? The formatter will take your system locale and this could result in different names for 'sun' and 'dec'
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 7 years ago.
How do I parse the date string below into a Date object?
String target = "Thu Sep 28 20:29:30 JST 2000";
DateFormat df = new SimpleDateFormat("E MM dd kk:mm:ss z yyyy");
Date result = df.parse(target);
Throws exception...
java.text.ParseException: Unparseable date: "Thu Sep 28 20:29:30 JST 2000"
at java.text.DateFormat.parse(DateFormat.java:337)
The pattern is wrong. You have a 3-letter day abbreviation, so it must be EEE. You have a 3-letter month abbreviation, so it must be MMM. As those day and month abbreviations are locale sensitive, you'd like to explicitly specify the SimpleDateFormat locale to English as well, otherwise it will use the platform default locale which may not be English per se.
public static void main(String[] args) throws Exception {
String target = "Thu Sep 28 20:29:30 JST 2000";
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
Date result = df.parse(target);
System.out.println(result);
}
This prints here
Thu Sep 28 07:29:30 BOT 2000
which is correct as per my timezone.
I would also reconsider if you wouldn't rather like to use HH instead of kk. Read the javadoc for details about valid patterns.
Here is a working example:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
public class j4496359 {
public static void main(String[] args) {
try {
String target = "Thu Sep 28 20:29:30 JST 2000";
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
Date result = df.parse(target);
System.out.println(result);
} catch (ParseException pe) {
pe.printStackTrace();
}
}
}
Will print:
Thu Sep 28 13:29:30 CEST 2000
String target = "27-09-1991 20:29:30";
DateFormat df = new SimpleDateFormat("dd MM yyyy HH:mm:ss");
Date result = df.parse(target);
System.out.println(result);
This works fine?
new SimpleDateFormat("EEE MMM dd kk:mm:ss ZZZ yyyy");
and
new SimpleDateFormat("EEE MMM dd kk:mm:ss Z yyyy");
still runs. However, if your code throws an exception it is because your tool or jdk or any other reason. Because I got same error in my IDE but please check these http://ideone.com/Y2cRr (online ide) with ZZZ and with Z
output is : Thu Sep 28 11:29:30 GMT 2000
I had this issue, and I set the Locale to US, then it work.
static DateFormat visitTimeFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",Locale.US);
for String "Sun Jul 08 00:06:30 UTC 2012"
A parse exception is a checked exception, so you must catch it with a try-catch when working with parsing Strings to Dates, as #miku suggested...