This question already has answers here:
java.time.format.DateTimeParseException, Date could not be parsed
(1 answer)
Create LocalDate Object from Integers
(3 answers)
Invalid localdate when it created
(1 answer)
Can't parse String to LocalDate (Java 8)
(2 answers)
Difference between year-of-era and week-based-year?
(7 answers)
Closed 3 months ago.
I want to convert date from String to LocalDate object: Starting date as String: "12/11/2022" I want to convert this String to LocalDate. I want to get LocalDate object and next use this LocalData inside if-else conditions to convert this object to String and return String. All conditions return first LocalData and next String as result: if I use this code with: date = LocalDate.parse(default)};
public String getStringDate(String timeProjection){
if (timeProjection.equals("1") {
date = LocalDate.now();
}
....
else {
String default = "12/11/2022";
date = LocalDate.parse(default);
}
command
return date.format(DataTimeFormatter.ofPattern("dd/MM/YYYY"));
I get issue that is impossible this parse, but when I use:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/YYYY");
String date = "16/08/2016";
LocalDate localDate = LocalDate.parse(date, formatter);
This convertion works good. What do you think is possible create solution when I can't use twice this formatter: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/YYYY") - first in else body, second in result command? Thanks for your help.
Related
This question already has answers here:
How to format LocalDate object to MM/dd/yyyy and have format persist
(4 answers)
Java - How to get the correct date format with LocalDate
(2 answers)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Closed 3 months ago.
Can I know how can convert one date format to another date format.
public static LocalDate localDateToAnotherLocalDate(String oldPattern, String newPattern, String input) {
DateTimeFormatter oldFormat = DateTimeFormatter.ofPattern(oldPattern);
DateTimeFormatter newFormat = DateTimeFormatter.ofPattern(newPattern);
LocalDate localDate = LocalDate.parse(input, oldFormat);
String output = localDate.format(newFormat);
System.out.println();
return getLocalDate(output, newPattern);
}
public static LocalDate getLocalDate(String date, String datePattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(datePattern);
return LocalDate.parse(date, formatter);
}
#Test
void getLocalDateToAnotherLocalDateTest() {
LocalDate localDate = DateUtil.localDateToAnotherLocalDate("yyyy-MM-dd", "dd-MM-yyyy", "2022-11-20");
System.out.println("localDate" + localDate.toString());
assertEquals("20-11-2022", localDate.toString());
}
A LocalDate object can only ever be printed in ISO8601 format (yyyy-MM-dd). In order to print the object in some other format, you need to format it and save the LocalDate as a string.
This question already has answers here:
Why am I getting a parse exception when I try to parse the current LocalDateTime [duplicate]
(2 answers)
How to convert a string date "2019-04-21T12:08:35" to SimpleDateFormat, there by convert to Date?
(2 answers)
Can’t rid of 'T' in LocalDateTime
(3 answers)
Closed 1 year ago.
I get a datetime string from an C# API that looks like this "2021-05-07T08:58:15.993". I am trying to convert it to "2021-05-07 08:58 AM" for my android app. I am using this code:
fun convertStringToDate(stringDate: String): LocalDate? {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm ", Locale.ENGLISH)
val date = LocalDate.parse(stringDate, formatter)
return date
}
but it gives me an error of DateTimeParseException "2021-05-07T08:58:15.993" could not be parsed at index 10.
Index 10 is the 'T' character. I could just hack it and just remove the T and remove the .993, but, what if the date format changes from the API?
This question already has answers here:
Parse String to date with timezone offset intact
(1 answer)
Java date offset format issue?
(4 answers)
DateTimeFormatter create pattern [duplicate]
(1 answer)
Closed 2 years ago.
Convert string '2018-08-27T16:40:02+08:00' to datetime using DateTimeFormatter
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
String rawDateString = "2018-08-27T16:40:02+08:00"
DateTimeFormatter dateTimeFormatter =
DateTimeFormatter.ofPattern(DATE_TIME_FORMAT, Locale.ENGLISH);
LocalDateTime localDateTime = LocalDateTime.parse(rawDateString, dateTimeFormatter);
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
but this returns DateTimeParseException: Text could not be parsed at index 19
I saw some similar issue like this
DateTimeParseException: Text could not be parsed at index 2
but I dont see any sample with timezone on datetime format.
You neeed to specify timezone offset in your format as well. Like that:
String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssXXX";
See the link bellow:
https://www.journaldev.com/17899/java-simpledateformat-java-date-format
This question already has answers here:
How to format LocalDate object to MM/dd/yyyy and have format persist
(4 answers)
How can you make LocalDate to return Date in a specific format?
(1 answer)
Closed 2 years ago.
In a need to convert Java LocalDate of the format of dd-MM-yyyy into a LocalDate of dd/MM/yyyy.
Trying with :
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date = // LocalDate value in dd-MM-yyyy format
String stringDate = dateFormat.format(date);
LocalDate convertedDate = LocalDate.parse(stringDate, dateFormat);
But still it resulting into return a date in dd-MM-yyyy format. Any efficient way to do this?
The default toString implementation in LocalDate.java seems to be hardwired with '-' as a separator. So all default print statements will result into same format. Seems only way out would be to use a formatter and get a string output.
return buf.append(monthValue < 10 ? "-0" : "-")
.append(monthValue)
.append(dayValue < 10 ? "-0" : "-")
.append(dayValue)
.toString();
Also, purpose of LocalDate.parse(..) is not to convert the date format. It's actually meant to just get a date value in String and give a resulting LocalDate instance.
Hope this helps.
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;
}