format date time from string [duplicate] - java

This question already has answers here:
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Java parsing date with timezone from a string
(5 answers)
Java Time Zone When Parsing DateFormat
(9 answers)
Closed last month.
how can I format such a date:
2023-01-04T17:19:08+0100 in a string to a date in LocalDateTime using DateTimeFormatter or another tool ? It tries this way and throws a DateTimeParseException :
private LocalDateTime parseLocalDateTime(String dateTime){
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS");
return LocalDateTime.parse(dateTime, formatter);
}

Related

Unparseable date exception for the date format 2020-12-31T11:46:18.000+00:00 [duplicate]

This question already has answers here:
Parsing ISO-8601 DateTime with offset with colon in Java
(4 answers)
2021-04-05T16:25:45.000+00:00 Time stamp change in SimpleDateFormat("yyyy-MM-dd hh:mm:ss a")
(1 answer)
DateTimeFormatter create pattern [duplicate]
(1 answer)
Closed 3 months ago.
How do i parse a date with the format 2020-12-31T11:46:18.000+00:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date startDate = sdf.parse("2020-12-31T11:46:18.000+00:00");
I get Unparseable date exception. I have tried providing the Z without the quote but still it throws the exception.
The below code also doesn't work
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

Convert string yyyy-mm-dd to date in javaEE [duplicate]

This question already has answers here:
Java date format conversion - getting wrong month
(8 answers)
java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
(8 answers)
Calculating days between two dates with Java
(16 answers)
Converting String to Date using SimpleDateFormat is returning random date [duplicate]
(2 answers)
Closed 2 years ago.
I need to convert the following format of date yyyy-mm-dd to date in format dd/mm/yyyy or dd-mm-yyyy, is it posible. I'm getting unparsable exception.
I have the following code:
String fechaInicial= docudetalle.getfechainicial();
String fechaFinal= docudetalle.getfechafinal();
String fechaEspecial= docudetalle.getfechaespecial();
SimpleDateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
Date dateInicial = formatter.parse(fechaInicial);
Date dateFinal = formatter.parse(fechaFinal);
Date dateEspecial = formatter.parse(fechaEspecial);
To which once converted I need to get the difference between the three dates, to show in a table as the amount days passed. How can I get the difference in the dates, I know with calendar instance is much easier to get the difference. Is there is an easier method for this?
Use LocalDate and DateTimeFormatter to manipulate dates.
String source = "2020-04-22";
LocalDate src = LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String dst = src.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
System.out.println(dst);
// or
dst = src.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
System.out.println(dst);
prints
22/04/2020
22-04-2020

Convert 201204121458 to date format 2012/04/12 14:59 in Java [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Change date format in a Java string
(22 answers)
Closed 3 years ago.
I have an issue with java how can convert string 201204121458 to date format 2012/04/12 14:59 using a method ?
Thank you
This code snippet may help:
String str = "201204121458";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
DateTimeFormatter printer = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
String formattedDateTime = dateTime.format(printer); // "2012/04/12 14:59"

Formatting LocalDate in Java [duplicate]

This question already has answers here:
Get first and last day of month using threeten, LocalDate
(13 answers)
How to format LocalDate to yyyyMMDD (without JodaTime)
(2 answers)
Closed 3 years ago.
I defined the following format in Java :
//define format of YYYYMMDD
private final DateTimeFormatter dtf = DateTimeFormatter.BASIC_ISO_DATE;
My application fetches a certain date from the calendar:
LocalDate localDate = fetchDate(); // fetch date in format "yyyy-mm-dd"
I want to store an additional two dates in format of dtf. The startOfMonth and endOfMonth of the given localDate.
E.g. if localDate is "2019-12-12" I want to create the following two variables -
String startOfMonth = "20191201";
String endOfMonth = "20191231";
How can I do that?
LocalDate atStartOfMonth = localDate.with(TemporalAdjusters.firstDayOfMonth());
LocalDate atEndOfMonth = localDate.with(TemporalAdjusters.lastDayOfMonth());
And then you format those local dates the way you want to (i.e. with your dtf formatter)

How to convert Date String pattern into DateTime Format format [duplicate]

This question already has answers here:
how to change format of date from string date
(4 answers)
Closed 4 years ago.
I have this Pattern of Date.
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-mm-dd");
DateTime jodatime = new DateTime(date);
I need in this format.
public static DateTime stringToDateTime(String date) {
DateTimeFormatter formatter = DateTimeFormat.forPattern(" yyyy-MM-dd'T'HH:mm:ss.SSSZZ");
return formatter.parseDateTime(date);
}
public static DateTime stringToDateTime(String date) {
return new DateTime(new Date(date));
}
Here Date Default java.util.Date

Categories