I have a method which generates a random date and time.
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.Period;
public String getRandomFormattedDateAndTime() {
LocalDateTime date = generateRandomDateAndTimeInPast();
return formatDate(date);
}
public LocalDateTime generateRandomDateAndTimeInPast() {
return LocalDateTime.now()
.minus(Period.ofDays(
(new Random().nextInt(365 * 2))
));
}
public static String formatDate(LocalDateTime date) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT_PATTERN);
return dateTimeFormatter.format(date);
}
and the printed output is something like "2020-08-07T08:57:09Z"
However, i need to obtain the same value with time zone format 2020-08-07T10:57:09+02:00
which has the +02:00
(my local time).
I have seen several questions and pages like this, but they do not give me a clue.