YouTip LogoYouTip

Java Simpledateformat Class

`SimpleDateFormat` is a class in Java used for formatting and parsing dates and times, belonging to the `java.text` package. It allows you to convert a date object (`Date`) to a string in a specific format, or parse a formatted date string back to a date object. * * * ## Basic Usage ### 1. Creating a SimpleDateFormat Object To use `SimpleDateFormat`, you first need to create an instance and specify a date format pattern: ## Example import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args){ // Create SimpleDateFormat object and specify format SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Get current date and time Date now =new Date(); // Format date to string String formattedDate = sdf.format(now); System.out.println("Current time: "+ formattedDate); } } The output is: Current time: 2025-05-01 11:35:07 ### 2. Common Format Pattern Symbols The following are commonly used format pattern symbols: | Symbol | Meaning | Example | | --- | --- | --- | | yyyy | Four-digit year | 2023 | | MM | Two-digit month | 01-12 | | dd | Two-digit day | 01-31 | | HH | Hour in 24-hour format | 00-23 | | mm | Minutes | 00-59 | | ss | Seconds | 00-59 | | E | Day of week | Mon, Tue, etc. | | a | AM/PM marker | AM or PM | * * * ## Date Formatting Formatting a `Date` object to a string: ## Example SimpleDateFormat sdf =new SimpleDateFormat("yyyyYearMMMonthddDay EEEE a hh:mm:ss"); Date now =new Date(); System.out.println(sdf.format(now)); // Sample output: 2023Year11Month15Day Wednesday PM 03:45:22 * * * ## Date Parsing Parsing a formatted date string to a `Date` object: ## Example try{ SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd"); String dateString ="2023-11-15"; Date date = sdf.parse(dateString); System.out.println(date); // Output: Wed Nov 15 00:00:00 CST 2023 }catch(ParseException e){ e.printStackTrace(); } Note: Parsing may throw a `ParseException`, which requires exception handling. * * * ## Thread Safety Issues `SimpleDateFormat` is not thread-safe. If the same `SimpleDateFormat` instance is shared in a multi-threaded environment, it may cause problems. Solutions: 1. Create a new instance each time (lower performance) 2. Use `ThreadLocal` to keep an instance for each thread 3. Use Java 8's `DateTimeFormatter` (recommended) ## Example // Solution using ThreadLocal private static final ThreadLocal dateFormat = ThreadLocal.withInitial(()->new SimpleDateFormat("yyyy-MM-dd")); // Usage String formattedDate = dateFormat.get().format(new Date()); * * * ## Java 8 Alternative In Java 8 and later, it is recommended to use the classes in the `java.time` package: ## Example import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args){ // Create formatter DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // Get current time and format LocalDateTime now = LocalDateTime.now(); String formattedDate = now.format(formatter); System.out.println("Current time: "+ formattedDate); // Parse string to date time LocalDateTime parsedDate = LocalDateTime.parse("2023-11-15 14:30:00", formatter); System.out.println("Parsed time: "+ parsedDate); } } `DateTimeFormatter` is thread-safe and has better performance. It is recommended for use in new projects. * * * ## Summary 1. `SimpleDateFormat` is used for date formatting and parsing 2. Specify the date format through pattern strings 3. It is not thread-safe; pay attention to usage in multi-threaded environments 4. For Java 8 and above, it is recommended to use `DateTimeFormatter` By mastering `SimpleDateFormat`, you can flexibly handle various date and time format conversion requirements in Java programs. The following are the commonly used methods of the java.text.SimpleDateFormat class: ### Constructors | Method | Description | | --- | --- | | `Simple
← Java Gregoriancalendar ClassJava Regex Macher β†’