java.time
You said:
need to switch them with new Date and Time API in Java 8
Yes, absolutely. The legacy Date
, Calendar
, and SimpleDateFormat
classes are terrible: confusing and flawed.
You said:
When I retrieve my date from database(it is SimpleDateFormat and I cant change it)
If your date-time value is properly stored in the database in a date-time column, it does not have a "format" because it is not text.
Date-time values stored in date-time columns should be retrieved as date-time objects, not text. If you are receiving text, you should back-track up the food chain of data to fix that problem.
Zoned
If stored in a column of a type akin to the SQL-standard TIMESTAMP WITH TIME ZONE
, retrieve as a OffsetDateTime
.
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
You said:
I want to add some hours(for example 4 PM)
That is a contradiction. Do you want to add four hours?
OffsetDateTime odtFourHoursLater = odt.plusHours( 4 ) ;
Or do you want to set the time-of-day to 4 PM?
OffsetDateTime odtSetToFourPm = odt.with( LocalTime.of ( 16 , 0 ) ) ;
You said:
that result I need to be in time zone which user selected from the dropdown(Lets say America/Caracas),
The OffsetDateTime
class supported by JDBC 4.2 represents a moment as seen through an offset-from-UTC, a number of hours-minutes-seconds. Most databases will deliver this object to you with an offset of zero hours-minutes-seconds.
Understand that a time zone is not an offset. A time zone is a named history of the past, present, and future changes to the offset used by the people of a particular region. A time zone has a name in the format of Continent/Region
.
To represent a moment as seen in a time zone rather than an offset, use ZonedDateTime
class.
ZoneId zoneId = ZoneId.of( "America/Caracas" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( zoneId ) ;
I cannot provide more specific code examples because your question is confused. Your exact goal is not clear.
Not zoned
If stored in a column of a type akin to the SQL-standard TIMESTAMP WITHOUT TIME ZONE
, retrieve as a LocalDateTime
.
LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class ) ;
This type cannot represent a moment, a point on the timeline. Lacking the context of a time zone or offset-from-UTC means we do not know if, for example, noon on a particular date is noon in Tokyo Japan, noon in Toulouse France, or noon in Toledo Ohio US — all different moments, several hours apart. Therefore, this type cannot be adjusted into an arbitrary time zone.
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
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?