How to format a date using different formats in Java

1 Answer

0 votes
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

// Use Java API: java.time (LocalDateTime, ZonedDateTime, DateTimeFormatter).

/**
Java Date Format Patterns:
yyyy — 4‑digit year
MM — month (01–12)
dd — day of month
HH — hour (00–23)
hh — hour (01–12)
mm — minutes
ss — seconds
a — AM/PM
EEE — short weekday
EEEE — full weekday
MMM — short month
MMMM — full month
D — day of year
w — week of year
z — timezone abbreviation
*/

public class DateFormats {
    public static void main(String[] args) {

        // Current local date/time
        LocalDateTime now = LocalDateTime.now();

        // Current date/time with timezone
        ZonedDateTime zoned = ZonedDateTime.now(ZoneId.systemDefault());

        // --- Basic numeric formats ---
        System.out.println("[ISO 8601] YYYY-MM-DD: " +
                now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

        System.out.println("[European] DD/MM/YYYY: " +
                now.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));

        System.out.println("[US] MM-DD-YYYY: " +
                now.format(DateTimeFormatter.ofPattern("MM-dd-yyyy")));

        // --- Time formats ---
        System.out.println("[24-hour] HH:MM:SS: " +
                now.format(DateTimeFormatter.ofPattern("HH:mm:ss")));

        System.out.println("[12-hour] HH:MM:SS AM/PM: " +
                now.format(DateTimeFormatter.ofPattern("hh:mm:ss a")));

        // --- Full date with names ---
        System.out.println("Full weekday + month name: " +
                now.format(DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy")));

        System.out.println("Short weekday + month name: " +
                now.format(DateTimeFormatter.ofPattern("EEE, MMM dd")));

        // --- Combined date/time ---
        System.out.println("Full timestamp: " +
                now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        System.out.println("ISO date/time: " +
                now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));

        // --- With timezone ---
        System.out.println("Zoned timestamp: " +
                zoned.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z")));

        System.out.println("RFC 1123 format: " +
                zoned.format(DateTimeFormatter.RFC_1123_DATE_TIME));

        // --- Special formats ---
        System.out.println("Day of year: " +
                now.format(DateTimeFormatter.ofPattern("D")));

        System.out.println("Week of year: " +
                now.format(DateTimeFormatter.ofPattern("w")));

        System.out.println("Week-based year: " +
                now.format(DateTimeFormatter.ofPattern("YYYY")));

        // --- Custom formats ---
        System.out.println("Long date: " +
                now.format(DateTimeFormatter.ofPattern("EEEE, MMMM d, yyyy")));

        System.out.println("Short date: " +
                now.format(DateTimeFormatter.ofPattern("dd/MM/yy")));

        System.out.println("Time only: " +
                now.format(DateTimeFormatter.ofPattern("HH:mm")));
    }
}



/*
run:

[ISO 8601] YYYY-MM-DD: 2026-05-20
[European] DD/MM/YYYY: 20/05/2026
[US] MM-DD-YYYY: 05-20-2026
[24-hour] HH:MM:SS: 09:46:54
[12-hour] HH:MM:SS AM/PM: 09:46:54 AM
Full weekday + month name: Wednesday, May 20, 2026
Short weekday + month name: Wed, May 20
Full timestamp: 2026-05-20 09:46:54
ISO date/time: 2026-05-20T09:46:54.992961699
Zoned timestamp: 2026-05-20 09:46:54 GMT
RFC 1123 format: Wed, 20 May 2026 09:46:54 GMT
Day of year: 140
Week of year: 21
Week-based year: 2026
Long date: Wednesday, May 20, 2026
Short date: 20/05/26
Time only: 09:46

*/

 



answered 12 hours ago by avibootz
...