Signup/Sign In
LAST UPDATED: SEPTEMBER 9, 2021

Custom formatting of Date and Time patterns in Java

    In our lives, a lot of information involves handling and validating date, time and date-time such as DOB, medicine expiry date, date of joining, selling date, movie start time, etc. Java has in-built support for date and time. In java, java.time package consists of three main classes which help us to handle and perform various such operations on date and time namely LocalDate class, LocalTime and LocalDateTime. These classes give us the date and/or time in a particular format but we may need it in some specific format. In this article, we will see how to format the Date and/or time, specific to our use case using predefined formats and also, user-defined formats.

    Formatting LocalDate:

    LocalDate class is used to manipulate and handle the date component. It is an immutable class that represents a date in default format of yyyy-MM-dd (year, month, day) and it doesn’t store time and time-zone. It implements the ChronoLocalDate interface. It should be used when the use case involves dealing with only the date component. So, in order to format the date we have a function in this class namely format(). The date can be used in default format (2020-06-21), short format (6/21/20), medium format (Jun 21, 2020), long fomat (June 21, 2020) and full format (Sunday, June 21, 2020):

    import java.time.LocalDate;
    
    public class StudyTonight{
       
            public static void main(String[] args) {
                  LocalDate todayDate = LocalDate.now();
                  System.out.println("Default pattern: " + todayDate .toString());
    
                  String shortDate = todayDate .format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT));
                  System.out.println("Short pattern: " + shortDate);
    
                  String mediumDate = todayDate .format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
                  System.out.println("Medium pattern: " + mediumDate);
    
                  String longDate = todayDate .format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG));
                  System.out.println("Long pattern: " + longDate);
    
                  String fullDate = todayDate .format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL));
                  System.out.println("Full pattern: " + fullDate);
    
                  String customDate1 = todayDate.format(DateTimeFormatter.ofPattern("dd-MMMM-yyy"));
                  System.out.println("User defined pattern 1: " + customDate1);
    
                  String customDate2 = todayDate.format(DateTimeFormatter.ofPattern("YY-MMM-dd"));
                  System.out.println("User defined pattern 2: " + customDate2);
    
                  String customDate3 = todayDate.format(DateTimeFormatter.ofPattern("DD 'day of' YYYY"));
                  System.out.println("User defined pattern (Day of the year): " + customDate3);
    
        }
    }


    Default pattern: 2020-06-21
    Short pattern: 6/21/20
    Medium pattern: Jun 21, 2020
    Long pattern: June 21, 2020
    Full pattern: Sunday, June 21, 2020
    User defined pattern 1: 21-June-2020
    User defined pattern 2: 20-Jun-21
    User defined pattern (Day of the year): 173 day of 2020

    Formatting LocalTime:

    LocalTime is used to manipulate and handle the time component. It is an immutable class that represents time in a default format of HH-mm-ss-zzz (hour, minute, second and milliseconds). It implements the Comparable interface. It should be used when the use case involves dealing with only the time component. Time can be used in default format (01:01:01), short format (1:01 AM) and medium format (1:01:01 AM). We can't use long or full format with LocalTime class because these formats require time zones but this class doesn't support it and will throw an exception: java.time.DateTimeException: Unable to extract ZoneId from temporal ...

    import java.time.LocalDate;
    
    public class StudyTonight{
       
            public static void main(String[] args) {
                 LocalTime nowTime = LocalTime.now();
                 System.out.println("Default pattern: " + nowTime.toString());
    
                 String shortTime = nowTime.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT));
                 System.out.println("Short pattern: " + shortTime);
    
                 String mediumTime = nowTime.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM));
                 System.out.println("Medium pattern: " + mediumTime);
    
                 String customTime = nowTime.format(DateTimeFormatter.ofPattern("hh::mm::ss"));
                 System.out.println("User defined pattern 1 (12 hr time): " + customTime);
    
                 String customTime2 = nowTime.format(DateTimeFormatter.ofPattern("HH::mm::ss"));
                 System.out.println("User defined pattern 2 (24 hr time): " + customTime2);
            }
    }


    Default pattern: 01:01:01
    Short pattern: 1:01 AM
    Medium pattern: 1:01:01 AM
    User defined pattern 1 (12 hr time): 08::29::10
    User defined pattern 2 (24 hr time): 20::29::10

    Also Read: Java LocalDate getDayOfMonth() Method

    Formatting LocalDateTime:

    LocalDateTime is used to manipulate and handle both, the date and time components of the date-time object. It is an immutable class that represents date-time in the default format of yyyy-MM-dd-HH-mm-ss-zzz (year, month, day, hour, minute, second, zone) and it implements the ChronoLocalDateTime interface. It should be used when the use case involves dealing with both, date and time component. We can't use long or full format with LocalDateTime class because these formats require time zones but this class doesn't support it and will throw an exception: java.time.DateTimeException: Unable to extract ZoneId from temporal ...

    import java.time.LocalDate;
    
    public class StudyTonight{
       
            public static void main(String[] args) {
                 LocalDateTime todayDateTime = LocalDateTime.now();
                 System.out.println("Default pattern: " + todayDateTime .toString());
    
                 String shortDateTime = todayDateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT));
                 System.out.println("Short pattern: " + shortDateTime);
    
                 String mediumDateTime = todayDateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
                 System.out.println("Medium pattern: " + mediumDateTime);
     
                 String customDateTime = todayDateTime.format(DateTimeFormatter.ofPattern("dd/MM/yy :: h-mm-ss"));
                 System.out.println("User defined pattern: " + customDateTime);
    
                 String customDateTime2 = todayDateTime.format(DateTimeFormatter.ofPattern("MM:dd:YYYY :: HH-mm-ss"));
                 System.out.println("User defined pattern: " + customDateTime2);
            }
    }


    Default pattern: 2020-06-21T19:31:49.978315600
    Short pattern: 6/21/20, 7:31 PM
    Medium pattern: Jun 21, 2020, 7:31:49
    User defined pattern: 21/06/20 :: 8-10-04
    User defined pattern: 06:21:2020 :: 8-30-29

    We have discussed almost all the most common patterns used for date and time in any application/system. But there is always a possibility that you may require some personalized format according to your or customer's need. For that purpose you can refer to the table given below that contains a list of all the symbols that you can use to design a custom date-time format patterns:

    Symbol Meaning Presentation Example
    G era designator Text AD
    y year Number 2009
    M month in year Text & Number July & 07
    d day in month Number 10
    h hour in am/pm (1-12) Number 12
    H hour in day (0-23) Number 0
    m minute in hour Number 30
    s second in minute Number 55
    S millisecond Number 978
    E day in week Text Tuesday
    D day in year Number 189
    F day of week in month Number 2 (2nd Wed in July)
    w week in year Number 27
    W week in month Number 2
    a am/pm marker Text PM
    k hour in day (1-24) Number 24
    K hour in am/pm (0-11) Number 0
    z time zone Text Pacific Standard Time
    ' escape for text Delimiter (none)
    ' single quote Literal '


    Conclusion:

    In this article, we have learned to use the date-time format patterns other than the default ones. Hope the article helped you in understanding the different date and time formats and how to use the java.time package classes.

    You may also like:

    I'm a writer at studytonight.com.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS