DateFormat class provides various formats to format the time. DateFormat.getTimeInstance() method is to be used. See the example below.
In following example we'll show how to use different formats to format time.
IOTester.java
import java.text.DateFormat; import java.util.Date; public class I18NTester { public static void main(String[] args) { DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.DEFAULT); System.out.println(dateFormat.format(new Date())); dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT); System.out.println(dateFormat.format(new Date())); dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM); System.out.println(dateFormat.format(new Date())); dateFormat = DateFormat.getTimeInstance(DateFormat.LONG); System.out.println(dateFormat.format(new Date())); dateFormat = DateFormat.getTimeInstance(DateFormat.FULL); System.out.println(dateFormat.format(new Date())); } }
It will print the following result.
4:11:21 PM 4:11 PM 4:11:21 PM 4:11:21 PM IST 4:11:21 PM ISTPrint