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.
Related
This question already has answers here:
Why am I getting a parse exception when I try to parse the current LocalDateTime [duplicate]
(2 answers)
Change date format in a Java string
(22 answers)
How to format LocalDate object to MM/dd/yyyy and have format persist
(4 answers)
Parsing LocalDate but getting DateTimeParseException; dd-MMM-uuuu
(2 answers)
Closed 27 days ago.
I am getting error as "java.time.format.DateTimeParseException: Text '2023-01-25' could not be parsed at index 0" when passing string "2023-01-25" to parse method.
String date = "2023-01-25";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MMM dd, yyyyy");
LocalDate localDate = LocalDate.parse(date, dateTimeFormatter);
String dueDate = localDate.toString();
System.out.println(dueDate);
I want to display result as "Jan 25, 2023"
You need 2 different DateTimeFormatter one to parse your input String into a LocalDate. Then a second to format your LocalDate into the wanted String.
String inputString = "2023-01-25";
DateTimeFormatter parser = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse(inputString, parser);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy");
String outputString = date.format(formatter);
System.out.println(outputString );
Your format that you created DateTimeFormatter.ofPattern("MMM dd, yyyyy") is how you want to display your date. Yet you attempt to parse the String "2023-01-25" to LocalDate with your format and this obviously fails since your format expects the String to start with 3-letter month name but it starts with "2023". So you need to create 2 formats - one for parsing a String to LocalDate and in your case the pattern should be "yyyy-MM-dd". Once you get your LocalDate you can format it to String with your formatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyyy");
String outputString = date.format(formatter);
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.
This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 2 years ago.
How to parse this date string in java
"2020-06-12T00:00:00.000+00:00"
I tried the following code :
public static String convertToStandardDateString(String date) {
// 2020-06-12T00:00:00.000+00:00
String resDate = null;
try {
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+'X");
Date parsedDate = sdf.parse(date);
resDate = sdf.format(parsedDate);
} catch (Exception e) {
}
return resDate;
}
I am getting the ParsingException with the above code.
Stop using the deprecated java.util.Date and start using the java-8 date-time api. The date string you have represents date time with offset, so you can parse it directly into OffsetDateTime
A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00.
OffsetDateTime offsetDateTime = OffsetDateTime.parse("2020-06-12T00:00:00.000+00:00");
This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
Java string to date conversion
(17 answers)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Closed 3 years ago.
I have string with format : '20018-03-03 11:00:00', and i want to convert to Date but keeping this format. Is this possible? Because when I do something like this :
Date.parse(string), I don't get this format, event when I use
SimpleDateFormat. What I'm doing wrong ?
I tried this:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.parse(entry.getValue(), formatter);
String formatDateTime = now.format(formatter);
// Date date = df.parse(sqlDate);
Date d = (Date) formatter.parse(formatDateTime);
It might not be the case in other languages, but in Java the format used to parse a date is not stored in the date itself. Thus you have to reuse the same format when you want to print (format) a date.
Old (Date, SimpleDateFormat) and new (LocalDateTime, etc.) API should not be mixed together. Stick to the new one unless you have legacy code to deal with.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// Parse: String -> LocalDateTime
LocalDateTime now = LocalDateTime.parse("2018-03-03 11:00:00", formatter);
// Format: LocalDateTime -> String
System.out.println(now.format(formatter));
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;
}