Using java.time
Your Question is a duplicate of many others. So briefly…
Use java.time classes rather than the troublesome old legacy date-time classes. For Android, use libraries from the ThreeTen-Backport and ThreeTenABP projects.
Get today’s date.
ZoneId z = ZoneId.of( "America/Montreal" ) )
LocalDate today = LocalDate.now( z );
Get to parts of the date.
int y = today.getYear() ;
int m = today.getMonthValue() ;
int d = today.getDayOfMonth() ;
Query the database.
myPreparedStatement.setInt( 1 , y ) ;
myPreparedStatement.setInt( 2 , m ) ;
myPreparedStatement.setInt( 3 , d ) ;
As others suggested, you should be using date-time types in your database to store date-time values rather than mere ints for the pieces.
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.
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.