2016-12-21 39 views
0

當我將實體保存在資源庫personRepository.save(person)中時,此返回對象的格式如下所示:Wed Dec 21 13:38:00 CET 2016 。我可以如何將其更改爲如下格式:2016-12-21 13:38:00.732?轉換完成的地方,如何改變?在我的數據庫中,日期以這種格式保存:2016-12-21 13:38:00.732。保存和返回實體中的日期格式(Spring-Data)

@Entity 
public class Person { 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private Long id; 
    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "update_date") 
    private java.util.Date updateDate; 

public interface PersonRepository extends CrudRepository<Person, Long> {} 
+0

「我的數據庫日期以這種格式保存:2016-12-21 13:38:00.732「。不正確。您用來查看數據庫的工具以該格式顯示它。日期是一個日期。 Wed Dec 21 13:38:00 CET 2016是調用toString()的結果。要以給定的格式顯示它,請使用適合您的目的的格式器。 –

+0

這個工具在實體中的字段java.util.Date中的eclipse中是debuger。我有另一個dao類(標準查詢),在那裏我得到這個實體(同一個實體),並且有一個格式2016-12-21 13:38:00.732 – Wait

+1

您可以使用各種解決方案以各種不同格式打印日期取決於你想寫的地方。在標準Java API中,您可以使用https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html類的實例以特定格式打印日期。 –

回答

-1

您應該使用「org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime」。首先,你改變你的人實體

@Entity 
public class Person {  

    @Column(name = "update_date") 
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime") 
    private LocalDateTime updateDate; 

} 

然後,更新日期時間的地方,比如PersonalImpl

 import org.joda.time.LocalDateTime; 

    Class PersonalImpl { 

     void updatePerson(...){ 
      Person person = new Person(); 
      //do update person data 
      person.setUpdateDate(LocalDateTime.now()); 
     } 

    } 

的PostgreSQL 9.5節目DB 2014年9月10日07:59:14.822

+0

爲什麼?爲什麼他應該使用它? –

+0

嗨艾倫海伊,我只是建議他使用此進一步格式日期時間。那麼如果我們加載人員信息(例如API)。 –

+0

然後,我們將覆蓋任何格式的JsonDeserializer或JsonSerializer org.joda.time.DateTime –