Issues parsing a String into a Calendar in Java - java

When I try the following code,
public static void main(String[] args) throws ParseException{
try {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
calendar.setTime(sdf.parse("Sun Feb 22 18:02:51 FET 2015"));
} catch (Exception e) {
e.printStackTrace();
}
}
I systematically get
java.text.ParseException: Unparseable date: "Sun Feb 22 18:02:51 FET 2015"
at java.text.DateFormat.parse(DateFormat.java:357)
at foo.Sample.main(LogEntry.java:131)
I realize that my issue is the FET timezone. That is a valid timezone. I have tried different things including using Locale.English but to no avail.
My issue seems to be due to the timezone as explained here.
EDIT
The following does not work either
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Here is another post I found but didn't help me.

Java doesn't seem to support FET.
Use MSK.

Related

Simple Date Parsing

I've been struggling a strange question.
I'm trying to parse a date format looks like this
"Thu, 18 Aug 2016 16:25:25 GMT"
to only Month and date like this
"08/18"
well I use two SimpleDateFormat, to parse and format the input and output
DateFormat inputFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
DateFormat outputFormat = new SimpleDateFormat("MM/dd");
however, everything seems to be correct when using genymotion emulator
but when it comes to the real phone, say sony Xperia Care
the following questions kept on comming
java.text.ParseException: Unparseable date: "Wed, 10 Aug 2016 15:40:57 GMT" (at offset 0)
so whats the reason that this happens?
Try to use
new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);
Your String-DAte has information that is related to an specific language, therefor you need to prepare the SimpleDAteFormat for such an information.
ERGO: you need to give a Locale
Example:
public static void main(String[] args) throws ParseException {
String dateStr = "Thu, 18 Aug 2016 16:25:25 GMT";
DateFormat inputFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
Date d = null;
d = inputFormat.parse(dateStr);
DateFormat outputFormat = new SimpleDateFormat("MM/dd");
System.out.println("After format: " + outputFormat.format(d));
}

How to get a date variable from an object with a date in Java

I have an Object with the following text:
System.out.println("My date: " + valor);
and it prints
My date: Thu Jan 01 13:00:00 CST 2015
I want to convert this text to a Date variable, this is my code:
dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy",new Locale("es","MX"));
try {
Date dateToString = dateFormat.parse(valor.toString());
} catch (ParseException e) {
new Date();
}
I always get current date, I've tried several combinations for SimpleDateFormat but none works, what's the proper way to convert my Object to a Date variable
Your format doesn't include time zone (z) and you shouldn't swallow an Exception without logging it (which is how I found the error). Something like,
DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
Date dateToString = dateFormat.parse("Thu Jan 01 13:00:00 CST 2015");
System.out.println(dateFormat.format(dateToString));
} catch (ParseException e) {
e.printStackTrace();
}
Output is
Thu Jan 01 13:00:00 CST 2015
Looks like your date format isn't accounting for the timezone. Maybe try
EEE MMM dd HH:mm:ss z yyyy
See Java Docs

SimpleDateFormat and parseException

I am developing a Web application into GWT and I am using the Object DatePicker. This object retrieves the date in a defined format which I am translating into a String such as:
Wed May 14 2014 00:00
For me it is useful to use this date as String for some operations. However, for one of them I need the Timestamp object. For that reason, I am making use of the SimpleDateFormat object in the following way:
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm");
Timestamp tDateIni = new Timestamp(sdf.parse(sDateIni).getTime());
Yet, when I run the remote debug I get a ParseException. Do you know what could the mistake be? I think I am using in a bad format the SimpleDateFormat object in the part "E MMM", but I am not sure. Thanks a lot in advance!
If you want to parse the date at client side in GWT then try with DateTimeFormat
DateTimeFormat dateTimeFormat=DateTimeFormat.getFormat("E MMM dd yyyy HH:mm");
Date date=dateTimeFormat.parse("Wed May 14 2014 00:00");
If you want to parse the date at server side then pass the time in milliseconds as long value instead of date string from client side and form the date back at server side using new Date(timeInMills)
Your date format uses the day of the week format that requires "EEE" instead of "E". This is causing the exception when the program is trying to read in your date string. It is expecting one letter for the day of the week.
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Change this from
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm");
to
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm");
It should be EEE instead of E to represent Weekdays like Wed
Below code, perfectly works (TESTED)
public static void main(String[] args) {
String s = "Wed May 14 2014 00:00";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm");
try {
Timestamp tDateIni = new Timestamp(sdf.parse(s).getTime());
System.out.println(tDateIni.getTime());
} catch (ParseException ex) {
System.out.println("Parse Error");
}
}
I have added the Locale object in the SimpleDateFormat object and now it works. Thank you for all your help and your comments!!!

Parsing Date from Formatted String in Java

I have a string with format "Mon Dec 03 00:00:00 IST 2012". I want to convert this string into Date. Any formats will do.
can try:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
Date date = sdf.parse("Mon Dec 03 00:00:00 IST 2012");
} catch (ParseException e) {
e.printStackTrace();
}
You can use parse() method of SimpleDateFormat class. Read the API documentation here.
date format of your string-represented date is:
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Use a SimpleDateFormatter. You can find details about them here:
SimpleDateFormat
DateFormat dateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy");

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