Java DateFormat getTimeInstance() Example
DateFormat class getTimeInstance() method example. This example shows you how to use getTimeInstance() method.
Syntax is : public static final DateFormat getTimeInstance(int style)
Method returns the time formatter with the given formatting style for the default locale.
Here is the code.
/**
* @(#) GetTimeInstance1DateFormat.java
* A class representing use of method getTimeInstance() of DateFormat
class in java.text Package.
* @Version 12-May-2008
* @author Rose India Team
*/
import java.util.*;
import java.text.*;
class GetTimeInstance1DateFormat {
public static void main(String[] args) {
// Create new Date object that will be initialized to the current date.
Date date = new Date();
System.out.println("Date by Date class : " + date.toString());
// Create new DateFormat object by getInstance method.
DateFormat dateFormat = DateFormat.getInstance();
String stringDate = dateFormat.format(date);
System.out.println("Date by DateFormat class : " + stringDate);
// getTimeInstance method call.
String dFormat=dateFormat.getTimeInstance(DateFormat.SHORT).format(date);
System.out.println("Time formatter in SHORT style: " + dFormat);
dFormat = dateFormat.getTimeInstance(DateFormat.LONG).format(date);
System.out.println("Time formatter in LONG style: " + dFormat);
dFormat = dateFormat.getTimeInstance(DateFormat.MEDIUM).format(date);
System.out.println("Time formatter in MEDIUM style: " + dFormat);
dFormat = dateFormat.getTimeInstance(DateFormat.FULL).format(date);
System.out.println("Time formatter in FULL style: " + dFormat);
}
}
|
Output of the program.
Date by Date class : Thu Jun 12 18:24:06 GMT+05:30 2008
Date by DateFormat class : 6/12/08 6:24 PM
Time formatter in SHORT style: 6:24 PM
Time formatter in LONG style: 6:24:06 PM GMT+05:30
Time formatter in MEDIUM style: 6:24:06 PM
Time formatter in FULL style: 6:24:06 PM GMT+05:30 |
|