Can't convert java Date default format - java

I'm trying to convert JSON to Java Object (transaction) and vice versa.
I keep getting this exception:
java.text.ParseException: Unparseable date: "Sun Apr 28 02:41:11 IDT 2019"
at this line in my code below:
timeReceived = sdf.parse(json.get("timeReceived").toString());
even though I read the Date class description a lot of times and the format I
used should match the date received.
Would appreciate some assistance. thanks!
My code:
public Transaction convertJsonToTransaction(JSONObject json){
UUID uuid= UUID.fromString(json.getAsString("uuid"));
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss z
yyyy");
Date timeReceived= null;
try {
timeReceived = sdf.parse(json.get("timeReceived").toString());
} catch (ParseException e) {
e.printStackTrace();
}
String recipient =json.get("recipient").toString();
Date timeSent= null;
try {
timeSent = sdf.parse(json.get("timeSent").toString());
} catch (ParseException e) {
e.printStackTrace();
}
String description=json.getAsString("description");
return new Transaction(uuid, sender, timeReceived, recipient,
timeSent, description);
}
public static void main (String args[]){
Transaction t = new Transaction(UUID.randomUUID(), "ms1", new
Date(), "ms2", new Date(), "flow");
net.minidev.json.JSONObject jo = t.convertTransactionToJson();
System.out.println(((JSONObject) jo).toString());
Transaction tr = t.convertJsonToTransaction(jo);
System.out.println(tr.toString());
}

You have a really small error in the date format: an extra space between date and hour placeholder EEE MMM d HH:mm:ss z yyyy. Get rid of that and it works EEE MMM d HH:mm:ss z yyyy

You have \n in regex. This thing happened many times with me. Just a suggestion when you found this type of issue go to any online tool to check special or hidden character in your string (Some time hidden char char will be there, you can not found them using your eyes.). Also one thing If you copy regex or any string used in program from any tool or online or any doc.., before pasting it in your code check if you copied any special char or hidden char. I normally use this tool.
Every thing else is correct It is working for me.

Related

java.time.LocalDate allow invalid value in date [duplicate]

I wanted to validate date in client side so I wrote the following code. But instead of getting an exception I am getting a proper date object for 31st of February date string, which is clearly an invalid date.
public class Test {
public static void main(String[] args) {
String dateFormat = "HH:mm:ss MM/dd/yyyy";
String dateString = "11:30:59 02/31/2015";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormat, Locale.US);
try {
LocalDateTime date = LocalDateTime.parse(dateString, dateTimeFormatter);
System.out.println(date);
} catch (Exception e) {
// Throw invalid date message
}
}
}
Output : 2015-02-28T11:30:59
Does anyone know why LocalDateTime is parsing this date instead of throwing an exception.
You just need a strict ResolverStyle.
Parsing a text string occurs in two phases. Phase 1 is a basic text parse according to the fields added to the builder. Phase 2 resolves the parsed field-value pairs into date and/or time objects. This style is used to control how phase 2, resolving, happens.
Sample code - where withResolverStyle(ResolverStyle.STRICT) is the important change, along with the use of uuuu rather than yyyy (where uuuu is "year" and "yyyy" is "year of era", and therefore ambiguous):
import java.time.*;
import java.time.format.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
String dateFormat = "HH:mm:ss MM/dd/uuuu";
String dateString = "11:30:59 02/31/2015";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern(dateFormat, Locale.US)
.withResolverStyle(ResolverStyle.STRICT);
try {
LocalDateTime date = LocalDateTime.parse(dateString, dateTimeFormatter);
System.out.println(date);
} catch (DateTimeParseException e) {
// Throw invalid date message
System.out.println("Exception was thrown");
}
}
}
The Java 8 DateTimeFormatter uses yyyy to mean YEAR_OF_ERA, and uuuu to mean YEAR. You need to modify your pattern string as follows:
String dateFormat = "HH:mm:ss MM/dd/uuuu";
The DateTimeFormatter defaults to using the SMART resolver style, but you want it to use the STRICT resolver style. Modify your dateTimeFormatter initialization code as follows:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormat, Locale.US)
.withResolverStyle(ResolverStyle.STRICT);
It is not rounding down. February has never had 31 days, and it is impossible to use a validating date / time object to represent a day that doesn't exist.
As a result, it takes the invalid input and gives you the best approximation to the correct date (the last date of February that year).
SimpleDateFormat inherits from DateFormat which has a setLenient(boolean value) method on it. I would expect that if you called setLenient(true) prior to parsing, it would probably complain more, as detailed in the javadocs.
try {
SimpleDateFormat df = new java.text.SimpleDateFormat("HH:mm:ss MM/dd/yyyy");
df.setLenient(false);
System.out.println(df.parse("11:30:59 02/29/2015"));
} catch (java.text.ParseException e) {
System.out.println(e);
}
I found one solution to recognize date as a valid date with DateFormat.setLenient(boolean). If you try to parse any invalid date it will throws parse exception.
Edit:
Java 8, but this will raise exception if a month is not between 1 and 12, if a day is more than 32. Exactly not working. But for month its working.
try {
TemporalAccessor ta = DateTimeFormatter.ofPattern("HH:mm:ss MM/dd/yyyy").parse("11:30:59 02/32/2015");
} catch (Exception e) {
System.out.println(e);
}
Output:
java.time.format.DateTimeParseException: Text '11:30:59 02/32/2015' could not be
parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 32
LocalDateTime.parse will only throw an error if the String passed in contains invalid characters, a number of days exceeding 31 or a month exceeding 12.
For example, if you modified your code as such:
String dateString = "11:30:59 0zz2/31/2015";
an exception would be thrown caused by the invalid 'zz' characters within your given date. As to why it's 'rounding-down' the date so to speak, that I don't know.
Source: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#parse-java.lang.CharSequence-

