Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to set date formats for different countries
Firstly, set the locale −
Locale[] strLocales = { US, UK, GERMANY};
Now, let us set the date formats i.e. different constants −
int[] constants = { SHORT, MEDIUM, LONG };
String[] str = { "SHORT", "MEDIUM", "LONG" };
Loop through and get the different date formats for different countries −
for (Locale country : strLocales) {
System.out.println("\n"+ country.getDisplayCountry() + ".....");
for (int i = 0; i < constants.length; i++) {
dateFormat = DateFormat.getDateInstance(constants[i], country);
System.out.println(str[i] + " constant = " + dateFormat.format(dt));
}
}
The following is an example
Example
import static java.text.DateFormat.*;
import static java.util.Locale.*;
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();
Locale[] strLocales = { US, UK, GERMANY};
int[] constants = { SHORT, MEDIUM, LONG };
String[] str = { "SHORT", "MEDIUM", "LONG" };
DateFormat dateFormat = null;
for (Locale country : strLocales) {
System.out.println("\n"+ country.getDisplayCountry() + ".....");
for (int i = 0; i < constants.length; i++) {
dateFormat = DateFormat.getDateInstance(constants[i], country);
System.out.println(str[i] + " constant = " + dateFormat.format(dt));
}
}
}
}
Output
United States..... SHORT constant = 11/22/18 MEDIUM constant = Nov 22, 2018 LONG constant = November 22, 2018 United Kingdom..... SHORT constant = 22/11/18 MEDIUM constant = 22-Nov-2018 LONG constant = 22 November 2018 Germany..... SHORT constant = 22.11.18 MEDIUM constant = 22.11.2018 LONG constant = 22. November 2018
Advertisements