Java I18N – Formatting Date and Time


Java Internationalization – Formatting Date and Time


”;


DateFormat class provides various formats to format the date and time together. DateFormat.getDateTimeInstance() method is to be used. See the example below.

Example

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

import java.text.DateFormat;
import java.util.Date;

public class I18NTester {
   public static void main(String[] args) {

      DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT);

      System.out.println(dateFormat.format(new Date()));

      dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);

      System.out.println(dateFormat.format(new Date()));

      dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);

      System.out.println(dateFormat.format(new Date()));

      dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);

      System.out.println(dateFormat.format(new Date()));

      dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);

      System.out.println(dateFormat.format(new Date()));
	  
   }
}

Output

It will print the following result.

Jun 7, 2024, 2:34:35 PM
6/7/24, 2:34 PM
Jun 7, 2024, 2:34:35 PM
June 7, 2024 at 2:34:35 PM IST
Friday, June 7, 2024 at 2:34:35 PM India Standard Time

Advertisements

”;

Leave a Reply

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