In this example, you will learn to display date and time in different formats using dateFormat()
function.
DateFormat provides many class methods for obtaining default date/time formats based on the default or a given locale and a number of formatting styles. The formatting styles include FULL, LONG, MEDIUM, and SHORT. More detail and examples of using these styles are provided in the method descriptions.
DateFormat helps you to format and parse dates for any locale. Your code can be completely independent of the locale conventions for months, days of the week, or even the calendar format: lunar vs. solar.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.text.*; import java.util.*; public class Main { public static void main(String[] args) { Date dt = new Date(1000000000000L); DateFormat[] dtformat = new DateFormat[6]; dtformat[0] = DateFormat.getInstance(); dtformat[1] = DateFormat.getDateInstance(); dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM); dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL); dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG); dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT); for (DateFormat dateform : dtformat) System.out.println(dateform.format(dt)); } } |
1 2 3 4 5 6 |
9/9/18 1:46 AM Sep 9, 2018 Sep 9, 2018 Sunday, September 9, 2018 September 9, 2018 9/9/18 |