请建议一种在 EST 中打印日期的方法。
public Date convertToEST(Date date)
{
// some code here
}
如果我在 IST 中传递一个日期,该方法应该在 EST 中返回该日期。
最佳答案
您需要以下内容
Date date = new Date();
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
// Set the formatter to use a different timezone
formatter.setTimeZone(TimeZone.getTimeZone("EST"));
// Prints the date in the EST timezone
System.out.println(formatter.format(date));
要使方法返回一个 Date
对象,您需要如下所示
public static Date convertToEST(Date date) throws ParseException {
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
formatter.setTimeZone(TimeZone.getTimeZone("EST"));
return formatter.parse((formatter.format(date)));
}
Javadoc- DateFormat.format
, DateFormat.parse
关于java - 如何在 EST 中获取日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14030313/