如何在java中以时区格式将字符串转换为日期。我正在尝试将字符串转换为美国时区格式的日期,但它始终采用IST格式
TimeZone tz = TimeZone.getTimeZone("US/Alaska");
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
String date="Sun, 9 Mar 2014 02:00:00 EST";
Date d = null;
try {
d = sdf.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("date is"+d);
boolean inDs = tz.inDaylightTime(d);
System.out.println("inDs"+inDs);
打印日期Mar 09 12:30:00 IST 2014
答案 0 :(得分:0)
使用日历而不是日期。无论如何,将日期转换为日历并返回日期的以下解决方案效率不高。如果你可以单独获得日,月,年等,最好使用cal.set()
TimeZone tz = TimeZone.getTimeZone("US/Alaska");
SimpleDateFormat sdf = new SimpleDateFormat(
"EEE, d MMM yyyy HH:mm:ss z");
String date = "Sun, 9 Mar 2014 02:00:00 EST";
Date d = null;
try {
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(date));
cal.setTimeZone(tz);
System.out.println("date is " + cal.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}