DateTimeFormatter in Java is a class that provides a way to format and parse dates and times. It is a part of the java.time package introduced in Java 8 and offers both predefined and custom formatting patterns that can be used to display date and time in a human-readable form.
Using DateTimeFormatter, developers can easily convert LocalDate, LocalTime, or LocalDateTime objects to strings in different formats, or convert strings to these date and time objects. This is particularly useful for applications that require date and time manipulation or display in a specific format.
Here is an example of how to use DateTimeFormatter:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatterExample {
public static void main(String[] args) {
// Create a DateTimeFormatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
// Get the current date and time
LocalDateTime now = LocalDateTime.now();
// Format the current date and time
String formattedDateTime = now.format(formatter);
System.out.println("Formatted Date and Time: " + formattedDateTime);
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?