Creating a java.util.Date gives you the date all right, but if you need to format it in a particular way, say, for a database query, you need to use a formatter, such as the SimpleDateFormat class. For example, this code turns today's date into the string "06-27-2004":
java.util.Date theDate = new java.util.Date;
java.text.SimpleDateFormat dateFormatter =
new java.text.SimpleDateFormat("MM-dd-yyyy");
String theFormattedDate = dateFormatter.format(theDate);
You can format it any way that you like. Check out Customizing Formats for more pattern options.
Technorati tags: Java | dates | formatting | formatting java dates | java.util.Date | Calendar | java.text.SimpleDateFormat | java.text.DateFormat |
Comments