- 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 date with DateFormat.SHORT in Java
DateFormat.SHORT is a constant for short style pattern.
Firstly, we will create date object
Date dt = new Date(); DateFormat dateFormat;
Let us format date for different locale with DateFormat.SHORT
// FRENCH dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.FRENCH); // GERMANY dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMANY);
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; // Date Format SHORT constant dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.FRENCH); System.out.println("Locale FRENCH = " + dateFormat.format(dt)); dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMANY); System.out.println("Locale GERMANY = " + dateFormat.format(dt)); dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINESE); System.out.println("Locale CHINESE = " + dateFormat.format(dt)); dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CANADA); System.out.println("Locale CANADA = " + dateFormat.format(dt)); dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ITALY); System.out.println("Locale ITALY = " + dateFormat.format(dt)); dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.TAIWAN); System.out.println("Locale TAIWAN = " + dateFormat.format(dt)); } }
Output
Locale FRENCH = 22/11/18 Locale GERMANY = 22.11.18 Locale CHINESE = 18-11-22 Locale CANADA = 22/11/18 Locale ITALY = 22/11/18 Locale TAIWAN = 2018/11/22
Advertisements