Date currentDate = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); System.out.println("Date without any format: " + currentDate); System.out.println("Our beautiful formatted date: " + formatter.format(currentDate));
Explaining the code:
- First we instantiated a variable called currentDate using the default constructor of the java.util.Date class, which creates a Date object with the value as the current date and time
- Then we got a instance of the SimpleDateFormat class, specifying the pattern "yyyy/MM/dd" which means year with four digits, month with two digits (01 to 12) and day of the month with two digits (01 to 31).
- We displayed on the console the currentDate variable (we used the default toString() method to display the variable value)
- Then we displayed the formatted value using the method format of the SimpleDateFormat class
You may find more information at:
http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
No comments:
Post a Comment