Java 8 LocalDateTime is parsing invalid date

I wanted to validate date in client side so I wrote the following code. But instead of getting an exception I am getting a proper date object for 31st of February date string, which is clearly an invalid date.
public class Test {
public static void main(String[] args) {
String dateFormat = "HH:mm:ss MM/dd/yyyy";
String dateString = "11:30:59 02/31/2015";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormat, Locale.US);
try {
LocalDateTime date = LocalDateTime.parse(dateString, dateTimeFormatter);
System.out.println(date);
} catch (Exception e) {
// Throw invalid date message
}
}
}
Output : 2015-02-28T11:30:59
Does anyone know why LocalDateTime is parsing this date instead of throwing an exception.
You just need a strict ResolverStyle.
Parsing a text string occurs in two phases. Phase 1 is a basic text parse according to the fields added to the builder. Phase 2 resolves the parsed field-value pairs into date and/or time objects. This style is used to control how phase 2, resolving, happens.
Sample code - where withResolverStyle(ResolverStyle.STRICT) is the important change, along with the use of uuuu rather than yyyy (where uuuu is "year" and "yyyy" is "year of era", and therefore ambiguous):
import java.time.*;
import java.time.format.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
String dateFormat = "HH:mm:ss MM/dd/uuuu";
String dateString = "11:30:59 02/31/2015";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofPattern(dateFormat, Locale.US)
.withResolverStyle(ResolverStyle.STRICT);
try {
LocalDateTime date = LocalDateTime.parse(dateString, dateTimeFormatter);
System.out.println(date);
} catch (DateTimeParseException e) {
// Throw invalid date message
System.out.println("Exception was thrown");
}
}
}
The Java 8 DateTimeFormatter uses yyyy to mean YEAR_OF_ERA, and uuuu to mean YEAR. You need to modify your pattern string as follows:
String dateFormat = "HH:mm:ss MM/dd/uuuu";
The DateTimeFormatter defaults to using the SMART resolver style, but you want it to use the STRICT resolver style. Modify your dateTimeFormatter initialization code as follows:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormat, Locale.US)
.withResolverStyle(ResolverStyle.STRICT);
It is not rounding down. February has never had 31 days, and it is impossible to use a validating date / time object to represent a day that doesn't exist.
As a result, it takes the invalid input and gives you the best approximation to the correct date (the last date of February that year).
SimpleDateFormat inherits from DateFormat which has a setLenient(boolean value) method on it. I would expect that if you called setLenient(true) prior to parsing, it would probably complain more, as detailed in the javadocs.
try {
SimpleDateFormat df = new java.text.SimpleDateFormat("HH:mm:ss MM/dd/yyyy");
df.setLenient(false);
System.out.println(df.parse("11:30:59 02/29/2015"));
} catch (java.text.ParseException e) {
System.out.println(e);
}
I found one solution to recognize date as a valid date with DateFormat.setLenient(boolean). If you try to parse any invalid date it will throws parse exception.
Edit:
Java 8, but this will raise exception if a month is not between 1 and 12, if a day is more than 32. Exactly not working. But for month its working.
try {
TemporalAccessor ta = DateTimeFormatter.ofPattern("HH:mm:ss MM/dd/yyyy").parse("11:30:59 02/32/2015");
} catch (Exception e) {
System.out.println(e);
}
Output:
java.time.format.DateTimeParseException: Text '11:30:59 02/32/2015' could not be
parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 32
LocalDateTime.parse will only throw an error if the String passed in contains invalid characters, a number of days exceeding 31 or a month exceeding 12.
For example, if you modified your code as such:
String dateString = "11:30:59 0zz2/31/2015";
an exception would be thrown caused by the invalid 'zz' characters within your given date. As to why it's 'rounding-down' the date so to speak, that I don't know.
Source: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#parse-java.lang.CharSequence-

