--
DateFormat helps you to format and parse dates for any locale. DateFormat provides many class methods for obtaining default date/time formatters based on the default or a given locale and a number of formatting styles. The formatting styles include FULL, LONG, MEDIUM, and SHORT. You can pass in different formatting style to these factory methods to control the length of the result but the exact result also depends on the locale, but generally:
DateFormat helps you to format and parse dates for any locale. DateFormat provides many class methods for obtaining default date/time formatters based on the default or a given locale and a number of formatting styles. The formatting styles include FULL, LONG, MEDIUM, and SHORT. You can pass in different formatting style to these factory methods to control the length of the result but the exact result also depends on the locale, but generally:
- SHORT is completely numeric, such as 12.13.52 or 3:30pm
- MEDIUM is longer, such as Jan 12, 1952
- LONG is longer, such as January 12, 1952 or 3:30:32pm
- FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST.
import java.text.DateFormat; import java.util.Date; public class DateFormatExample { public static void main(String[] args) { Date now = new Date(); DateFormat defaultDf = DateFormat.getDateInstance(); DateFormat shortDf = DateFormat.getDateInstance(DateFormat.SHORT); DateFormat mediumDf = DateFormat.getDateInstance(DateFormat.MEDIUM); DateFormat longDf = DateFormat.getDateInstance(DateFormat.LONG); DateFormat fullDf = DateFormat.getDateInstance(DateFormat.FULL); System.out.println(" 1. " + defaultDf.format(now)); System.out.println(" 2. " + shortDf.format(now)); System.out.println(" 3. " + mediumDf.format(now)); System.out.println(" 4. " + longDf.format(now)); System.out.println(" 5. " + fullDf.format(now)); } }The output is
1. Jun 20, 2008 2. 6/20/08 3. Jun 20, 2008 4. June 20, 2008 5. Friday, June 20, 2008Use getTimeInstance to get the time format for that country. For example,
import java.text.DateFormat; import java.util.Date; public class DateFormatExample { public static void main(String[] args) { Date now = new Date(); DateFormat defaultDf = DateFormat.getTimeInstance(); DateFormat shortDf = DateFormat.getTimeInstance(DateFormat.SHORT); DateFormat mediumDf = DateFormat.getTimeInstance(DateFormat.MEDIUM); DateFormat longDf = DateFormat.getTimeInstance(DateFormat.LONG); DateFormat fullDf = DateFormat.getTimeInstance(DateFormat.FULL); System.out.println(" 1. " + defaultDf.format(now)); System.out.println(" 2. " + shortDf.format(now)); System.out.println(" 3. " + mediumDf.format(now)); System.out.println(" 4. " + longDf.format(now)); System.out.println(" 5. " + fullDf.format(now)); } }The output is
1. 10:09:12 PM 2. 10:09 PM 3. 10:09:12 PM 4. 10:09:12 PM EDT 5. 10:09:12 PM EDTUse getDateTimeInstance to get a date and time format. For example,
import java.text.DateFormat; import java.util.Date; public class DateFormatExample { public static void main(String[] args) { Date now = new Date(); DateFormat defaultDf = DateFormat.getDateTimeInstance(); DateFormat shortDf = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT); DateFormat mediumDf = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.SHORT); DateFormat longDf = DateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.SHORT); DateFormat fullDf = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.SHORT); System.out.println(" 1. " + defaultDf.format(now)); System.out.println(" 2. " + shortDf.format(now)); System.out.println(" 3. " + mediumDf.format(now)); System.out.println(" 4. " + longDf.format(now)); System.out.println(" 5. " + fullDf.format(now)); System.out.println("============================"); shortDf = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM); mediumDf = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); longDf = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM); fullDf = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM); System.out.println(" 6. " + shortDf.format(now)); System.out.println(" 7. " + mediumDf.format(now)); System.out.println(" 8. " + longDf.format(now)); System.out.println(" 9. " + fullDf.format(now)); System.out.println("============================"); shortDf = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG); mediumDf = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG); longDf = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); fullDf = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.LONG); System.out.println(" 10. " + shortDf.format(now)); System.out.println(" 11. " + mediumDf.format(now)); System.out.println(" 12. " + longDf.format(now)); System.out.println(" 13. " + fullDf.format(now)); System.out.println("============================"); shortDf = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL); mediumDf = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL); longDf = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL); fullDf = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); System.out.println(" 14. " + shortDf.format(now)); System.out.println(" 15. " + mediumDf.format(now)); System.out.println(" 16. " + longDf.format(now)); System.out.println(" 17. " + fullDf.format(now)); } }The output is
1. Jun 21, 2008 9:30:41 PM 2. 6/21/08 9:30 PM 3. Jun 21, 2008 9:30 PM 4. June 21, 2008 9:30 PM 5. Saturday, June 21, 2008 9:30 PM ============================ 6. 6/21/08 9:30:41 PM 7. Jun 21, 2008 9:30:41 PM 8. June 21, 2008 9:30:41 PM 9. Saturday, June 21, 2008 9:30:41 PM ============================ 10. 6/21/08 9:30:41 PM EDT 11. Jun 21, 2008 9:30:41 PM EDT 12. June 21, 2008 9:30:41 PM EDT 13. Saturday, June 21, 2008 9:30:41 PM EDT ============================ 14. 6/21/08 9:30:41 PM EDT 15. Jun 21, 2008 9:30:41 PM EDT 16. June 21, 2008 9:30:41 PM EDT 17. Saturday, June 21, 2008 9:30:41 PM EDT
Comments
Post a Comment