Why does this date parsing fail? [duplicate] - java

This question already has answers here:
Is java.time failing to parse fraction-of-second?
(3 answers)
Closed 5 years ago.
I'm trying to convert a string into a LocalDateTime object.
#Test
public void testDateFormat() {
String date = "20171205014657111";
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
LocalDateTime dt = LocalDateTime.parse(date, formatter);
}
I would expect this test to pass.
I get the following error:
java.time.format.DateTimeParseException: Text '20171205014657111' could not be parsed at index 0

Looks like I may have run across this bug: https://bugs.openjdk.java.net/browse/JDK-8031085 as it corresponds to the JVM version I'm using. The workaround in the comments fixes the issue for me:
#Test
public void testDateFormat() {
String date = "20171205014657111";
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
.appendPattern("yyyyMMddHHmmss")
.appendValue(ChronoField.MILLI_OF_SECOND, 3).toFormatter();
LocalDateTime dt = LocalDateTime.parse(date, dtf);
}

Related

Failed to parse date(expiry) data read from CSV file [duplicate]

This question already has an answer here:
How to handle upper or lower case in JSR 310? [duplicate]
(1 answer)
Closed 7 years ago.
I'm trying to parse date time string to LocalDateTime. However if I send month with all caps its thorwning an error, is there any workaround. Here is the below code
#Test
public void testDateFormat(){
DateTimeFormatter formatter= DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse("04-NOV-2015 16:00:00", formatter); //if I send month Nov it works
System.out.println(dateTime.getYear());
}
The Same works for simpleDateFormat
#Test
public void testSimpleDateTime() throws ParseException{
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date dateTime = format.parse("04-NOV-2015 16:00:00");
System.out.println(dateTime.getTime());
}
Answering this question because most of us might not know JSR 310. Hence would search for java 8 LocalDateTime ignore case.
#Test
public void testDateFormat(){
DateTimeFormatter formatter= new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("dd-MMM-yyyy HH:mm:ss").toFormatter();
LocalDateTime dateTime = LocalDateTime.parse("04-NOV-2015 16:00:00", formatter);
System.out.println(dateTime.getYear());
}
**UPDATE*
To locale
DateTimeFormatter parser = new DateTimeFormatterBuilder().parseCaseInsensitive() .appendPattern("dd-MMM-yyyy HH:mm:ss.SS").toFormatter(Locale.ENGLISH)

Retrieving formatted date get error using DateTimeFormatter and SimpleDateFormat [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Why am I getting a parse exception when I try to parse the current LocalDateTime [duplicate]
(2 answers)
How to format LocalDate object to MM/dd/yyyy and have format persist
(4 answers)
Closed 1 year ago.
Here is my INPUT DATE:
String myDate = "2010-02-19";
I want to get OUTPUT from above DATE like below:
Friday, 19-Feb-2010
I tried many way for getting exact output but not success yet.
Here is my code that i trying using DateTimeFormatter and SimpleDateFormat:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String myDate = "2010-02-19";
String strDateTimeFormatter, strSimpleDateFormat;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("EEEE, dd-MMM-yyyy", Locale.US);
LocalDate localDate = LocalDate.parse(myDate, dateTimeFormatter);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, dd-MMM-yyyy");
strSimpleDateFormat = simpleDateFormat.format(myDate);
strDateTimeFormatter = String.valueOf(localDate);
Log.d("TAG", "DateTimeFormatter: "+strDateTimeFormatter);
Log.d("TAG", "SimpleDateFormat: "+strSimpleDateFormat);
}
});
I get java.time.format.DateTimeParseException: Text '2010-02-19' could not be parsed at index 0 from the following line:
LocalDate localDate = LocalDate.parse(myDate, dateTimeFormatter);
How to get the actual OUTPUT that i want?

Unable to parse string into Java 8 LocalDateTime [duplicate]

