简体   繁体   中英

LocalDateTime getting updated by 4 hours everyTime I do repository.save()

I am trying to update the class test in JAVA . Here when I am trying to update it as shown below It always adds 4 hours to the created Date. I am doing something like this. I am using LocalDateTime for createdDate. I think 4 hours is because of the Zone.

Any help is appreciated. Please let me know.

Thanks in Advance. :)

    public Test updateTest(TestDTO testDTO, Employee employee) throws 
     TransactionSystemException {
    Test test = TestRepository.findOne(testDTO.getId());

    Test returnTest = null;

    testDTO = (TestDTO) CustomData.convertData(testDTO);

    test.setTestDate(testDTO.getTestDate());
    if(testDTO.getCreatedDate() != null){
       test.setCreatedDate(test.getCreatedDate());
    }
    test.setCreatedBy(test.getCreatedBy());
    test.setTestProcedureId(testDTO.getTestProcedureId());
    test.setCollectionSiteType(testDTO.getCollectionSiteType());
    test.setMobileId(testDTO.getMobileId());

    returnTest = TestRepository.save(test);

    return returnTest;
    }

    // Test Domain
    @Entity
    @Table(name = "test")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    public class Test implements Serializable {
    @Id
    @NotNull
    @Size(max = 30)
    @Column(name = "id", length=30)
    private String id;

    @Column(name = "created_date")
    private LocalDateTime createdDate;

    @Column(name="created_by")
    private Long createdBy;

    @Column(name="test_procedure_id", nullable = false)
    private Long testProcedureId;

    @Column(name="collection_site_type", nullable = false)
    private String collectionSiteType;

    @Column(name="mobile_id")
    private Long mobileId;

     @Column(name = "updated_date")
     private LocalDateTime updatedDate;
    // --------------------- GETTER / SETTER METHODS ---------------------

    public String getId() {
    return id;
    }

    public void setId(String id) {
    this.id = id;
    }

    public Long getTestProcedureId() {
    return testProcedureId;
    }

    public LocalDateTime getCreatedDate() {
    return createdDate;
    }

    public void setCreatedDate(LocalDateTime createdDate) {
    this.createdDate = createdDate;
    }

    public Long getCreatedBy() {
    return createdBy;
    }

    public void setCreatedBy(Long createdBy) {
    this.createdBy = createdBy;
    }

    public void setTestProcedureId(Long testProcedureId) {
        this.testProcedureId = testProcedureId;
    }

    public String getCollectionSiteType() {
        return collectionSiteType;
    }

    public void setCollectionSiteType(String collectionSiteType) {
        this.collectionSiteType = collectionSiteType;
    }

    public Long getMobileId() {
        return mobileId;
    }

    public void setMobileId(Long mobileId) {
           this.mobileId = mobileId;
    }

    public LocalDateTime getUpdatedDate() {
        return updatedDate;
    }

    public void setUpdatedDate(LocalDateTime updatedDate) {
        this.updatedDate = updatedDate;
    }
    }

https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html <-- 尝试 ZonedDateTime 或只是 util.Date。

tl;dr

Instant.now()  // Capture current moment in UTC.

Use Instant , not LocalDateTime

You do not post the relevant code so we cannot say for certain. But likely you are mishandling time zone. And you are certainly using the wrong class.

The LocalDateTime class cannot represent a moment as it lacks any concept of time zone or offset-from-UTC. So this class is irrelevant to your purpose of logging events.

Instead use Instant . This class represents a moment in UTC, always in UTC by definition.

Instant instant = Instant.now() ;

To adjust into a time zone, such as for display to the user, apply a ZoneId to get a ZonedDateTime object.

ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

See another Question, What's the difference between Instant and LocalDateTime?


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.

To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations. Specification is JSR 310 .

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM