Time Format Example

This example shows how to format time using Format class. In this program we use a pattern of special characters to time format.
Description of the code :
SimpleDateFormat() : SimpleDateFormat class use for formatting and parsing dates. It allows for formatting date into text , parsing text into date and normalization.
format() : This method used for format Date class object into date / time and appends the it into the StringBuffer.
TimeFormat.java
import java.text.*;
import java.util.*;
public class TimeFormat {
public static void main(String args[]) {
String s;
Format formatter;
Date date = new Date();
// Time formate 01:12:53 AM
formatter = new SimpleDateFormat("hh:mm:ss a");
s = formatter.format(date);
System.out.println("hh:mm:ss a formate : " + s);
//Time formate 14.36.33
formatter = new SimpleDateFormat("HH.mm.ss");
s = formatter.format(date);
System.out.println("HH.mm.ss : formate : " + s);
}
}
|
Output
hh:mm:ss a formate : 04:31:16 PM
HH.mm.ss : formate : 16.31.16
|
Download code

|