This question already has answers here:
Is java.time failing to parse fraction-of-second?
(3 answers)
Closed 2 years ago.
Running this gives me the following error, what am I missing ?
public static void main(String[] args) {
DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
LocalDateTime localDateTime = LocalDateTime.parse("20200331094118137",_timestampFomatGMT);
System.out.println(localDateTime);
}
Gives me the following exception. What am I missing ?
Exception in thread "main" java.time.format.DateTimeParseException: Text '20200331094118137' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at cotown.lib.common.util.JavaTimeUtil.main(JavaTimeUtil.java:90)
Java does not accept a plain Date value as DateTime.
Try using LocalDate,
public static void main(String[] args) {
DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate localDateTime = LocalDate.parse("20200331",_timestampFomatGMT);
System.out.println(localDateTime);
}
or if you really have to use LocalDateTime, then try
LocalDateTime time = LocalDate.parse("20200331", _timestampFomatGMT).atStartOfDay();
EDIT
there was a bug for this already raised https://bugs.openjdk.java.net/browse/JDK-8031085.
It is fixed in JDK 9.
You cannot parse a date-only String into a LocalDateTime without passing a time value in addition.
What you can do is use a date-only class like LocalDate similar to your code:
public static void main(String[] args) {
DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate localDate = LocalDate.parse("20200331",_timestampFomatGMT);
System.out.println(localDate);
}
That would simply output
2020-03-31
If you really need to have a LocalDateTime and the String to be parsed cannot be adjusted to include time, then pass an additional time of 0 hours and minutes with an intermediate operation like this (but keep in mind that the output will include the time information as well):
public static void main(String[] args) {
DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate localDate = LocalDate.parse("20200331",_timestampFomatGMT);
LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.of(0, 0));
System.out.println(localDateTime);
}
Or use LocalDateTime time = LocalDate.parse("20200331", _timestampFomatGMT).atStartOfDay(); as suggested by #Shubham.
Output would be:
2020-03-31T00:00
For outputting the date only, change the last line of the last example to
System.out.println(localDateTime.format(DateTimeFormatter.ISO_DATE));
which will only output the date part of the LocalDateTime in an ISO representation:
2020-03-31
EDIT
Targeting your latest question update, this might help:
public static void main(String[] args) {
DateTimeFormatter timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
LocalDateTime localDateTime = LocalDateTime.parse("20200331094118137", timestampFomatGMT);
System.out.println(localDateTime);
}
Output:
2020-03-31T09:41:18.137

Java 8 LocalDate won't parse valid date string [duplicate]

This question already has answers here:
Can't parse String to LocalDate (Java 8)
(2 answers)
Closed 3 years ago.
Java 8 here. I have the following code:
final String createdDateStr = "20110920";
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYYMMdd");
final LocalDate localDate = LocalDate.parse(createdDateStr, formatter);
At runtime I get the following exception:
java.time.format.DateTimeParseException: Text '20110920' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.
...being thrown from the LocalDate.parse(...) invocation. What is wrong with the parser?!
An example from the documentation:
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
String text = date.format(formatter);
LocalDate parsedDate = LocalDate.parse(text, formatter);
You should use "yyyyMMdd" instead of "YYYYMMdd". The difference between Y and y was mentioned here.
No need to specify the format by hand. It’s already built-in.
final String createdDateStr = "20110920";
final DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
final LocalDate localDate = LocalDate.parse(createdDateStr, formatter);
System.out.println(localDate);
This outputs:
2011-09-20

DateTimeParseException; Text '2018-10-18 00:00:00' could not be parsed at index 4 [duplicate]

This question already has answers here:
Java - Converting yyyy-MM-dd'T'HH:mm:ssZ to readable dd-MM-yyyy [duplicate]
(2 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
Closed 4 years ago.
I'm having some difficulty finding the right way to parse a date.
I receive the date as a String in the following format: '2018-10-18 00:00:00'
I need to convert it to 18/10/2018 and store in a variable startDate
I then need a new variable to hold an endDate variable so roll the date forward by a week.
My code:
public String getStartDate(String startDate){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate localStartDate = LocalDate.parse(startDate, formatter);
String startDateFormatted = localStartDate.format(formatter);
return startDateFormatted;
}
public LocalDate getEndDate(String startDate) {
LocalDate localEndDate = LocalDate.parse(getStartDate(startDate)).plusDays(7);
return localEndDate;
}
My error is:
java.time.format.DateTimeParseException: Text '2018-10-18 00:00:00' could
not be parsed at index 4
Index 4 suggests the '-' char. Not sure the formatter pattern for removing the ISO time format that's in the original String
I'm wading through the Javadocs now but can anyone tell me how I can fix?
Your input format is wrong. Try this:
public String getStartDate(String startDate)
{
DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy");
return LocalDate.parse(startDate, inputFormat).format(outputFormat);
}
You need two formatters. One for the input and one for the output:
public String getStartDate(String startDate) {
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate localStartDate = LocalDate.parse(startDate, inputFormatter);
String startDateFormatted = localStartDate.format(outputFormatter);
return startDateFormatted;
}

Categories

Resources