Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The issue is probably that <code>sdf.parse(date);</code> will be null if <code>date</code> does not match the pattern. If you change the pattern of the SimpleDateFormat object but don't change the format of the date you are parsing the ParseException will be thrown and <code>date_out</code> will be null. <code>sdf.format()</code> will then throw a NullPointerException when you try to format the null string.</p> <p>Based on your comment I think a bit more explanation is required with some examples - so I'm editing my answer appropriately...</p> <p>SimpleDateFormat applyPattern changes the pattern of the SimpleDateFormat object it acts in the same way as the string in the constructor does, that is, it tells the Formatter which pattern to expect / use for output.</p> <p>For what I think you want, you need two SimpleDateFormatters, an input one and an output one. eg.</p> <pre><code>SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S"); SimpleDateFormat sdfOutput = new SimpleDateFormat(yyyy MM dd"); Date date_out = null; try { date_out = sdfInput.parse(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } String myOutputtedDate = sdfOutput.format(date_out); </code></pre> <p>Having said that I guess you could put applyPattern after the parsing, and before the output like so...</p> <pre><code>SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S"); String reformattedDate = ""; Date date_out = null; try { date_out = sdf.parse(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } sdf.applyPattern("yyyy MM dd"); if (date_out != null) { reformattedDate = sdf.format(date_out); } </code></pre> <p>Hope this helps a bit more.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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