我需要创建一个具有澳大利亚时区的java.util.Date对象。这个对象是在下游组件中使用的标签库所必需的(所以我被Date卡住了)。将日历转换为java.util.Date的问题
这是我尝试:
TimeZone timeZone = TimeZone.getTimeZone("Australia/Sydney");
GregorianCalendar defaultDate = new GregorianCalendar(timeZone);
Date date = defaultDate.getTime();
然而, “日期” 总是返回当前的本地时间(在我的情况,ET)。我在这里做错了什么?是否有可能使用不同的时区设置Date对象?
更新:
感谢您的回应!如果我想输出格式化的日期作为字符串,但这不工作,如果我想返回一个日期对象。例如:
Date d = new Date();
DateFormat df = new SimpleDateFormat();
df.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
String formattedDate = df.format(d); // returns Sydney date/time
Date myDate = df.parse(formattedDate); // returns local time(ET)
我想我最终将会重新调整我们最新的taglib。
[如何使用Java来处理日历时区?(http://stackoverflow.com/questions/230126/how-to-handle-calendar-timezones-using-java) – duffymo 2010-11-09 02:17:01