如何在Spring Data JPA中显示具有特定格式的时间戳列

时间:2013-04-18 01:49:29

标签: spring jpa

我正在使用Spring Data JPA,我有一个如下表:

public class Apk {

  @Temporal(TemporalType.TIMESTAMP)
  @Column
  private java.util.Date creationTime;

}

我的DBMS是MySQL5.x,上面的列是在其中定义的日期时间类型。我只是在存储库类中调用findAl()方法扩展PaginAndSortingRepository。

public interface ApksRepository extends PaginAndSortingRepository<Apk, Long>{

}


public class ApksServiceImpl implements ApksService {

    public PagingRes<Apk> findAll(PageInfo pageInfo){
        PaginRes<Apk> result = new PagingRes<Apk>();

        Page page = apksRepos.findAll(pageInfo.toPageRequest());
        result.fromPage(page);

        return result;
    }
}

public class PageInfo {

  private int page;
  private int rp;
  private String sortname;
  private String sortorder;
  private String query;
  private String qtype;

  //getters and setters

  public PageRequest toPageRequest() {

     Sort.Direction direction = Sort.Direction.ASC;
     if (sortorder!=null && "DESC".equals(sortorder))
         direction = Sort.Direction.DESC;
     return new PageRequest(page-1, rp, direction, sortname);
  }
}


public class PagingRes<T> {

   private long total;
   private int page;
   private int rowPerPage;
   private List<T> rows;

   //getters and setters

   public PagingRes<T> fromPage(Page page) {
       this.page = page.getNumber();
       this.rowPerPage = page.getSize();
       this.total = page.getTotalElements();
       this.rows = page.getCotent();
       return this;
   }
}

我正在尝试在表格中显示数据,包括列,但是当我这样做时,列显示为长类型。我想在fomat'dd-MM-yyyy hh:mm:ss'中显示列。我怎么能这样做?

先谢谢。

1 个答案:

答案 0 :(得分:0)

我知道用于JSON响应的杰克逊映射器给出了奇怪的结果。所以在搜索之后,我使用JsonSerializer来修复它,如下所示:

Entity class

              @JsonSerialize(using=AuDateSerializer.class)  
              private java.util.Date eventTime;




Custom Serializer
              public class AuDateSerializerextends JsonSerializer<Date> {
              @Override
              public void serialize(Date value, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonProcessingException {
                DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                jgen.writeString(formatter.format(value));
              }
            }

现在它运作正常。感谢。