- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Format time with DateFormat.SHORT in Java
Use the getTimeInstance() method in Java to get the time format for the locale you have set. DateFormat.SHORT is a constant for short style pattern.
Firstly, we will create Date object
Date dt = new Date(); DateFormat dateFormat;
Let us format time for different locale with DateFormat.SHORT
dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.CHINESE); System.out.println("Locale CHINESE = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.CANADA); System.out.println("Locale CANADA = " + dateFormat.format(dt));
The following is an example −
Example
import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Demo { public static void main(String args[]) { Date dt = new Date(); DateFormat dateFormat; // Displaying time with SHORT constant dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.FRENCH); System.out.println("Locale FRENCH = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.GERMANY); System.out.println("Locale GERMANY = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.CHINESE); System.out.println("Locale CHINESE = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.CANADA); System.out.println("Locale CANADA = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.ITALY); System.out.println("Locale ITALY = " + dateFormat.format(dt)); dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.TAIWAN); System.out.println("Locale TAIWAN = " + dateFormat.format(dt)); } }
Output
Locale FRENCH = 09:45 Locale GERMANY = 09:45 Locale CHINESE = 上午9:45 Locale CANADA = 9:45 AM Locale ITALY = 9.45 Locale TAIWAN = 上午 9:45
Advertisements