I would like to parse this string: Thu Jan 01 00:00:58 CET 1970
I use this pattern: EEE MMM dd hh:mm:ss z yyyy
But I got this exception:
java.text.ParseException: Unparseable date: "Thu Jan 01 00:00:58 CET 1970" (at offset 20)
stacktrace:
java.text.ParseException: Unparseable date: "Thu Jan 01 00:01:18 CET 1970" (at offset 20)
W/System.err: at java.text.DateFormat.parse(DateFormat.java:571)
system env: android studio 2.0, compileSdkVersion 23, buildToolsVersion "23.0.3"
device: HTC One M7, android 5.0.2
You should create a test case and demonstrate the behavior. I did it for you:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.junit.Test;
public class DateParseTest {
#Test
public void testDateFormat() {
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.US);
try {
Date date = dateFormat.parse("Thu Jan 01 00:00:58 CET 1970");
System.out.println("parsed date:" + date);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
}
Use an explicit locale setting Locale.US. In your case hungarian is the default locale and you have to parse a date string in hungarian format.
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", new Locale("HU"));
String dateString = "P máj. 01 01:00:58 CET 1970";
Date date = dateFormatHu.parse(dateString);
Please refer to this post Java Date(0) is not 1/1/1970.
All of the issues with the 1/1/1970 date are explained in full detail.
Related
This question already has answers here:
Parse any date in Java
(6 answers)
Closed 4 years ago.
I was trying to parse the formatted string date, was getting parse error
input date is "Wed Nov 11 14:24:46 IST 2015", need output date as "Wed Nov 11 2015 14:24:46 IST"
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormat {
public static void main(String[] args) {
try {
String target = "Wed Nov 11 14:24:46 IST 2015";
SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
Date result = df.parse(target);
SimpleDateFormat df2 = new SimpleDateFormat("EEE MMM dd yyyy kk:mm:ss zzz");
String result2 = df2.format(result);
Date result3 = df.parse(result2);
System.out.println(result2);
System.out.println(result3);
} catch (ParseException pe) {
pe.printStackTrace();
}
}
}
getting error as java.text.ParseException: Unparseable date: "Wed Nov 11 2015 14:24:46 IST"
I have updated my answer to do the parsing as mentioned in your question/comment. See below the explanation:
"Wed Nov 11 14:24:46 IST 2015"
to the following
"Wed Nov 11 2015 14:24:46 IST"
I setup two SimpleDateFormat objects as follow
SimpleDateFormat sourceFormat, destinationFormat;
//this is to format the string to Date object
sourceFormat = new SimpleDateFormat("EEE MMM d kk:mm:ss zzz yyyy", Locale.US);
//this is to format the Date object to desired pattern
destinationFormat = new SimpleDateFormat("EEE MMM d yyyy kk:mm:ss zzz", Locale.US);
I then set the timezone as follow
TimeZone istTimeZone = TimeZone.getTimeZone("Asia/Kolkata");
sourceFormat.setTimeZone(istTimeZone);
destinationFormat.setTimeZone(istTimeZone);
I use the sourceFormat object to format the date string to Date object as follow:
Date sourceDate = sourceFormat.parse(target);
//output: Wed Nov 11 08:54:46 GMT 2015
I then use the destination format to format the date object that represents the string as follow:
Date destinationDate = destinationFormat.format(d);
//output: Wed Nov 11 2015 14:24:46 IST
Basically in order to get a legit Date object I have to use the first SimpleDateFormat sourceFormat which contains pattern that maps the date in String. Once a legit Date object is created using the String then I use the second formatter to re-format the Date object. Below is full code that should give output if copy/pasted.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class ParseDate {
public static void main(String[] args) {
try {
String target = "Wed Nov 11 14:24:46 IST 2015";
SimpleDateFormat sourceFormat, destinationFormat;
sourceFormat = new SimpleDateFormat("EEE MMM d kk:mm:ss zzz yyyy", Locale.US);
destinationFormat = new SimpleDateFormat("EEE MMM d yyyy kk:mm:ss zzz", Locale.US);
TimeZone istTimeZone = TimeZone.getTimeZone("Asia/Kolkata");
sourceFormat.setTimeZone(istTimeZone);
destinationFormat.setTimeZone(istTimeZone);
Date d = sourceFormat.parse(target);
System.out.println(d.toString());
//output: Wed Nov 11 08:54:46 GMT 2015
System.out.println(destinationFormat.format(d));
//output: Wed Nov 11 2015 14:24:46 IST
} catch (ParseException pe) {
pe.printStackTrace();
}
}
}
I need help parsing the String date
"Thu Oct 22 13:51:51 CEST 2015"
with SimpleDateFormat, but I'm not able to find the correct pattern.
Here is what I've tried
String date = "Thu Oct 22 13:51:51 CEST 2015";
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("E M d H:m:s z y");
d1 = dateFormat.parse(date);
} catch (ParseException e1) {
Log.e(null, String.valueOf(e1));
}
I get back the error:
java.text.ParseException: Unparseable date: "Thu Oct 22 13:51:51 CEST 2015" (at offset 0)
UPDATE
I tried the solution below by durron597 adapting it to my needs:
String date = "Thu Oct 22 13:51:51 CEST 2015";
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy", Locale.US);
Date d1 = dateFormat.parse(date);
} catch (ParseException e1) {
Log.e("null", String.valueOf(e1));
}
but in the logcat I keep getting the error:
java.text.ParseException: Unparseable date: "Thu Oct 22 13:51:51 CEST 2015" (at offset 20)
Use more letters for the longer versions of the Date elements.
In particular the problem in your case was probably the Month portion, which you can see from this part of the Javadoc for SimpleDateFormat:
Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.
You only had one character for Month, it tried to interpet it as a number, found a letter, and failed.
You also need to specify the US locale, as these are United States dates.
Specify the Locale to use in translating the name of the day of the week, “Thu”. You are in Italy (it says so in your profile); when I use Locale.Italy on my machine, I get the same error as you do. If you specify Locale.US as in the below code, it should work.
Update: Android doesn't support three letter time zones, according to this:
Other than the special cases "UTC" and "GMT" (which are synonymous in this context, both corresponding to UTC), Android does not support the deprecated three-letter time zone IDs used in Java 1.1.
You should be able to replace your Europe time zone with the full name of the time zone defined in the official “tz” time zone database (formerly known as Olson database). For example, Europe/Rome instead of "CEST".
Try this instead:
SimpleDateFormat dateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy", Locale.US);
date.replace("CEST", "Europe/Rome");
Date d1 = dateFormat.parse(date);
Here's a full working example:
public class DateExample {
public static void main(String[] args) {
String date = "Thu Oct 22 13:51:51 CEST 2015";
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy", Locale.US);
date.replace("CEST", "Europe/Rome");
Date d1 = dateFormat.parse(date);
System.out.println(d1);
} catch (ParseException e1) {
e1.printStackTrace();
}
}
}
Output:
Thu Oct 22 06:51:51 CDT 2015
I can parse a HTTP Date but I don't get what I want, i.e. in this Example
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Example01 {
public static void main(String[] argv) throws Exception {
Date date = null;
String dateValue = "Tue, 27 Jan 2015 07:33:54 GMT";
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("date = " + date);
}
}
And the output I got is
date = Tue Jan 27 08:33:54 CET 2015.
What should I change, in order to get
date = Tue, 27 Jan 2015 08:33:54 GMT?
There are a few problems with your code:
You never initialize date to anything other than null, thus System.out.println("date = " + date); will print date = null, not date = Tue Jan 27 08:33:54 CET 2015.
The date string you provide is Tue, 27 Jan 2015 07:33:54 GMT, yet you ask the output to be Tue, 27 Jan 2015 08:33:54 GMT. The output is one hour later than the date string provided. I'm going to assume it's a typo on your side and you actually want the former as output.
You didn't use the SimpleDateFormat dateFormat object that you've got when printing out the date. System.out.println("date = " + date); calls the Date.toString() method which uses your local timezone.
A working version is:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class Example01 {
public static void main(String[] argv) throws Exception {
Date date = null;
String dateValue = "Tue, 27 Jan 2015 07:33:54 GMT";
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
date = dateFormat.parse(dateValue);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("date = " + dateFormat.format(date));
}
}
I'm trying to parse date like this: Tue Aug 28 21:16:23 +0000 2012 with this code:
SimpleDateFormat format = new SimpleDateFormat("E M dd HH:mm:ssZ yyyy", Locale.ENGLISH);
String d = object.getString("created_at"); // d = Tue Aug 28 21:16:23 +0000 2012;
date = format.parse(d);
But there is exception:
09-28 11:10:24.471: W/System.err(10388): java.text.ParseException: Unparseable date: "Fri Sep 28 07:09:09 +0000 2012" (at offset 4)
Where I make a mistake?
try this
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
MMM is used to represent short Month.
you need MMM for month representation for Aug.
SimpleDateFormat format = new SimpleDateFormat("E MMM dd HH:mm:ssZ yyyy", Locale.ENGLISH);
String d = "Tue Aug 28 21:16:23 +0000 2012"; // d =;
Date date = format.parse(d);
System.out.println(date);
Output:Tue Aug 28 22:16:23 BST 2012
It might help to look into SimpleDateFormat's javadoc, there are some helpful examples for pattern strings.
I want to parse the date Thu Aug 04 00:00:00 IST 2011 to dd-MM-YY format like 04-08-2011. How to do this in Java?
Use the following format to parse: EEE MMM dd HH:mm:ss z yyyy with SimpleDateFormat.parse(..)
The use another SimpleDateFormat with the dd-MM-yy format, to format(..) the resultant date. Something like:
SimpleDateFormat parseFormat =
new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = parseFormat.parse(dateString);
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yy");
String result = format.format(date);
I hope you can find the solution from the sample code below
import java.text.DateFormat;
import java.util.Date;
public class DateFormatExample
{
public static void main(String args[])
{
Date now=new Date();
System.out.println("dd/mm/yy format:" +DateFormat.getDateInstance(DateFormat.SHORT).format(now));
}
}
Now you will get your required format....