Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wrote a static utility method that you can just drop in and use...and hopefully it is clear enough to demonstrate the proper use of the SimpleDateFormat parse() and format() methods:</p> <pre><code> /** * Returns a reformatted version of the input date string, where the format * of the input date string is specified by dateStringFormat and the format * of the output date string is specified by outputFormat. Format strings * use SimpleDateFormat format string conventions. * * @param dateString input date string * @param dateStringFormat format of the input date string (e.g., dd/MM/yyyy) * @param outputFormat format of the output date string (e.g., MMM dd, yyyy) * * @return reformatted date string * * @throws ParseException if an error occurs while parsing the input date * string using the provided format * * @author Steve */ public static final String reformatDateString(final String dateString, final String dateStringFormat, final String outputFormat) throws ParseException { final SimpleDateFormat dateStringParser = new SimpleDateFormat(dateStringFormat); final SimpleDateFormat outputFormatter = new SimpleDateFormat(outputFormat); return outputFormatter.format(dateStringParser.parse(dateString)); } </code></pre> <p>You call it as follows:</p> <pre><code> System.out.println(reformatDateString("2013-5-28", "yyyy-MM-dd", "dd/MMM/yyyy")); </code></pre> <p>Which, in this example, will output this:</p> <pre><code> 28/May/2013 </code></pre> <p>The basic idea is that you generally use a SimpleDateFormat instance for one of two things:</p> <ol> <li>To convert a String containing a date with a known format to a java.util.Date instance...using the parse() method, or</li> <li>To convert a java.util.Date instance to a String of a specified format...using the format() method.</li> </ol> <p>I am doing both in one line in the method that I wrote using the two different SimpleDateFormat instances that I create in that method - one created using the input format (for parsing the original String into a Date instance)...and one created using the output format (for converting the created Date back into a String with the desired format).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload