title: How to ignore milliseconds and format using DateTimeFormatter
body: <p>I am trying to use DateTimeParser and DateTimeFormatter to format the date time. My requirement is to format the input string to "yyyy-MM-dd'T'HH:mm:ssZ" format irrespective of whether the input has milliseconds or not. I am using below code to parse the input. But so far I can get it working for only with or without milliseconds.</p>
  
  <p>This is my method:</p>
  
  <pre><code>public static DateTime convertDateTime(String dateTime){
  DateTimeParser parsers = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ").getParser();
  DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(parsers ).toFormatter().withLocale(Locale.US).withChronology(ISOChronology.getInstanceUTC());
  return DateTime.parse(dateTime,formatter);
  </code></pre>
  
  <p>}</p>
  
  <p>Imports used :</p>
  
  <pre><code>import org.joda.time.DateTime;
  import org.joda.time.chrono.ISOChronology;
  import org.joda.time.format.DateTimeFormat;
  import org.joda.time.format.DateTimeFormatter;
  import org.joda.time.format.DateTimeFormatterBuilder;
  import org.joda.time.format.DateTimeParser;
  </code></pre>
  
  <p>I want my code to accept 2 different input forms(with or without milliseconds) and returns one DateTime without milliseconds.</p>
  
  <p>Examples:</p>
  
  <pre><code>input can be: 
  
      2020-10-01T12:05:22.458-04:00
      2020-10-01T12:05:22-04:00
  Output:
  2020-10-01T12:05:22-04:00
  </code></pre>
  
  <p>Any help on this will be greatly appreciated. Thanks!</p>