Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Formatting Dates with printf in Java

Tech May 15 1

Using printf for Date and Time Formatting

The printf method provides a straigthforward way to format dates and times in Java. Format specifiers start with %t followed by a single letter that determines the output format.

Code Example

import java.util.Date;

public class DateFormatting {
    public static void main(String[] args) {
        Date currentDate = new Date();
        
        System.out.printf("Full date and time: %tc%n", currentDate);
        System.out.printf("ISO format: %tF%n", currentDate);
        System.out.printf("US format: %tD%n", currentDate);
        System.out.printf("12-hour with AM/PM: %tr%n", currentDate);
        System.out.printf("24-hour time: %tT%n", currentDate);
        System.out.printf("24-hour without seconds: %tR", currentDate);
    }
}

Output:

Full date and time: Mon Sep 10 10:43:36 CST 2012
ISO format: 2012-09-10
US format: 09/10/12
12-hour with AM/PM: 10:43:36 AM
24-hour time: 10:43:36
24-hour without seconds: 10:43

Using Parameter Indexes

When you need to refernece the same date object multiple times within a format string, you can use indexed parameters. The index must immediately follow % and end with $.

import java.util.Date;

public class DateFormatting {
    public static void main(String[] args) {
        Date currentDate = new Date();
        
        System.out.printf("%1$s %2$tB %2$td, %2$tY", "Expiration:", currentDate);
    }
}

Output:

Expiration: February 09, 2014

Using the < Flag

The < flag allows you to reuse the previously formatted argument. This simplifies format strings when the same date needs multiple conversions.

import java.util.Date;

public class DateFormatting {
    public static void main(String[] args) {
        Date currentDate = new Date();
        
        System.out.printf("%s %tB %<te, %<tY", "Expiration:", currentDate);
    }
}

Output:

Expiration: February 09, 2014

Individual Date Component Format Specifiers

These format specifiers extract specific components from a date object:

import java.util.Date;
import java.util.Locale;

public class DateComponentDemo {
    public static void main(String[] args) {
        Date sampleDate = new Date();
        
        // Abbreviated month name
        String result = String.format(Locale.US, "Month (short): %tb", sampleDate);
        System.out.println(result);
        System.out.printf("Month (short, local): %tb%n", sampleDate);
        
        // Full month name
        result = String.format(Locale.US, "Month (full): %tB", sampleDate);
        System.out.println(result);
        System.out.printf("Month (full, local): %tB%n", sampleDate);
        
        // Abbreviated weekday
        result = String.format(Locale.US, "Weekday (short): %ta", sampleDate);
        System.out.println(result);
        
        // Full weekday
        System.out.printf("Weekday (full, local): %tA%n", sampleDate);
        
        // Century (first two digits of year)
        System.out.printf("Century: %tC%n", sampleDate);
        
        // Last two digits of year
        System.out.printf("Year (last two): %ty%n", sampleDate);
        
        // Day of year
        System.out.printf("Day of year: %tj%n", sampleDate);
        
        // Month (two digits)
        System.out.printf("Month (two digits): %tm%n", sampleDate);
        
        // Day of month (two digits, zero-padded)
        System.out.printf("Day (zero-padded): %td%n", sampleDate);
        
        // Day of month (no padding)
        System.out.printf("Day (no padding): %te", sampleDate);
    }
}

Output:

Month (short): May
Month (short, local): 五月
Month (full): May
Month (full, local): 五月
Weekday (short): Thu
Weekday (full, local): 星期四
Century: 20
Year (last two): 17
Day of year: 124
Month (two digits): 05
Day (zero-padded): 04
Day (no padding): 4

Tags: Java

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.