Formatting Time and Date

Java-Formatting Time and Date

Formatting Time and Date

The conversion specifier %t is used to format time and date information. The %t specifier works a bit differently than the others because it requires the use of a suffix to describe the portion and precise format of the time or date desired. For example, to display time, you would use %tT, where T indicates time in 24-hour clock as hh:mm:ss. The argument corresponding to the %t specifier must be of type Calendar, Date, Long or long. For example:

Formatting Time and Date


Time and Date Format Suffixes

Formatting Time and Date 13

Program

Formatting Time and Date
Program Source

import java.util.Formatter;
import java.util.Calendar;

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

        Calendar cl = Calendar.getInstance();
        Formatter fmt = new Formatter();
        fmt.format("tT --> %tT", cl);
        fmt.format("%ntr --> %tr", cl);
        fmt.format("%ntD --> %tD", cl);
        fmt.format("%ntF --> %tF", cl);
        fmt.format("%nTc --> %Tc", cl);
        fmt.format("%ntc --> %tc", cl);
        fmt.format("%ntl:tM:Tp --> %tl:%tM:%Tp", cl, cl, cl);
        fmt.format("%ntB tm --> %tB %tm", cl, cl);
        System.out.println(fmt);
    }
}

Leave a Comment