title: Local Time to UTC in Java
body: <p>I am trying to convert local time to UTC time in java. I have written the code:</p>
  
  <pre><code>    String datesToConvert = "31-12-2018 23:37:00";
      String dateFormat = "dd-MM-yyyy HH:mm:ss";
  
      SimpleDateFormat sdf2 = new SimpleDateFormat(dateFormat);
      sdf2.setTimeZone(TimeZone.getDefault());
      Date gmt = null;
  
      SimpleDateFormat sdfOutPutToSend = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
      sdfOutPutToSend.setTimeZone(TimeZone.getTimeZone("UTC"));
  
      try {
          gmt = sdf2.parse(datesToConvert);
          System.out.println("UTC FORMATTED DATE : " + sdfOutPutToSend.format(gmt));
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>The Output I am getting is :
  UTC FORMATTED DATE : 2018-12-31T18:07:00.000Z</p>
  
  <p>But I want it to be same date and time but changes in .000Z at last which shows the time difference.</p>