Format date and time value

In this chapter you will learn:

  1. What specifiers to use to format time and date
  2. How to show month by name and number
  3. How to display standard 12-hour time format
  4. How to display complete time and date information
  5. How to just display hour and minute
  6. How to display day of month as a decimal

Specifiers for formatting time and date

%t formats time and date information.

%t specifier requires the use of a suffix to describe the portion and precise format of the time or date desired.

The time and date suffixes are shown in the following table.

SuffixReplaced By
aAbbreviated weekday name
AFull weekday name
bAbbreviated month name
BFull month name
cStandard date and time string formatted as day month date hh::mm:ss tzone year
CFirst two digits of year
dDay of month as a decimal (01-31)
Dmonth/day/year
eDay of month as a decimal (1-31)
Fyear-month-day
hAbbreviated month name
HHour (00 to 23)
IHour (01 to 12)
jDay of year as a decimal (001 to 366)
kHour (0 to 23)
lHour (1 to 12)
LMillisecond (000 to 999)
mMonth as decimal (01 to 13)
MMinute as decimal (00 to 59)
NNanosecond (000000000 to 999999999)
pLocale's equivalent of AM or PM in lowercase
QMilliseconds from 1/1/1970
rhh:mm:ss (12-hour format)
Rhh:mm (24-hour format)
SSeconds (00 to 60)
sSeconds from 1/1/1970 UTC
Thh:mm:ss (24-hour format)
yYear in decimal without century (00 to 99)
YYear in decimal including century (0001 to 9999)
zOffset from UTC
ZTime zone name

To display minutes, you would use %tM, and M indicates minutes in a two-character field.

The argument corresponding to the %t specifier must be of type Calendar, Date, Long, or long.

Display month by name and number

The following code shows how to use format('%tB %tb %tm', cal, cal, cal) to display month name and month number.

import java.util.Calendar;
import java.util.Formatter;
/*j  a v a2 s  .  c o  m*/
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    Calendar cal = Calendar.getInstance();

    // Display month by name and number. 
    fmt = new Formatter(); 
    fmt.format("%tB %tb %tm", cal, cal, cal); 
    System.out.println(fmt); 
  }
}

The output:

Display standard 12-hour time format

The following code displays standard 12-hour time format.

import java.util.Calendar;
import java.util.Formatter;
/*from   j a  va2  s.c  o  m*/
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    Calendar cal = Calendar.getInstance();
    // Display standard 12-hour time format.
    fmt.format("%tr", cal);
    System.out.println(fmt);//
  }
}

The output:

Display complete time and date information

The following code uses the %tc to display time and date information in a complete form.

import java.util.Calendar;
import java.util.Formatter;
//  ja  v a 2 s . co  m
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    Calendar cal = Calendar.getInstance();
    // Display complete time and date information.
    fmt = new Formatter();
    fmt.format("%tc", cal);
    System.out.println(fmt);
  }
}

The output:

Display just hour and minute

The following code only shows the hour and minute information.

import java.util.Calendar;
import java.util.Formatter;
//java2s. co m
public class Main {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    Calendar cal = Calendar.getInstance();
    // Display just hour and minute.
    fmt = new Formatter();
    fmt.format("%tl:%tM", cal, cal);
    System.out.println(fmt);
  }
}

The output:

Display Day of month as a decimal

The following code displays day of month as a decimal.

import java.util.Calendar;
import java.util.Formatter;
/*  ja v a2 s.co m*/
public class MainClass {
  public static void main(String args[]) {
    Formatter fmt = new Formatter();
    Calendar cal = Calendar.getInstance();

    fmt.format("Today is day %te of %<tB, %<tY", cal);
    System.out.println(fmt);
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to use %n and %% specifiers to escape new line and percentage
Home » Java Tutorial » String

String

    Java String type
    Java String Concatenation
    Java String Creation
    Java String Compare
    Java String Search
    Java String and char array
    Java String Conversion
    String trim, length, is empty, and substring
    String replace

StringBuffer

    StringBuffer class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String

StringBuilder

    StringBuilder
    StringBuilder insert and append
    StringBuilder length and capacity
    StringBuilder get,delete,set char
    StringBuilder delete, reverse
    StringBuilder search with indexOf and lastIndexOf
    StringBuilder to String

String Format

    Formatter class
    Format Specifier
    Format String and characters
    Format integer value
    Format decimal
    Scientific notation format
    Format octal and hexadecimal value
    Format date and time value
    Escape Formatter
    Minimum Field Width
    Specifying Precision
    Format Flags
    Uppercase Option
    Formatter Argument Index
    Align left and right
    Left and right padding a string

String Format Utilities

    Abbreviate string
    Caplitalize a string
    Uncapitalize a string
    Utility class for right padding
    Left padding
    Centers a String
    Transforms words