While displaying the datetime in specific format is it possible to add the offset value to the hour and date
Example:
Let Say Date Time is 5/12/2023 23:18 PM
is stored without any timezone in DB.
Is it possible to display in particular format (consider PST) as 5/12/2023 10:18 AM
tried below methods
DateTimeFormatter COMMENT_DATE_TIME_FORMATTER_ZONE = DateTimeFormatter.ofPattern("M/d/y hh:mm a zzz");
ZonedDateTime zdt = ZonedDateTime.of(date, ZoneId.of("America/Los_Angeles"));
String zonfs = zdt.format(Constants.COMMENT_DATE_TIME_FORMATTER_ZONE); //5/12/2023 06:50 PM PDT
DateTimeFormatter COMMENT_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("M/d/y hh:mm a"); //5/12/2023 06:50 PM
SimpleDateFormat sdf = new SimpleDateFormat("M/d/y hh:mm a");
String sdfd =sdf.format(GregorianCalendar.from(zdt).getTime()); //5/13/2023 07:20 AM here its increasing the day by 1
Yes, it is possible, try this way:
LocalDateTime dateTime = LocalDateTime.of(2023, 5, 12, 23, 18);
Instant instant = dateTime.toInstant(ZoneOffset.UTC);
ZoneId pstZone = ZoneId.of("America/Los_Angeles");
ZonedDateTime pstDateTime = instant.atZone(pstZone);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/y hh:mm a");
String formattedDateTime = pstDateTime.format(formatter);