+ 1

TimeZone and MilliSeconds

Hi, I'm able to get the current time by using LocalDateTime and I'm also able to get milliseconds using long milliSeconds = Timestamp.valueOf(today).getTime(); The problem is the current time I'm getting is not in my time zone. So how will I able to get my desired time zone? You can check my code. https://code.sololearn.com/c7zueeCtLf2d/?ref=app https://code.sololearn.com/c7zueeCtLf2d/?ref=app

17th Aug 2019, 7:50 PM
Sibusiso Mbambo
3 odpowiedzi
+ 2
bear in mind thar Timestamp can’t handle ZonedDateTime. You have to remove the zone information first: ZoneId timeZone = ZoneId.of("Africa/Johannesburg"); ZonedDateTime today = ZonedDateTime.now(timeZone); LocalDateTime withoutTimezone = today.toLocalDateTime(); long milliSeconds = Timestamp.valueOf(withoutTimezone).getTime();
17th Aug 2019, 8:23 PM
Michael
Michael - avatar
+ 2
try : ZoneId z = ZoneId.systemDefault() ZonedDateTime zdt = ZonedDateTime.now( z ) ;
17th Aug 2019, 8:12 PM
Tom Hammerbacher
Tom Hammerbacher - avatar
+ 1
Thanks, I figured out the answer.
17th Aug 2019, 8:17 PM
Sibusiso Mbambo