Parsing a date in Korean

I am trying to parse a date in Korean Date format using SimpleDateFormat which works. However i would like to remove any dependency on adding up Korean Characters in Pattern for Year(년), Month(월) and Day(일) and so on.
String dateinKorean = "2013년 9월 26일 (목)";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy'년' M'월' d'일' '('EE')'", Locale.KOREA);
try {
Date dt = sdf.parse(dateinKorean);
System.out.println(dt.toGMTString());
} catch (ParseException e) {
e.printStackTrace();
}
I am trying to use DateFormatSymbols Class to Parse the date using the Locale, however the problem is I am not able to parse the complete date, I can parse an individual Month(MM), Year(yyyy) or Day(dd) without any issues.
DateFormatSymbols df = DateFormatSymbols.getInstance(Locale.KOREAN);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd", df);
try {
// 2013년 9월 26일
Date dt = sdf.parse("2013년 9월 26일");
} catch (ParseException e) {
}
Can anyone please help me identify if there is any other way to parse the dates other than shown above ?
If the date format includes "fixed" characters they are used as-is, so if you include words like "year" or "month" in the format they cannot be ignored.
You can hack your way around this by removing all non-digits from the string before parsing, for example:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", df);
Date dt = sdf.parse("2013년 9월 26일".replaceAll("\\D+", "-"));
A better solution could be internationalizing the date format so that each language you support could have a format of its own.

SimpleDateFormat - Unparseable?

Take a look at my code:
try {
// String date = "30Jul2013";
SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH);
Date d = sdf.parse(date);
SimpleDateFormat nsdf = new SimpleDateFormat("MMMM dd, yyyy h:mm a", Locale.ENGLISH);
String nd = nsdf.format(d);
System.out.println(nd);
return nd;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Im am getting a error:
java.text.ParseException: Unparseable date: "2013-07-30 10:58:55.171"
at java.text.DateFormat.parse(DateFormat.java:337)
I would like to have an output of July 30, 2013 11:10 AM from the simpledateformat. There's LOCALE in my code. So what else should I do?
Thanks in advance!
try {
// String date = "30Jul2013";
SimpleDateFormat sdf = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH);
Date d = sdf.parse(date);
Your date String variable line is commented out, so who's to know what String you're parsing? -- the JVM that's who.
As Robert Harvey points out, the String that you're actually trying to parse is printed for you in the exception message. If you print that String before you parse you'll also see that it's not what you expect it is and that the compiler's right.
In sum, you are somehow expecting that your sdf SimpleDateFormat object is formatting a String of a format similar to "30Jul2013", but the JVM is telling you that this simply is not so, that the String you are trying to parse in fact looks nothing like this, but rather is "2013-07-30 10:58:55.171".

java : java.text.ParseException: Unparseable date

I am reciving a input in this format 2012-01-13T00:00:00.000-05:00 and which i need to convert this into yyyyMMdd Format .
I have also set the SimpleDateFormat.setLenient(false);
This is my coding for parsing the Date
public static String getparsedDate(String date) throws Exception {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);
sdf.setLenient(false);
String s1 = date;
String s2 = null;
Date d;
try {
d = sdf.parse(s1);
s2 = (new SimpleDateFormat("yyyyMMdd")).format(d);
} catch (ParseException e) {
e.printStackTrace();
}
return s2;
}
But i am getting a Exception at
java.text.ParseException: Unparseable date: "201201"
at java.text.DateFormat.parse(Unknown Source)
Could anybody please let me know , what might be the issue ?
You are missing the timezone in your format string. If you check the argument, it is finishing with -05:00 and you are also using Lenient==false.
Unfortunately, the time zone formats available to SimpleDateFormat are not ISO8601 compliant. SimpleDateFormat understands time zone strings like "GMT+01:00" or "+0100", the latter according to RFC822. Therefore using SimpleDateFormat does not seem as an option in your case (since you use UTC−05:00 as timezone).
Instead of SimpleDateFormat you need to use JodaTime for that type of date format.

Categories

Resources