Examples
1. Generating Log Messages
Formatter formatter = new Formatter();
String logMessage = formatter.format("%1$tF %1$tT - [%2$s] - %3$s", new java.util.Date(), "INFO", "Application started successfully").toString();
System.out.println(logMessage); // 2024-12-27 14:35:20 - [INFO] - Application started successfully2. Creating a Table of Data (Report Generation)
Formatter formatter = new Formatter();
formatter.format("| %-20s | %-10s | %-10s | %-15s |\n", "Employee", "Department", "Salary", "Joining Date");
formatter.format("| %-20s | %-10s | %-10.2f | %-15s |\n", "Alice", "HR", 75000.50, "2020-01-15");
formatter.format("| %-20s | %-10s | %-10.2f | %-15s |\n", "Bob", "Engineering", 95000.75, "2018-05-20");
formatter.format("| %-20s | %-10s | %-10.2f | %-15s |\n", "Charlie", "Marketing", 80000.00, "2019-11-30");
System.out.println(formatter.toString());| Employee | Department | Salary | Joining Date |
| Alice | HR | 75000.50 | 2020-01-15 |
| Bob | Engineering| 95000.75 | 2018-05-20 |
| Charlie | Marketing | 80000.00 | 2019-11-30 |3. Currency and Number Formatting
4. Generating a Custom File Output (Exporting Data)
5. Locale-specific Date Formatting
6. File Content Formatting for Reporting
7. Formating Time and Duration
Last updated