Java I18N – Formatting Time


Java Internationalization – Formatting Time


”;


DateFormat class provides various formats to format the time. DateFormat.getTimeInstance() method is to be used. See the example below.

Example

In following example we”ll show how to use different formats to format time.

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()));
	  
   }
}

Output

It will print the following result.

2:36:09 PM
2:36 PM
2:36:09 PM
2:36:09 PM IST
2:36:09 PM India Standard Time

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *