The "T" in the example above is used to separate the date from the time. You can use the DateTimeFormatter
class with the ofPattern()
method in the same package to format or parse date-time objects. The following example will remove both the "T" and nanoseconds from the date-time:
Example
import java.time.LocalDateTime; // Import the LocalDateTime class
import java.time.format.DateTimeFormatter; // Import the DateTimeFormatter class
public class Main {
public static void main(String[] args) {
LocalDateTime myDateObj = LocalDateTime.now();
System.out.println("Before formatting: " + myDateObj);
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDate = myDateObj.format(myFormatObj);
System.out.println("After formatting: " + formattedDate);
}
}
The output will be:
Before Formatting: 2021-07-06T16:38:36.469691
After Formatting: 06-07-2021 16:38:36
The ofPattern()
method accepts all sorts of values, if you want to display the date and time in a different format. For example:
Value | Example | |
---|---|---|
yyyy-MM-dd | "1988-09-29" | |
dd/MM/yyyy | "29/09/1988" | |
dd-MMM-yyyy | "29-Sep-1988" | |
E, MMM dd yyyy | "Thu, Sep 29 1988" |
Practice Excercise Practice now