sgsg
sgsg

Reputation: 7

How to add HH:MM:SS format to the LocalDate in java?

LocalDate beginDate = LocalDate.now()
        .with(ChronoField.DAY_OF_WEEK, 1)
        .atStartOfDay()
        .minusDays(8)
        .toLocalDate();

I am getting the previous week begin date using the above code line. However I want to add HH:MM:SS format to this. I have tried different ways to get this. Tried using LocalDateTime instead of Localdate. But could not find atStartOfDay() method for LocalDateTime. Help me to add HH:MM:SS to beginDate variable

Upvotes: 1

Views: 971

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 339372

tl;dr

LocalDate                                   // Represents a date only, without a time of day, without a time zone or offset. 
.now( ZoneId.of( "Asia/Amman" ) )           // Returns a `LocalDate`. 
.minusDays( 8 )                             // Returns another `LocalDate` object. 
.atStartOfDay( ZoneId.of( "Asia/Amman" ) )  // Returns a `ZonedDateTime`. 
.toString()                                 // Returns a `String` object, with text in standard ISO 8601 format wisely extended to append the name of time zone in brackets. 

See this code run at Ideone.com. Notice that on that date in that zone, the day began at 01:00, not 00:00.

2022-02-22T01:00+03:00[Asia/Amman]

No “format” involved

Date-time objects do not have a “format”. Text has a format. Date-time objects are not text.

LocalDate has no time of day

You said:

add HH:MM:SS format to [a LocalDate object]

A LocalDate represents a date only, without a time of day, without a time zone or offset.

ZonedDateTime

Apparently you want the first moment of the day eight days ago as seen in your locality.

First, specify your desired/expected time zone.

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;

Or use your JVM‘s current default time zone.

ZoneId z = ZoneId.systemDefault() ; 

Capture the current date as seen in that zone.

LocalDate today = LocalDate.now( z ) ;

Go back eight days.

LocalDate eightDaysAgo = today.minusDays( 8 ) ;

If you meant to go back to the previous Monday, use a TemporalAdjuster.

LocalDate previousMonday = today.with( TemporalAdjusters.previous( DayOfWeek.MONDAY ) ) ;

Get the first moment of that day. Pass your time zone.

ZonedDateTime zdt = eightDaysAgo.atStartOfDay( z ) ;

The time-of-day may be 00:00:00, but not necessarily. Some days on some dates in some zones start at another time such as 01:00:00.

All of this has been covered many times already on Stack Overflow. Search to learn more.

Upvotes: 4

Christoph Dahlen
Christoph Dahlen

Reputation: 836

LocalDateTime
 .of(LocalDate.now().with(ChronoField.DAY_OF_WEEK, 1), LocalTime.MIDNIGHT)
 .minusWeeks(1)

Gives you start of last week at midnight (local time).

Upvotes: 0

jwenting
jwenting

Reputation: 5663

What you want is a LocalDateTime, which is a LocalDate with a time component (including timezone).

LocalDate does as it says on the tin, it gives you a date, not a date and time.

Upvotes: 0

Arkady Dymkov
Arkady Dymkov

Reputation: 43

@DateTimeFormat("HH:MM:SS")
@JsonFormat("HH:MM:SS")

Upvotes: -1

Related Questions