请建议一种在 EST 中打印日期的方法。
public Date convertToEST(Date date)
{
// some code here
}
Run Code Online (Sandbox Code Playgroud)
如果我在 IST 中传入一个日期,则该方法应该在 EST 中返回该日期。
myJavaUtilDate // Avoid the troublesome legacy date-time classes. \n .toInstant() // Convert from legacy class to modern class `Instant`, always in UTC by definition.\n .atZone( // Adjust into a time zone. Same moment, different wall-clock time.\n ZoneId.of( \xe2\x80\x9cAmerica/New_York\xe2\x80\x9d ) // Never use \xe2\x80\x9cEST\xe2\x80\x9d/\xe2\x80\x9cIST\xe2\x80\x9d pseudo-zones. Use proper continent/region names. \n ) // Yields a `ZonedDateTime` object. \nRun Code Online (Sandbox Code Playgroud)\nInstant,不使用Date其他答案使用麻烦的旧遗留日期时间类。这些类在几年前就被java.time类取代了。忘记吧java.util.Date,一个设计糟糕、混乱不堪的类。
\n\n如果我在 IST 中传递一个日期,
\n
不要\xe2\x80\x99t!
\n学习思考、工作、交换和记录 UTC 值。当程序员或管理员工作时,请忘记自己狭隘的时区。将 UTC 视为唯一的真实时间,所有其他区域都只不过是变体。
\njava.time 中的构建块是Instant类。此类代表一个时刻,即 UTC 时间轴上的一个点。内部表示为自 1970 年开始(UTC)以来的秒数加纳秒数。
Instant instant = Instant.now() ; // Capture current moment in UTC.\nRun Code Online (Sandbox Code Playgroud)\n这样的Instant一般来说,仅当您的业务逻辑或用户界面需要时才调整时区。
切勿使用媒体中常见的 3-4 个字母的伪区域,例如 \xe2\x80\x9cIST\xe2\x80\x9d 或 \xe2\x80\x9cEST\xe2\x80\x9d。相反,请使用正确的时区名称定义,continent/region例如Europe/Paris或Asia/Kolkata或America/New_York。
ZoneId z = ZoneId.of( \xe2\x80\x9cAmerica/New_York\xe2\x80\x9d ) ;\nZonedDateTime zdt = instant.atZone( z ) ;\nRun Code Online (Sandbox Code Playgroud)\n如果必须序列化为文本以交换日期时间值,请使用标准 ISO 8601 格式。toString这些在 java.time和中默认使用parse。
要生成其他格式的字符串,请参阅DateTimeFormatter。
与尚未更新到java.time 的旧代码进行互操作的旧代码进行互操作,请调用添加到旧类中的新转换方法。
\nADate代表 UTC 中的一个时刻,因此它直接映射到Instant。
Instant instant = myJavaUtilDate.toInstant() ;\nRun Code Online (Sandbox Code Playgroud)\n和\xe2\x80\xa6
\njava.util.Date myJavaUtilDate = java.util.Date.from( instant ) ;\nRun Code Online (Sandbox Code Playgroud)\n遗产GregorianCalendar映射到现代ZonedDateTime。
java.time框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如java.util.Date, Calendar, &SimpleDateFormat。
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310。
\nJoda -Time项目目前处于维护模式,建议迁移到java.time类。
\n您可以直接与数据库交换java.time对象。使用与JDBC 4.2或更高版本兼容的JDBC 驱动程序。不需要字符串,不需要类。Hibernate 5 和 JPA 2.2 支持java.timejava.sql.*。
从哪里获取 java.time 类?
\n您需要以下内容
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));
Run Code Online (Sandbox Code Playgroud)
要使方法返回一个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)));
}
Run Code Online (Sandbox Code Playgroud)
Javadoc- DateFormat.format,DateFormat.parse
更改java中的时区
public class TimeZoneSample {
public static void main(String[] args) throws ParseException {
// I am in IST time Zone (Its ID is Asia/Calcutta or ITS)
System.out.println(TimeZone.getDefault());
// I get Indian Time printed
System.out.println(new Date());
System.out.println("-------------------");
// I am setting the time zone to China
TimeZone.setDefault(TimeZone.getTimeZone("CTT"));
// Now my default time zone is in China
System.out.println(TimeZone.getDefault());
// I get Chian Time printed
System.out.println(new Date());
System.out.println("-------------------");
// I am setting the time zone to EST
TimeZone.setDefault(TimeZone.getTimeZone("EST"));
// Now my default time zone is in EST
System.out.println(TimeZone.getDefault());
// I get Eastern Time printed
System.out.println(new Date());
}
}
Run Code Online (Sandbox Code Playgroud)
控制台输出
sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]
Wed Dec 26 10:22:25 IST 2012
-------------------
sun.util.calendar.ZoneInfo[id="CTT",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null]
Wed Dec 26 12:52:25 CST 2012
-------------------
sun.util.calendar.ZoneInfo[id="EST",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
Tue Dec 25 23:52:25 EST 2012
Run Code Online (Sandbox Code Playgroud)