In the previous section of examples you have learned about how to create date and how to format them and many such examples. Now you will learn how we can change the formatting symbols for date.
Example program to change Date formatting Symbols
In the previous section of examples you have learned about how to create date and how to format them and many such examples. Now you will learn how we can change the formatting symbols for date.
In this example of changing the date formatting symbols we have used DateFormatSymbols class. In this class we have used four different methods of DateFormatSymbols class.
DateFormatSymbols symbols = new DateFormatSymbols();
symbols.setMonths(MONTHS);
above lines of code sets symbols for Months with the string array MONTHS. It sets months full name symbol.
symbols.setShortMonths(ShortMONTHS);
sets months short name symbols.
symbols.setWeekdays(Weekdays);
sets Weekdays full name symbol.
symbols.setShortWeekdays(shortWeekdays);
sets Weekdays symbols name in short.
Here is the full example code of DateSymbolsFormat.java as follows:
import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.text.DateFormatSymbols; public class DateSymbolsFormat { public static void main(String[] args) { String[] MONTHS = {"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"}; String[] ShortMONTHS = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}; String[] Weekdays = {"", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"}; String[] shortWeekdays = {"", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"}; DateFormatSymbols symbols = new DateFormatSymbols(); symbols.setMonths(MONTHS); symbols.setShortMonths(ShortMONTHS); symbols.setWeekdays(Weekdays); symbols.setShortWeekdays(shortWeekdays); //////////////////////////////////////////////////////////////// // Format date into dd-MMMM-yyyy format e.g 10-OCTOBER-2008 // //////////////////////////////////////////////////////////////// DateFormat format = new SimpleDateFormat("dd-MMMM-yyyy", symbols); System.out.println(format.format(new Date())); ///////////////////////////////////////////////////////////// // Format date into dd-MMM-yyyy format e.g 10-OCT-2008 // ///////////////////////////////////////////////////////////// format = new SimpleDateFormat("dd-MMM-yyyy", symbols); System.out.println(format.format(new Date())); /////////////////////////////////////////////////////////////////////////// // Format date into EEEE, dd-MMM-yyyy format e.g SATURDAY,10-OCT-2008 // /////////////////////////////////////////////////////////////////////////// format = new SimpleDateFormat("EEEE, dd-MMM-yyyy", symbols); System.out.println(format.format(new Date())); /////////////////////////////////////////////////////////////////////////// // Format date into E, dd-MMM-yyyy format e.g SAT,10-OCT-2008 // /////////////////////////////////////////////////////////////////////////// format = new SimpleDateFormat("E, dd-MMM-yyyy", symbols); System.out.println(format.format(new Date())); } } |
To run this example create and save DateSymbolsFormat.java . Compile this class with the javac command and execute it with the java command. You will get the following output on your command prompt.
Output:
C:\DateExample>javac DateSymbolsFormat.java C:\DateExample>java DateSymbolsFormat 10-OCTOBER-2008 10-OCT-2008 SATURDAY, 10-OCT-2008 SAT, 10-OCT-2008 |