Presentation and storage of data - human age

I have a design problem that I am facing, I bring it up here, so maybe I can get more ideas.

In my project, I have a scenario where I need to imagine the age of the children.

The age can be from 0 to 15. With the age of 1 to 15 years, I can store it as an integer, but the problem is that the age is from 0 to 1. Then I want to store it for several months.

Saving the age as java.util.Date will not work, because I do not want to compare with the current date (I am dealing with a really large database). relatively floating point, well, you know that there are 12 months a year, so I need to translate from 0.5 to 6 months and 0.7 so that I don't know ...

I am looking for a good way to store age in one type. It could be Enum , etc. It should be as small as possible. and it should be possible to save the type in the database without any problems ...

+4
source share
6 answers

I would suggest two things:

  • In the database, save the date of birth as the date in the appropriate database date type. If you keep the age in the database, it will be constantly out of date and needs to be updated. You said that your database is large, but that should not be the reason for maintaining transient value, such as "age". Put a good indicator on the date of birth, and let the database calculate the age for you when necessary.

  • Use one of the above sentences to represent the (calculated) age, respectively, in the java class if you need to (although this is even doubtful). One way would be to have separate members for “months” and “years,” and months would be significant only when the value of years is zero.

+4
source

You can always save the age in the corresponding small block (for example, in months) and convert it to years when you want.

+7
source

I would do this in one of two ways:

  • Keep the age for each for several months and convert it to years when necessary.
  • You have an int for age and there is a boolean flag that shows if age years or months.
+2
source

When you store values ​​relative to the current date, you need to update the values ​​daily. You can still save the date of birth in numerical representation, days from 1.1.1900 are reported.

+2
source

TL; DR

SQL ...

 SELECT * FROM person WHERE birthdate <= ? ; 

For many years...

 LocalDate.now( ZoneId.of( "Africa/Tunis" ) ) .minus( Period.ofYears( 15 ) ) 

And for several months ...

 LocalDate.now( ZoneId.of( "Africa/Tunis" ) ) .minus( Period.ofMonths( 6 ) ) 

More details

The accepted answer of GreyBeardedGeek is correct. You must store the date value in the column date type. Create your queries as a search for strings whose birth date is before / after a certain date for a certain age.

java.time

Here is an example of Java code. The modern approach uses the java.time classes built into Java 8 and later.

Avoid the nasty old time classes like Date and Calendar . They are poorly designed and confused. Now they are deprecated from JSR 310 .

LocalDate

The LocalDate class represents a date value only without time and without a time zone.

The time zone is critical for determining the date. At any given moment, the date changes around the world by zone. For example, a few minutes after midnight in Paris, France is a new day, still "yesterday" in Montreal Quebec .

Specify the time zone name in continent/region format, such as America/Montreal , Africa/Casablanca or Pacific/Auckland . Never use the abbreviation 3-4 letters, for example EST or IST , as they are not real time zones, and are not standardized and not even unique (!).

 ZoneId z = ZoneId.of( "America/Montreal" ); LocalDate today = LocalDate.now( z ); 

Period

Imagine a time span that is not tied to a timeline, like Period or Duration .

 Period fifteenYears = Period.ofYears( 15 ) ; 

Calculate the target date for the SQL query.

 LocalDate ld = today.minus( fifteenYears ) ; // Get the date fifteen years ago. 

Your SQL will run something like this example. ? is a placeholder for the LocalDate value defined above. A comparison using <= or < depends on your age-specific business rules.

 SELECT * FROM person WHERE birthdate <= ? ; 

Your problem about months compared to years is not a problem at all. Use Period months, not years.

 Period sixMonths = Period.ofMonths( 6 ) ; 

You might want to make this range of probable Period objects permanent, or perhaps Enum .

JDBC 4.2

As in JDBC 4.2, we can directly exchange java.time types with the database. Time-related java.sql classes are now deprecated, for example java.sql.Timestamp and java.sql.Date . Good disposal of these bugs.

 myPreparedStatement.setObject( 1 , ld ) ; 

... and ...

 LocalDate birthdate = myResultSet.getObject( … , LocalDate.class ) ; 

Two criteria

If you need only 15-year-olds, for example, rather than 16-year-olds, use two criteria: a pair of early and late birth dates.

 LocalDate earlierBirthDate = today.minus( Period.ofYears( 16 ) ) ; LocalDate laterBirthDate = today.minus( Period.ofYears( 15 ) ) ; 

SQL would be something like this:

 SELECT * FROM person WHERE birthdate > ? AND birthdate <= ? ; 

About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy datetime classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , is advised to switch to the java.time classes.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .

Where to get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .

+1
source

I would do

 enum Age { ZERO_MONTHS , ONE_MONTH , TWO_MONTHS , THREE_MONTHS , ... , ELEVEN_MONTHS , ONE_YEAR , TWO_YEARS , THREE_YEARS , ... , FIFTEEN_YEARS } 
0
source

Source: https://habr.com/ru/post/1385633/


All Articles