Hello I'm working on my system and I'm stuck right now on how to save separably the month day and year to a separate column .. In my system I have an objective that let user print report month or daily basis , and my solution is to save the day month and year to a each separate columns.. Is this posible ? Sorry for my bad english .
-
which Database? and whats table structure? – Deepkamal May 03 '17 at 16:49
-
I use MySql , I have a table name Logs .. with column ID Name Date .. I want to make it ID NAME MONTH YEAR DAY . is it possible ? – kaisar Great May 03 '17 at 16:54
-
@kaisarGreat Please edit your Question to provide detail and clarification rather than posting as a Comment. And no need to apologize for your English as we all know Stack Overflow is used around the world – and your English is quite good (better than the English of many Americans!). – Basil Bourque May 03 '17 at 18:19
-
Is it possible? Yes. However, this greatly depends on what your actual requirements mandate. Are you saying that they need a different *type* to store based on month or day, or are you saying you absolutely need to represent a date as either a month, a day, or both? – Makoto May 03 '17 at 22:01
3 Answers
For example, you can take system date as String, as it can be seen from this resource: How to convert current date into string in java?
ZoneId zoneId = ZoneId.of( "America/Montreal" ) ; // Or ZoneOffset.UTC or ZoneId.systemDefault()
LocalDate today = LocalDate.now( zoneId ) ;
String output = today.toString() ;
Later you can use split to divide string for needed values (Year, month of day), for example, like here: How to split a String by space
str = "2017-05-03";
String[] splited = str.split("-");
In the last example instead "-", maybe, you need to use ASCII code of "-". Alas, I forget this

- 1
- 1

- 1,920
- 2
- 23
- 38
-
Sorry for late update , This approach works fine with me . thanks :D – kaisar Great May 05 '17 at 13:15
-
You can use the LocalDate from JDK 8 Date-Time API as below to get year, month and day and persist them in DB:
LocalDate date = LocalDate.now();
int year = date.getYear();
int month = date.getMonthValue(); // or Month month = date.getMonth() if you want month names
int day = date.getDayOfMonth();

- 3,661
- 1
- 15
- 19
-
Good answer, but I recommend always specifying the optional argument of your desired/expected time zone `ZoneId` in the call to `LocalDate.now` rather than relying implicitly on the JVM’a current default time zone that can change at any moment during runtime. – Basil Bourque May 03 '17 at 18:30
The other Answers are correct about using LocalDate
in Java.
Database
As for the database, define your column as a date-only type such as the SQL Standard‘s type DATE
.
JDBC
If your JDBC driver complies with JDBC 4.2, you can use the java.time types such as LocalDate
directly.
myPreparedStmt.setObject( … , myLocalDate ) ;
And:
LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;
If your driver does not comply, fall back to using the java.sql types. You may convert to-and-fro by calling new methods added to the old classes. But use the java.sql types as briefly as possible, doing all the real work with java.time classes.
myPreparedStmt.setDate( … , java.sql.Date.valueOf( myLocalDate ) ) ;
…and…
LocalDate ld = myResultSet.getDate( … ).toLocalDate() ;

- 303,325
- 100
- 852
- 1,154