Date time parse exception in previously working code [duplicate] - java

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.

Related

Getting exception on LocalDateTime.parse while using java8 with java11 works fine [duplicate]

This question already has an answer here:
Exception when trying to parse a LocalDateTime
(1 answer)
Closed 4 years ago.
Basically, I am trying to parse a string to a timestamp.
public static void main(String[] args) {
System.out.println("Timestamp:" + DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS").parse("20180301050630663"));
}
I got an exception saying that
Exception in thread "main" java.time.format.DateTimeParseException: Text '20180301050630663' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at Lob.main(Lob.java:41)
Then I tried doing this:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
LocalDateTime timestamp = LocalDateTime.parse("20180301050630663", fmt);
System.out.println("Timestamp:" + timestamp);
And got the same exception error too.
What have I done wrong here? Ideally, I want to store the timestamp to a variable and compare it with another timestamp that I am reading in . How can I achieve that ?
A bug of JDK8 and has been resolved in JDK9:
https://bugs.java.com/view_bug.do?bug_id=JDK-8031085
Update
As User #Aaron said in comment, you can use the workaround that is included in the bug tracker IMO:
public static void main(String[] args) {
DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendPattern("yyyyMMddHHmmss").appendValue(ChronoField.MILLI_OF_SECOND, 3).toFormatter();
System.out.println("Timestamp:" + dtf.parse("20180301050630663"));
}
Looks like it's a bug. That code works in Java 10 (and 9, it seems).
You can go around the issue using java.text.SimpleDateFormat:
new java.text.SimpleDateFormat('yyyyMMddHHmmssSSS').
parse("20180301050630663")
Which works just fine (Thu Mar 01 05:06:30 SAST 2018 is the output in my TZ):

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

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

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

Why are the get methods crossed out in this code? [duplicate]

This question already has answers here:
Code fragments are being struck out in Eclipse, why?
(3 answers)
Closed 7 years ago.
I have a piece of code here to calculate someone's age with the SQL date type in java.
The code works fine, but however you can't see it in the code pasted here, but in my netbeans environment the get methods are crossed out, but only in this line:
LocalDate datumDB = LocalDate.of(gbdat.getYear() + 1900, gbdat.getMonth() + 1,
gbdat.getDate());
someone any idea why?
this line isn't crossed out:
int leeftijd = datumVanVandaag.getYear() - datumDB.getYear();
this is my code:
private void checkSpelerGeschiktVoorPloeg(Persoon p) {
Date gbdat = p.getGbDatum();
LocalDate datumVanVandaag = LocalDate.now();
LocalDate datumDB = LocalDate.of(gbdat.getYear() + 1900, gbdat.getMonth() + 1, gbdat.getDate());
int leeftijd = datumVanVandaag.getYear() - datumDB.getYear();
}
The strikeout indicates that those methods are deprecated. You should use either java.util.Calendar or the new Java 8 Datetime API (which is the new-and-improved API that the LocalDate you're using is part of) to calculate these values for a date.
For instance the API document says:
#Deprecated
public int getYear()
Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900.
Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.

How to remove "00:00:00.0" from "2012-03-04 00:00:00.0" in java? [duplicate]

This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
Closed 7 years ago.
I hava java program , the program get table 'Employees' from oracle Database 11G
the problem is ,
java program get the Hire_Date Column in this format “YYYY-MM-DD 00:00:00.0”
I want get the date in this format “YYYY-MM-DD"
How can I solve This Problem ?
the photo will explian the problem
http://i.imgur.com/4CT7MnG.png
You can do this as part of your select statement:
SELECT TO_CHAR(HIRE_DATE, 'DD-MON-YYYY') HIRE_DATE_TRUNC FROM EMPLOYEES;
Use of to_char to format dates is described here. Alternatively, you could use a SimpleDateFormat to do the same thing in Java after the data has been extracted:
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
Date d = new Date();
String formatted = sdf.format(d);
System.out.println(formatted);

Categories