I'm working on site that is writing on java 8 and spring 4, we are using java 8 standard time api, now on pages always is shown server time (in user profile is shown date of his registration). With old java time api there was way use SimpleDateFormat, but it doesn't fit now. Is there a way to convert server time to client time in jsp pages or in controllers?
Asked
Active
Viewed 451 times
1 Answers
2
One of the way to handle this is get users timezone String ( GMT , IST etc ) in request ( using AJAX or other relevant technology. )
Now by using new Java 8 Local date API we can achieve this as following:
Step 1 : Get Zone from giving zone query parameter that we got from ajax Using Java 8 ZoneId class. :
ZoneId losAngeles = ZoneId.of("America/Los_Angeles");
Step 2: Get current time based on that timezone using LocalTime API like below:
ZoneId losAngeles = ZoneId.of("America/Los_Angeles");
LocalDateTime currentTimeInLosAngeles = LocalTime.now(losAngeles );
Step 3 : Get DATETIME of particular zone By Using Java 8 ZonedDateTime class
ZoneId losAngeles = ZoneId.of("America/Los_Angeles");
LocalDateTime currentTimeInLosAngeles = LocalTime.now(losAngeles );
ZonedDateTime losAngelsDateTime = ZonedDateTime.of(currentTimeInLosAngeles , losAngeles );

Pramod S. Nikam
- 4,271
- 4
- 38
- 62