[英]How can I convert a string into a DateTime for a specific timezone, preserving the date exactly is it comes for that timezone?
我建立了一个函数,该函数接收字符串格式的日期和时区ID,并以时区时间转换该日期:
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;
}
我不喜欢DateTimeZone.setDefault
的部分; 有什么办法可以做到这一点吗?
只需设置DateTimeFormatter
本身的区域。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.