0

In my db table I have a column c_dob of Type date

For my controller Request class, I use the above field like this :

import java.sql.Timestamp;
public class UpdateUserProfileRequest {

    private Timestamp dob;
    public Timestamp getDob() {
        return dob;
    }
    public void setDob(Timestamp dob) {
        this.dob = dob;
    }
}

Problems :

  • What happens is when I have the value 1979-06-30 passed as a request json value, in the controller class, the request class logs the value as 1979-06-30 02:00:00.0 (hour part is added and given a value on its own)

  • Second, for setting the dob the following code I am using :

    Map<String, Object> outMap = proc.execute(inParams);       
    profile.setDob((Date) outMap.get("out_dob") != null
                             ? new Timestamp(((Date) outMap.get("out_dob")).getTime()) : null);
    

And what happens here is that 1979-06-30 02:00:00.0 is changed to 1979-06-30 01:00:00.0 which causes logical problem.

So, is there any reason or any solution for this behavior?

Edit : Database is MySql

6
  • What database? And are you sure the data type is named exactly DATE? Commented Dec 8, 2020 at 6:42
  • 1
    FYI, the class java.sql.Timestamp is terribly flawed, and was supplanted years ago by the modern java.time classes. Commented Dec 8, 2020 at 6:44
  • @BasilBourque Database is mysql Commented Dec 8, 2020 at 6:58
  • 1
    No, java.sql.Time is also one of the terrible legacy classes. You need to learn about the classes in the java.time package, defined in JSR 310. Search Stack Overflow to learn more. There are many hundreds of existing Questions and Answers. Commented Dec 8, 2020 at 7:14
  • 1
    Don't use the javax.sql classes in your model. Instead use the newer java.time ones. Also Timestamp is both a data and time hence the time will be added. If you really want to stick with the javax.sql stuff (which I hardly recommend) use Date which is only a data without time. Commented Dec 8, 2020 at 8:21

2 Answers 2

2

You are using the wrong data types. The MySQL type DATE represents a date-only value, without time-of-day and without time zone. In contrast, the java.sql.Timestamp class represents a moment, a specific point on the timeline, a date with time-of-day as seen in UTC. Square peg, round hole.

Use only java.time classes. Never use the terrible legacy date-time classes such as Timestamp.

For MySQL DATE use java.time.LocalDate with a JDBC driver that complies with JDBC 4.2 or later.

LocalDate localDate = myResultSet.getObject( … , LocalDate.class ) ;
Sign up to request clarification or add additional context in comments.

Comments

0

Make use of java.time.LocalDateTime which is timezone aware and should help you a lot...

java.sql.Timestamp is a terrible and some leftover of the dark ages :)

1 Comment

Thanks for wanting to contribute. java.sql.Timestamp is a terrible … Correct. java.time.LocalDateTime which is timezone aware … Incorrect. … and should help you a lot... It’s far better to use LocalDate for a date without time of day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.