Java Date parsing from specific String format under Android env - java

What's wrong with below date format? I'm getting java.text.ParseException: Unparseable date Thu, 03 May 2012 14:00:00 CEST
String inputDate = "Thu, 03 May 2012 14:00:00 CEST";
SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
Date parsedDate = DATE_FORMATTER.parse(inputDate);
I have tried below combinations but am out of luck:
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz");
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US);
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz", Locale.US);
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
I'm getting the same exception if modify CEST to CET, but not for PST. Any pointer will be appreciated. Thanks.

It works for me too
package test.java;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException {
String inputDate = "Thu, 03 May 2012 14:00:00 CEST";
SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
Date parsedDate = DATE_FORMATTER.parse(inputDate);
System.out.println("Date = " + parsedDate);
}
}
And the output I get is:
Date = Thu May 03 13:00:00 BST 2012

Its working... see this..
- Please correct the typo from inputDateString to inputDate in parse()
Its a Working Java Code... Modify it to suit your Android use....
public class CopyArray {
public static void main(String[] args) {
String inputDate = "Thu, 03 May 2012 14:00:00 CEST";
SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(
"EEE, d MMM yyyy HH:mm:ss z");
try {
Date parsedDate = DATE_FORMATTER.parse(inputDate);
System.out.println(parsedDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}

Aside from the minor issue that you are trying to parse inputDateString when your variable is actually called inputDate, I'm able to run your code with no exceptions.

Related

I want to write a code to validate different types of dates using java but dates with T and Z are still showing Invalid

Here is the code that I have written
Please suggest me if anyone has ever tried something like this.
private static final String[] Date_Reg_Ex = {
"yyyy-MM", "yyyy-MM-dd", "dd-MMMM-yyyy", "dd-MM-yyyy", "MM-dd-yyyy", "dd-MM-yyyy HH:mm:ss/SSS",
"yyyy-MM-dd HH:mm", "yyyy-mm-dd'T'HH:MM:ss", "yyyy-MM-dd'T'HH:mm:ss.SSS",
"yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.S", "yyyy-MM-dd'#'HH:mm:ss",
"yyyy-MM-dd'/'K:mm a", "MM-dd-yyyy hh:mm a", "MM-dd-yyyy hh:mm:ss a", "hh:mm:ss MMM d, yyyy z",
"EEEE dd/MM/yyyy", "EEE, dd MMM yyyy HH:mm:ss zzz", "EEE MMM dd hh:mm:ss z yyyy", "dd MMMMM yyyy",
"MM/dd/yyyy HH:mm", "MM/YYYY", "MM/YY", "dd/MM/yyyy", "yyyy/MM/dd", "MMM, dd/yyyy", "dd.MM.yyyy", "MMM. dd",
"yyyy", "yyyyMMdd", "MMddyyyy", "MMMM yyyy", "ddMMyyyyHHmmss", "yyyy"
};
public static boolean isValidDate(String value, TimeZone timeZone) {
DateValidator dateValidator = DateValidator.getInstance();
for (String dateRegEx : Date_Reg_Ex) {
if (dateValidator.isValid(value, dateRegEx)) {
return true;
}
}
return false;
}
Since you wrote "dates with T and Z are still showing invalid", I suppose you mean:
"yyyy-mm-dd'T'HH:MM:ss"
"yyyy-MM-dd'T'HH:mm:ss.SSS"
and
"hh:mm:ss MMM d, yyyy z"
"EEE, dd MMM yyyy HH:mm:ss zzz"
"EEE MMM dd hh:mm:ss z yyyy"
I could not find any problems with 2,3,4 and 5. Date 1 however switches months and minutes which I guess is not intended.
Here are examples of dates that worked for me:
"yyyy-MM-dd'T'HH:mm:ss" -> "2019-09-12T13:21:31"
"yyyy-MM-dd'T'HH:mm:ss.SSS" -> "2019-09-12T13:21:31.020"
"hh:mm:ss MMM d, yyyy z" -> "1:21:31 Sep 12, 2019 PST"
"EEE, dd MMM yyyy HH:mm:ss zzz" -> "Thursday, 12 Sep 2019 13:21:31 PST"
"EEE MMM dd hh:mm:ss z yyyy" -> "Thursday Sep 12 1:21:31 PST 2019"
It could be related to your local, I tested some example from #Aio and had to force a specific default local to make it succeed I also applied his change to 8th pattern:
I used version 1.6 of Apache Commons Validator
private static final String[] Date_Reg_Ex = {
"yyyy-MM", "yyyy-MM-dd", "dd-MMMM-yyyy", "dd-MM-yyyy", "MM-dd-yyyy", "dd-MM-yyyy HH:mm:ss/SSS",
"yyyy-MM-dd HH:mm", "yyyy-MM-dd'T'HH:mm:ss" /** This has been updated as suggested by Aio **/, "yyyy-MM-dd'T'HH:mm:ss.SSS",
"yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.S", "yyyy-MM-dd'#'HH:mm:ss",
"yyyy-MM-dd'/'K:mm a", "MM-dd-yyyy hh:mm a", "MM-dd-yyyy hh:mm:ss a", "hh:mm:ss MMM d, yyyy z",
"EEEE dd/MM/yyyy", "EEE, dd MMM yyyy HH:mm:ss zzz", "EEE MMM dd hh:mm:ss z yyyy", "dd MMMMM yyyy",
"MM/dd/yyyy HH:mm", "MM/YYYY", "MM/YY", "dd/MM/yyyy", "yyyy/MM/dd", "MMM, dd/yyyy", "dd.MM.yyyy", "MMM. dd",
"yyyy", "yyyyMMdd", "MMddyyyy", "MMMM yyyy", "ddMMyyyyHHmmss", "yyyy"
};
public static boolean isValidDate(String value, TimeZone timeZone) {
DateValidator dateValidator = DateValidator.getInstance();
for (String dateRegEx : Date_Reg_Ex) {
if (dateValidator.isValid(value, dateRegEx)) {
return true;
}
}
return false;
}
public static void main(String... args) {
// without the next line some tests fail
Locale.setDefault(Locale.US);
assertTrue("2019-09-12T13:21:31");
assertTrue("2019-09-12T13:21:31.020");
assertTrue("1:21:31 Sep 12, 2019 PST");
assertTrue("Thursday, 12 Sep 2019 13:21:31 PST");
assertTrue("Thursday Sep 12 1:21:31 PST 2019");
}
private static void assertTrue(String s) {
if (!isValidDate(s, null)) {
System.err.println("Not valid: " + s);
} else {
System.out.println("Valid: " + s);
}
}
This gives me:
Valid: 2019-09-12T13:21:31
Valid: 2019-09-12T13:21:31.020
Valid: 1:21:31 Sep 12, 2019 PST
Valid: Thursday, 12 Sep 2019 13:21:31 PST
Valid: Thursday Sep 12 1:21:31 PST 2019
Process finished with exit code 0
I think there is a typo at yyyy-mm-dd'T'HH:MM:ss.
mm stands for minutes and MM stands for month, here it should be yyyy-MM-dd'T'HH:mm:ss.
Please try correcting it once, I hope you we get the desired result without any program/extra code.
I used SimpleDateFormat for this instead of any external Library,
You can Validate the Date with simply parsing the date and checking an exception -
I suppose you can use something like this -
private static final String[] DATEFORMATS = {"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'","yyyy-MM-dd'T'HH:mm:ss'Z'",
"dd-MM-yy HH:mm:ss","dd-MMM-yy HH:mm:ss","yyyy-MM-dd","yyyy-MMM-dd","dd-MMM-yyyy","dd-MM-yyyy"};
public static Date parseDate(String date ) {
SimpleDateFormat sdf = new SimpleDateFormat();
Date parsedDate = null;
for(String format : DATEFORMATS) {
try {
sdf.applyPattern(format);
parsedDate = sdf.parse(date);
break;
}catch(ParseException e) {
LOG.debug("parseDate:: Not in format::{} Moving to next ",format);
}
}
return parsedDate;
}

Java parsing String date to MilliSecond

How do I convert date string 'Fri Mar 14 09:44:31 IST 2014' to millisec?
I tried with this Java code:
String dateStr = "Fri Mar 14 09:44:31 IST 2014";
SimpleDateFormat sdf = new SimpleDateFormat("MM dd HH:MM:ss");
try {
System.out.println(sdf.parse(dateStr).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
your SimpleDateFormat is not correct.
Try this
SimpleDateFormat sd = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try following code.
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);

java.text.ParseException: Unparseable date Exception while converting timestamp value to time in java

I want to convert a Timestamp value which is passed as String to SimpleDateFormat Object into Time Value but it throws a Unparseable date exception.
The Value which i am passing is Thu Jan 1 17:45:00 UTC+0530 1970
Bur i am getting an Exception as mentioned below:
java.text.ParseException: Unparseable date: "Thu Jan 1 17:45:00 UTC+0530 1970"
Please find the below code which i have implemented(Not Working):
static SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
static SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
static SimpleDateFormat outputFormatTime = new SimpleDateFormat("HH:mm:ss");
public static String convertUtcDateStringToTime(String utcDateValue) throws Exception
{
Date parsedDate = dateFormat.parse(utcDateValue);
String returnDate=outputFormatTime.format(inputFormat.parse(parsedDate.toString()));
return returnDate;
}
If i use the below code it works fine for me(Working) but its a depreciated function of Date which i want to avoid..
#SuppressWarnings("deprecation")
public static String convertUtcDateStringToTime(String utcDateValue) throws Exception
{
Date dateValue=new Date(utcDateValue);
Date parsedDate = dateFormat.parse(dateValue.toString());
String returnDate=outputFormatTime.format(inputFormat.parse(parsedDate.toString()));
return returnDate;
}
Please Guide Me To implement the logic where i have missed. Thanks in advance.
with an addition to the answers if the formatting string is like this
"EEE MMM dd HH:mm:ss z yyyy"
then your input string should be
"Thu Jan 1 17:45:00 +0530 1970"
note that the "UTC" is skipped as implicitly it refers to the RFC 822 time zone
First of all, your 2nd SimpleDateFormat object, is not needed at all. You are doing the extra work, which is not needed. So, remove this variable:
static SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US); // Not needed.
Secondly DateFormat#format(Date) methods takes a Date object. You are passing it a String. That wouldn't work. That is why you don't need the above object. There is no need to do a inputFormat.parse(parsedDate.toString()) again.
Now, the format to parse your current string should be:
"EEE MMM dd HH:mm:ss 'UTC'z yyyy"
You need to give the UTC in quotes, before z. Or for more general case:
"EEE MMM dd HH:mm:ss zZ yyyy"
So, your code should be like:
static SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zZ yyyy", Locale.US);
static SimpleDateFormat outputFormatTime = new SimpleDateFormat("HH:mm:ss");
public static String convertUtcDateStringToTime(String utcDateValue) throws Exception
{
Date parsedDate = dateFormat.parse(utcDateValue);
String returnDate=outputFormatTime.format(inputFormat);
return returnDate;
}
You input dateformat needs to be
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zZ yyyy", Locale.US);
The other formatting is all upto, you based on your requirements.

Correct dateformat for String

I'm receiving an exception when parsing the following date:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date parsedDate=null;
try {
parsedDate=sdf.parse("Thu Jan 26 15:05:48 COT 2012");
} catch (ParseException e) {
e.printStackTrace();
}
Is the pattern incorrect? What would be the correct form to parse the date string?+
The default SimpleDateFormat constructor doesn't support all Locales.
You would have to specify the Locale:
SimpleDateFormat sdf = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
Date parsedDate = null;
try {
parsedDate = sdf.parse("Thu Jan 26 15:05:48 COT 2012");
System.out.println(parsedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Try using only one z:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Have you ever tried a single 'z' instead of a triple 'z' ?
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Should become
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Perhaps you need to use CO instead of COT for the timezone?

Simpledateformat parse issue

I'm parsing a date which is in format EEE, dd MMM yyyy HH:mm:ss Z. One of the sample values is Thu, 02 Sep 2010 04:03:10 -0700.
This is the parsing code:
SimpleDateFormat FORMATTER = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Date date = FORMATTER.parse(dateString);
This works absolutely fine if Phone Language is English but it throws parserException when language is changed to "France" or "Italian". I have even tried this:
SimpleDateFormat FORMATTER = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.getDefault());
or for French locale more explicit:
SimpleDateFormat FORMATTER = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.French);
But no luck.....Can someone tell me what I am doing wrong?
Since "Thu" is English, you'll want to use Locale.ENGLISH to parse it.

Categories

Resources