Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Java data type will Date of Birth be? [duplicate]

I am currently making an App on Android where a user will Register their details.

In my database I have DOB as a Date type.

Would I declare DOB in Java as a String? Double? Int? I'm confused how I will go about this so it satisfies the database's Date format.

Any help will be great!


1 Answers

A java.time.LocalDate (or logically-equivalent classes likes org.joda.time.LocalDate) is the best way to represent a date-of-birth (DOB) in Java code.

Note that DOB is not well-modelled as a java.sql.Date, or a java.util.Date: these model instants in time, not timezone-independent dates.

Whilst one is clearly born at some instant in time, this is not how we, societally, report when we are born. Consider that two children born at the same instant in different time zones might actually have different dates of birth - if the child in NYC is born at 2AM on 1st April, the child born in LA is born at 10PM on 31st March.

Neither child's date of birth changes when they travel to the other city (or anywhere else, of course). As such, if you store the instant of birth, you also need to store the time zone of birth in order to correctly interpret that instant as the date of birth.

But storing an instant and a timezone is inconvenient: not only is it more data to store, but also it is difficult to interpret it "by eye" (e.g. it's hard to tell what date (1459438541000, America/Los_Angeles) refers to). It is much easier to store and interpret the year, month and day directly: (2016, 3, 31).

like image 138
Andy Turner Avatar answered Oct 24 '25 00:10

Andy Turner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!