假设,我有时间作为2018-08-08T00: 00: 00
并且有iso代码IN
。现在我想做的是,我想根据该国家代码将时间转换为utc服务器时间。为此,我编写了以下方法:
try {
Date dateFrom = new SimpleDateFormat(format, Locale.ENGLISH).parse(strFrom);
long millisecondsFrom = dateFrom.getTime();
Date dateTo = new SimpleDateFormat(format, Locale.ENGLISH).parse(strTo);
long millisecondsTo = dateTo.getTime();
long milliseconds = millisecondsTo - millisecondsFrom;
int msDiff = (int)milliseconds/1000;
return msDiff;
}catch (Exception e){
e.printStackTrace();
}
以某种方式,这不能完全满足我的目的,那么我有什么办法可以做到这一点?
答案 0 :(得分:0)
尝试:
final DateFormat df = new SimpleDateFormat(pattern, Locale.ENGLISH);
df.setTimeZone(timeZone); // Timezone is the Timezone the of the time you want to parse
try {
Date d = df.parse(date);
// TODO: Do something with the date
} catch (ParseException e) {
e.printStackTrace();
}