title: How to format LocalDate to ISO 8601 with T and Z?
body: <p>I'm trying to generate a random date and time, and convert it to the <code>&quot;yyyy-MM-dd'T'HH:mm:ss'Z'&quot;</code> format.</p>
  <p>Here is what I have tried:</p>
  <pre><code>  public static String generateRandomDateAndTimeInString() {
      LocalDate date = LocalDate.now().minus(Period.ofDays((new Random().nextInt(365 * 70))));
      System.out.println(&quot;date and time :: &quot; + date.toString());
      return formatDate(date) ;
    }
  
    public static String formatDate(LocalDate date){
      DateFormat dateFormat = new SimpleDateFormat(&quot;yyyy-MM-dd'T'HH:mm:ss'Z'&quot;);
      return dateFormat.format(date);
    }
  </code></pre>
  <p>But in the line <code>dateFormat.format(date)</code>, it complains with:</p>
  <blockquote>
  <p>java.lang.IllegalArgumentException: Cannot format given Object as a Date</p>
  </blockquote>
  <p>The second problem is that, the output of print does not contain the time:</p>
  <pre><code>date :: 1998-12-24 
  </code></pre>
  <p>I don't know how to get it to work.</p>