How to Convert the Date Format in Java
The Java standard API comes with a number of special classes to assist programmers in dealing not just with dates but with the various ways they can use strings to represent dates. Performing a conversion between different string date representations will depend primarily upon three classes within the Java API: Date, SimpleDate, and DateFormat.
Instructions
-
-
1
Create a DateFormatConverter class. Paste the following class skeleton into it:<br /><br /><br />import java.text.DateFormat<br />GO<br />import java.text.SimpleDateFormat<br />GO<br />import java.util.Date<br />GO<br /><br />/**<br /> * This class provides a method for converting <br /> * between multiple date formats in Java.<br /> * @author Kevin Walker<br /> */<br />public class DateFormatConverter {<br /><br />}
-
2
Create the formatConverter class. Paste the following inside the DateFormatConverter class:<br /><br /> /**<br /> * Converts a date from one format to another.<br /> *<br /> * @param date a string representing the date.<br /> *<br /> * @param currentFormat The format currently used. Must be one of the constants defined in DateFormat<br /> * <br /> * @param newFormat The format to convert to. Must be one of the constants defined in DateFormat<br /> *<br /> * @return a string representing the date<br /> *<br /> * @throws Exception <br /> */<br /> public static String formatConverter(String date, int currentFormat, int newFormat) throws Exception {<br /> <br /> // If currentFormat or newFormat are invalid, throw an exception<br /> if (!((currentFormat == DateFormat.LONG ||<br /> currentFormat == DateFormat.FULL ||<br /> currentFormat == DateFormat.MEDIUM ||<br /> currentFormat == DateFormat.SHORT) &&<br /> (newFormat == DateFormat.LONG ||<br /> newFormat == DateFormat.FULL ||<br /> newFormat == DateFormat.MEDIUM ||<br /> newFormat == DateFormat.SHORT))) {<br /> throw new Exception(\"Invalid argument\")<br />GO<br /> }<br /><br /><br /> DateFormat df = SimpleDateFormat.getDateInstance(currentFormat)<br />GO<br /><br /> Date d = df.parse(date)<br />GO<br /> <br /> DateFormat new = DateFormat.getDateInstance(newFormat)<br />GO<br /> return new.format(d)<br />GO<br /><br /> }<br /><br />Going line by line, first comes the JavaDoc comment. This comment block describes the functionality of the function in a special format that can be read by the automatic documentation program JavaDoc.<br /><br />Then, after the method is declared, the function checks to insure that the format parameters are valid and returns an error if they are not.<br /><br />Next, a DateFormat object is created using the format of the current date. It is then used to parse it into a Date object. <br /><br />A new DateFormat object is created using the desired new format, and the date is returned with that formatting.
-
-
3
Create a main method to illustrate how the class is used. <br /><br /> public static void main(String[] args) {<br /> String date = \"2/16/98\"<br />GO<br /> try {<br /> System.out.println(formatConverter(date, DateFormat.SHORT, DateFormat.FULL))<br />GO<br /> } catch (Exception e) {<br /> System.out.println(\"Error.\")<br />GO<br /> e.printStackTrace()<br />GO<br /> }<br /><br /> }<br /><br />This takes a date, in this case \"2/16/98,\" which is in the SHORT format. <br /><br />It is run through the format converter in order to change it to use the FULL format, with a printed result. In this case, \"Monday, February 16, 1998.\"
-
1
References
- Photo Credit calendar 2007 image by Franc Podgoršek from Fotolia.com