SimpleDateFormat for a HTTP Date - java

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

Related

Android SimpleDateFormat parse error

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.

how to parse a formatted string date to date object in java [duplicate]

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

java does not parse for 'M dd, yyyy' date format

I want to parse date strings like "February 7, 2011" using "M dd, yyyy" format. But I get an exception.
Try this code. I ran it with two dates "November 20, 2012" and "January 4, 1957" and got this output:
arg: November 20, 2012 date: Tue Nov 20 00:00:00 EST 2012
arg: January 4, 1957 date: Fri Jan 04 00:00:00 EST 1957
It works fine. Your regex was wrong.
package cruft;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* DateValidator
* #author Michael
* #since 12/24/10
*/
public class DateValidator {
private static final DateFormat DEFAULT_FORMATTER;
static {
DEFAULT_FORMATTER = new SimpleDateFormat("MMM dd, yyyy");
DEFAULT_FORMATTER.setLenient(false);
}
public static void main(String[] args) {
for (String dateString : args) {
try {
System.out.println("arg: " + dateString + " date: " + convertDateString(dateString));
} catch (ParseException e) {
System.out.println("could not parse " + dateString);
}
}
}
public static Date convertDateString(String dateString) throws ParseException {
return DEFAULT_FORMATTER.parse(dateString);
}
}
Your parsing string is not correct as mentioned by others
To correctly parse February you need to use an english Locale or it may fail if your default Locale is not in English
DateFormat df = new SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH);
Date dt = df.parse("February 7, 2011");
You will want to use "MMM dd, yyyy"
SimpleDateFormat("MMM dd, yyyy").parse("February 7, 2011")
See SimpleDateFormat
Assuming you are using SimpleDateFormat, the month format is incorrect, it should be MMM dd, yyyy
MMM will match the long text format of the month:
String str = "February 7, 2011";
SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy");
Date date = format.parse(str);

How to parse "Thu Aug 04 00:00:00 IST 2011" to "04-08-2011"?

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....

SimpleDateFormatter won't parse!

Hello I am trying to use the SimpleDateFormatter to parse the date Wed, 30 Jun 2010 15:07:06 CST
I am using the following code
public static SimpleDateFormat postedformat =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
Date newDate = new Date(posteformat.parse("Wed, 30 Jun 2010 15:07:06 CST"));
but I am getting an illegalArgumentException. Please help!
postedformat.parse() returns a Date, and there is no Date(Date) constructor.
Presumably removing the call to new Date, so you say Date newDate = poste.... will suffice
Your code fragment doesn't compile. This slight modification compiles and parses successfully:
public static void main(String[] args) throws ParseException {
SimpleDateFormat postedformat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
Date newDate = postedformat.parse("Wed, 30 Jun 2010 15:07:06 CST");
System.out.println("newDate = " + newDate);
}
This is using Java 6 on Mac OS X.
There is no java.util.Date() constructor that takes a java.util.Date as an argument
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormat {
public static SimpleDateFormat postedformat =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
public static void main(String[] args) {
try {
Date newDate = postedformat.parse("Wed, 30 Jun 2010 15:07:06 CST");
System.out.println("Date: " + newDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Outputs:
Date: Wed Jun 30 22:07:06 BST 2010
The javadoc examples shows unescaped comma but for the US locale. So either try escaping the comma (as Aaron suggested) or use the other constructor and set the Locale:
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
Another problem could be the timezone ('CST') which is deprecated on the on hand and ambigious on the other (as per javadoc of java.util.TimeZone). Test, if it works without the timezone attribute (in both the format String and the value).

Categories