title: How to correctly handle this Date field mapping using JdbcTemplate into a Spring Boot project?
body: <p>it is pretty long time that I am not using Spring and I am finding some difficulties with this JdbcTemplate row mapper mapping a date field.</p>
  
  <p>I try to explain my situation in details.</p>
  
  <p>On my database (MariaDB) I have this table named <strong>TREND002</strong>:</p>
  
  <pre><code>+-----------------+----------+------+-----+---------+-------+
  | Field           | Type     | Null | Key | Default | Extra |
  +-----------------+----------+------+-----+---------+-------+
  | Time_Stamp      | datetime | NO   | PRI | NULL    |       |
  | Time_Stamp_ms   | int(11)  | NO   | PRI | NULL    |       |
  | Sensor001DV     | double   | YES  |     | NULL    |       |
  | Sensor002DV     | double   | YES  |     | NULL    |       |
  | Sensor003DV     | double   | YES  |     | NULL    |       |
  ..............................................................
  ..............................................................
  ..............................................................
  | Sensor00NDV     | double   | YES  |     | NULL    |       |
  </code></pre>
  
  <p><strong>NOTE-1:</strong> the <strong>Time_Stamp</strong> field contains value as <strong>2010-10-22 16:50:12</strong></p>
  
  <p>Then I have this DTO class mapping the field of the previous table:</p>
  
  <pre><code>public class TrendDTO {
  
      private Date dateTime;
      private int timeStampMs;
      private String sensorValue;
  
      public TrendDTO() {
          super();
      }
  
      public TrendDTO(Date dateTime, int timeStampMs, String sensorValue) {
          super();
          this.dateTime = dateTime;
          this.timeStampMs = timeStampMs;
          this.sensorValue = sensorValue;
      }
  
      public Date getDateTime() {
          return dateTime;
      }
  
      public void setDateTime(Date dateTime) {
          this.dateTime = dateTime;
      }
  
      public int getTimeStampMs() {
          return timeStampMs;
      }
  
      public void setTimeStampMs(int timeStampMs) {
          this.timeStampMs = timeStampMs;
      }
  
      public String getSensorValue() {
          return sensorValue;
      }
  
      public void setSensorValue(String sensorValue) {
          this.sensorValue = sensorValue;
      }
  
      @Override
      public String toString() {
          return "TrendDTO [dateTime=" + dateTime + ", timeStampMs=" + timeStampMs + ", sensorValue=" + sensorValue + "]";
      }
  
  }
  </code></pre>
  
  <p>Basially in this DTO class I have:</p>
  
  <ul>
  <li>A <strong>Date</strong> field mapping the <strong>datetime</strong> field of my table.</li>
  <li>An <strong>int</strong> field mapping the Time_Stamp_ms field of my table.</li>
  <li>A <strong>String</strong> field mapping the value of a specific <strong>SensorXXXDV</strong> field of the table (I know that from a relational database point of view this is ugly as hell...but I inherited the project and at the moment I can't change).</li>
  </ul>
  
  <p>Finnally I have a <strong>repository</strong> class in which there is this method:</p>
  
  <pre><code>@Override
  public List&lt;TrendDTO&gt; findTrendListBySensorName(String tableName, String columnName) {
  
      List&lt;TrendDTO&gt; result = null;
  
      String RETRIEVE_TREND_BY_SENSOR_NAME = "SELECT Time_Stamp, Time_Stamp_ms, Sensor240DV FROM Delphys.TREND003";
  
      //result = jdbcTemplate.query(RETRIEVE_TREND_BY_SENSOR_NAME, BeanPropertyRowMapper.newInstance(TrendDTO.class));
  
      result =  jdbcTemplate.query(
              RETRIEVE_TREND_BY_SENSOR_NAME,
              (rs, rowNum) -&gt;
  
                      new TrendDTO(
                              new java.util.Date(rs.getTimestamp("Time_Stamp").getTime()),
                              rs.getInt("Time_Stamp_ms"),
                              String.valueOf(rs.getDouble(columnName))
                      )
      );
  
      return result;
  }
  </code></pre>
  
  <p>It works fine but doing in this way I obtain a list of <strong>TrendDTO</strong> instances containing <strong>dateTime</strong> field values like this: </p>
  
  <p><strong>dateTime=Fri Oct 22 16:50:12 PDT 2010</strong></p>
  
  <p>The date is perfectly correct but I think that this is the wrong format. I have to return this DTO object in JSON format to a front end and I need a date in the format: </p>
  
  <p><strong>2010-10-22 16:50:12</strong></p>
  
  <p>I also tried to change my mapper code using this:</p>
  
  <pre><code>result =  jdbcTemplate.query(
                  RETRIEVE_TREND_BY_SENSOR_NAME,
                  (rs, rowNum) -&gt;
  
                          new TrendDTO(
                                  //new java.util.Date(rs.getTimestamp("Time_Stamp").getTime()),
                                  rs.getTimestamp("Time_Stamp"),
                                  rs.getInt("Time_Stamp_ms"),
                                  String.valueOf(rs.getDouble(columnName))
                          )
          );
  </code></pre>
  
  <p>As you can see I am simply using <strong>rs.getTimestamp("Time_Stamp")</strong> but doing in this way I am obtaining a <strong>dateTime</strong> inizialization like this: <strong>2010-10-22 16:50:12.0</strong></p>
  
  <p>As you can see it end with a <strong>.0</strong> that should represents milliseconds that I don't want. Can I specify the format to avoid to put also this ending millisecond section?</p>
  
  <p>Another possible approach to solve my problem (maybe the best solution) is: in this pretty horrible database table the millisecond information is contained into the <strong>Time_Stamp_ms</strong> column that, at the moment, is mapped with my <strong>timeStampMs</strong> DTO field. </p>
  
  <p>Can I modify my previous code to encapsulate this information directly into the <strong>dateTime</strong> field of my DTO object?</p>
  
  <p>I am absolutly not sure that with this syntax (is it a lambda expression?) this is possible. Can I do it using this syntax or have I to implement a row mapper class to implement this behavior?</p>