Input: 2017-03-15

String date="2017-03-15"; LocalDate localedate = LocalDate.parse( date ); cal = Calendar.getInstance(); cal.set(Calendar.YEAR, localedate.getYear()); cal.set(Calendar.MONTH, localedate.getMonthValue()); cal.set(Calendar.DAY_OF_MONTH, localedate.getDayOfMonth()); date1 = cal.getTime(); 

At the exit I get - Sat Apr 15 16:12:47 GMT + 02: 00 2017. Why is there a month error? 2nd moment: I write down the date in postgresql, and in my database not only the date but also the time is recorded. How to avoid it. For reference: the type of the column in postgresql, I use date. The documentation says - the date (without the time of day) . In Pojo, I use java.util.Date respectively on a table.

    1 answer 1

    From the LocalDate documentation, getMonthValue () returns the month value from 1 to 12, for the Calendar, set (Calendar.MONTH, ...) takes month values ​​from 0, see Calendar.JANUARY, etc.

    java.util.Date - contains the date and time, if only the date is stored in the database, then after reading from the database your fields of the type java.util.Date will contain the date and 00:00:00. To avoid use immediately LocalDate.

    • Thank! Almost succeeded, only replacing Data with LocalDate works for 50%, everything is fine in the heap. But when writing to PostgreSql, it says: Caused by: org.postgresql.util.PSQLException: ERROR: column "date_when" is a type of bytea Hint: - titsi
    • All the problem solved. - titsi