-3

I want to display complete date in mySQL format but don't want to see the Time along with it.

Wanted to increment or decrement the date to perform actions

Ami Hollander
  • 2,435
  • 3
  • 29
  • 47

1 Answers1

0

tl;dr

myResultSet.getObject( … , Instant.class )
.atZone( ZoneId.of( "Pacific/Auckland" ) ) 
.toLocalDate()
.toString() 

Details

For a column of type akin to the SQL-standard TIMESTAMP WITH TIME ZONE, retrieve as a Instant.

Instant instant = myResultSet.getObject( … , Instant.class ) ;

An Instant is always in UTC. Adjust into the time zone by which you want to perceive the date according to the wall-clock time used by the people of a certain region. For any given moment, the date varies around the world by zone. It might be “tomorrow” in Asia/Kolkata India while still “yesterday” in America/Montreal Canada.

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

Extract the date-only, as that is what you want to focus on.

LocalDate ld = zdt.toLocalDate() ; 

Use the various plus/minus methods to add or subtract days, weeks, months, years, or a combination of those (Period).

LocalDate dayAfter = ld.plusDays( 1 ) ;

Or…

Period p = Period.parse( "P3M7D" ) ;
LocalDate later = ld.plus( p ) ;

For a database type akin to SQL-standard TIMESTAMP WITHOUT TIME ZONE, retrieve as a LocalDateTime. Skip the part above with time zone. Go directly to extracting the LocalDate. Proceed with your addition/subtraction.

myResultSet.getObject( … , LocalDateTime.class )
.toLocalDate()
.plusDays( 1 ) 
.toString()  

To generate a String representing the date value in standard ISO 8601 format, call LocalDate::toString(). For other formats, use DateTimeFormatter. Search Stack Overflow to learn more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154