Java datetime parse exception: text cannot be parsed [duplicate] - java

This question already has an answer here:
Exception when trying to parse a LocalDateTime
(1 answer)
Closed 2 years ago.
This piece of code is working fine on my old computer. However, once I moved it to this computer , i received this exception. I looked into the possible answers from other posts, i.e. adding Locale.US do not work.
Basically,
Locale locale = new Locale("en", "US");
DateTimeFormatter dtformatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS",locale);
LocalTime time = LocalTime.parse("20190502050634678",dtformatter);
Exception in thread "main" java.time.format.DateTimeParseException: Text '20190502050634678' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalTime.parse(Unknown Source)
I am not sure how do I overcome this ? I removed Locale and that do not work either.

This is because Java failed to parse the fraction of a second part. There is a bug in the jdk thats not fixed until Java 9. Probably that's why it fails when you move the program from one to another computer, because they used different Java runtime. You can update the runtime on your other computer to Java 9.
Or use the following code.
Locale locale = new Locale("en", "US");
DateTimeFormatter formatter =
new DateTimeFormatterBuilder()
.appendPattern("yyyyMMddHHmmss")
.appendValue(ChronoField.MILLI_OF_SECOND, 3)
.toFormatter();
LocalTime time = LocalTime.parse("20190502050634678",formatter);
Solution is borrowed from here Is java.time failing to parse fraction-of-second? .

Related

ParseException - Unparseable date exception in Java [duplicate]

This question already has answers here:
Why can't this SimpleDateFormat parse this date string?
(4 answers)
ParseException when parsing 3 character abbreviated month using SimpleDateFormat
(5 answers)
Closed 2 years ago.
I have oracle database with columns data type "TIMESTAMP(0) WITH TIME ZONE" . Which produces
like 14/11/2019 06:30:00, +03:00 in database .
When I am trying to get this timestamp from java
I got java.text.ParseException: Unparseable date: "28-MAY-12 07.40.03 PM +03:00" . Our project java version is 6 . I tried with
new SimpleDateFormat("dd-MM-yy hh.mm.ss") ,
new SimpleDateFormat("dd-MM-yy hh.mm.ss, Z"),
new SimpleDateFormat("dd-MM-yy hh.mm.ss, z") ,
new SimpleDateFormat("dd/MM/yy hh.mm.ss, z") ,
new SimpleDateFormat("dd/MM/yy hh.mm.ss, Z") ,
new SimpleDateFormat("dd/MM/yy hh.mm.ss")
But none of them worked for me all of throwing java.text.ParseException: Unparseable date error.
In my java side i have date datatype property in my class and i want to retrieve data from oracle database then parse then set to date data type property like below.
serDto.setTarih(new Timestamp(new SimpleDateFormat("dd-MM-yy hh.mm.ss").parse(str).getTime()));
For ex in db timestamp data looks like : 14/11/2019 06:30:00, +03:00 and i want to retrieve as date like above.
Thank you in advance.
A partial solution is changing the pattern so that it parses the name of the month and the am/pm marker. Change MM to MMM and add a:
new SimpleDateFormat("dd-MMM-yy hh.mm.ss a").parse(str)
However this will ignore the time zone information. It will return a date with the expected time (i.e. 07:40:03 PM) in the default local time zone where this code is running, which might be different from the time zone in the input GMT+0300.
There doesn't seem to be a way to parse +03:00 into a time zone with SimpleDateFormat in Java 6, so if you need to parse the time zone this will require more custom code.

Cannot parse date time, issue with parsing pattern "ssSSS" [duplicate]

This question already has answers here:
Is java.time failing to parse fraction-of-second?
(3 answers)
Closed 3 years ago.
So I am trying to parse:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
TemporalAccessor parse = dateTimeFormatter.parse("20180521073438514");
And I receive the following error:
"Text '20180521073438514' could not be parsed at index 0"
But when I try to add sth that will separate "ss" and "SSS" it works:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss.SSS");
TemporalAccessor parse = dateTimeFormatter.parse("20180521073438.514");
Am I missing some kind of delimiter to separate "ss" and "SSS"?
This is a bug in Java 8 which was fixed in Java 9. Have a look at the official bug report https://bugs.openjdk.java.net/browse/JDK-8031085

