I have built a function that receives a date in string format and a timezone id, and transform that date in the timezone time:
public static DateTime transformSrcTimeInTzTime(String timeAsString, String timezoneId){
DateTimeZone zone = DateTimeZone.forID(timezoneId);
DateTimeZone.setDefault(zone);
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime tzDate = formatter.parseDateTime(timeAsString);
System.out.println("++++ timeAsString: " + tzDate.toString(formatter));
DateTimeZone.setDefault(DateTimeZone.forID(Constants.SERVER_TIMEZONE));
return tzDate;
}
I don't like the part with DateTimeZone.setDefault
; is there any way to achieve this differently?
Just set the zone of the DateTimeFormatter
itself.
DateTimeZone zone = DateTimeZone.forID(timezoneId);
DateTimeFormatter formatter = DateTimeFormat
.forPattern("dd/MM/yyyy HH:mm:ss")
.withZone(zone);
DateTime tzDate = formatter.parseDateTime(timeAsString).withZone(zone);
return tzDate;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.