Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The method <code>getConvertedDate(String)</code>, will do a plain text parsing for conversion. </p> <pre><code>private String getConvertedDate(String inputDate) { // extract and adjust Month int index = inputDate.indexOf('/'); String month = inputDate.substring(0, index); if (month.length() &lt; 2) { month = "0" + month; } // extract and adjust Day inputDate = inputDate.substring(index + 1); index = inputDate.indexOf('/'); String day = inputDate.substring(0, index); if (day.length() &lt; 2) { day = "0" + day; } // extract Year inputDate = inputDate.substring(index + 1); index = inputDate.indexOf(' '); String year = inputDate.substring(0, index); // extract Hour inputDate = inputDate.substring(index + 1); index = inputDate.indexOf(':'); String hour = inputDate.substring(0, index); // extract and adjust Minute inputDate = inputDate.substring(index + 1); index = inputDate.indexOf(':'); String minute = inputDate.substring(0, index); if (minute.length() &lt; 2) { minute = "0" + minute; } // extract and adjust Second inputDate = inputDate.substring(index + 1); index = inputDate.indexOf(' '); String second = inputDate.substring(0, index); if (second.length() &lt; 2) { second = "0" + second; } // extract AM/PM marker // adjust hour, +12 for PM inputDate = inputDate.substring(index + 1); String am_pm_marker = inputDate.substring(0); if (am_pm_marker.equalsIgnoreCase("pm")) { int hourValue = 0; try { hourValue = Integer.parseInt(hour); } catch (Exception e) { } hourValue += 12; hour = "" + hourValue; if (hour.length() &lt; 2) { hour = "0" + hour; } } else { if (hour.length() &lt; 2) { hour = "0" + hour; } } String outputDate = year + "-" + month + "-" + day; outputDate += "T" + hour + ":" + minute + ":" + second; return outputDate; } </code></pre> <p>Sample input and output:</p> <pre><code>String input = "04/01/2012 9:55:47 pm"; System.out.println("Output: " + getConvertedDate(input)); // Output: 2012-04-01T21:55:47 </code></pre>
 

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