Date time parse exception in previously working code [duplicate]

This question already has answers here:
java DateTimeFormatterBuilder fails on testtime [duplicate]
(2 answers)
Closed 4 years ago.
I have this code fragment which worked previously for half year ( I wrote it myself ).
Yesterday I received new laptop with windows 10 ( previously 8.1 ) installed the most recent Java JDK jdk1.8.0_181 and this code stopped working with error.
Is it something I was missing for the whole time or there were some changes in java internal API ?
How I can fix it ? I believe it was written properly.
Caused by: java.time.format.DateTimeParseException: Text '29-Apr-2010,13:00:14' could not be parsed at index 3
private static final DateTimeFormatter PP_FORMATTER = DateTimeFormatter.ofPattern("d-MMM-yyyy,HH:mm:ss");
private static final LocalDate DATE = LocalDate.parse("29-Apr-2010,13:00:14", PP_FORMATTER);
try to add Locale.US
private static final DateTimeFormatter PP_FORMATTER = DateTimeFormatter.ofPattern("d-MMM-yyyy,HH:mm:ss", Locale.US);
otherwise, you maybe able to parse only numeric format for the month.

'Calendar.getDisplayName' giving 'IllegalArgumentException' with 'Unknown Source'

Problem- My application is used in a production environment across a few hundred computers. The issue arose after installing some new computers and finding out that my application was crashing on ONLY the new computers. The new computers came with JAVA v8u5, in attempts to fix the issue I also installed v7u55 (I thought it might be a versioning issue).
NOTE* I'm compiling using JDK v7u45 **
Root Cause- The failing code is a call to the Calendar.getDisplayName(int, int, Locale)
Code That is failing:
System.out.println("Getting calendar instance");
Calendar instanceCalendarObj = Calendar.getInstance();
String date = instanceCalendarObj.getDisplayName(Calendar.MONTH, 0, Locale.ENGLISH);
date = date.concat(" "+String.valueOf(instanceCalendarObj.get(Calendar.DAY_OF_MONTH)));
date = date.concat(", "+String.valueOf(instanceCalendarObj.get(Calendar.YEAR)));
JOptionPane.showMessageDialog(this, date);
Error Message:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException
at java.util.Calendar.checkDisplayNameParams(Unknown Source)
at java.util.Calendar.getDisplayName(Unknown Source)
Any help would be greatly appreciated, even if its just a workaround.
You pass a wrong parameter to the getDisplayName() method.
The second parameter is the style, whose possible values are Calendar.SHORT and Calendar.LONG. Use these constants as seen below:
Calendar c = Calendar.getInstance();
c.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.ENGLISH);
c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH);
Btw, the constant values for Calendar.SHORT and Calendar.LONG are 1 and 2 (and you passed 0 in your code). But always use the constant names and not their values!

convert timestamp to ISO860 in java, [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting ISO8601-compliant String to java.util.Date
I have issues when changing the timestamp to ISO 8601 in JS as it errors at 'topicDate' in IE and Firefox, but it works in Chrome. So i want to change the timestamp to ISO 8601 in the server side and send that via json instead. Can anyone help me how to convert the below time stamp to ISO 8601 format in Java using standard classes? Any other suggesting about this approach is also welcomed.
Time sent via json
"topic_lstUpdate" : "2012-09-07 19:39:56.439",
JS script
var topicDate = new Date(args.topic_lstUpdate);
var topicDateISO = topicDate.toISOString();
var topicDateTimeago=jQuery.timeago(topicDate);
To format within Java on the server-side:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String my8601formattedDate = df.format(new Date());
It is recommended that you include the T delimiter - but if you're certain your requirements on both ends permit excluding it, you are permitted to omit it. See http://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations for details.
It's almost there already. You can use this JavaScript to convert it:
topicDate.replace(" ", "T");

Categories