如何在 EST 中获取日期

but*_*ski 5 java date

请建议一种在 EST 中打印日期的方法。

public Date convertToEST(Date date)
{
     // some code here
}
Run Code Online (Sandbox Code Playgroud)

如果我在 IST 中传入一个日期,则该方法应该在 EST 中返回该日期。

Bas*_*que 7

太长了;博士

\n
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. \n
Run Code Online (Sandbox Code Playgroud)\n

使用Instant,不使用Date

\n

其他答案使用麻烦的旧遗留日期时间类。这些类在几年前就被java.time类取代了。忘记吧java.util.Date,一个设计糟糕、混乱不堪的类。

\n
\n

如果我在 IST 中传递一个日期,

\n
\n

不要\xe2\x80\x99t!

\n

学习思考、工作、交换和记录 UTC 值。当程序员或管理员工作时,请忘记自己狭隘的时区。将 UTC 视为唯一的真实时间,所有其他区域都只不过是变体。

\n

java.time 中的构建块是Instant类。此类代表一个时刻,即 UTC 时间轴上的一个点。内部表示为自 1970 年开始(UTC)以来的秒数加纳秒数。

\n
Instant instant = Instant.now() ;  // Capture current moment in UTC.\n
Run Code Online (Sandbox Code Playgroud)\n

这样的Instant一般来说,仅当您的业务逻辑或用户界面需要时才调整时区。

\n

切勿使用媒体中常见的 3-4 个字母的伪区域,例如 \xe2\x80\x9cIST\xe2\x80\x9d 或 \xe2\x80\x9cEST\xe2\x80\x9d。相反,请使用正确的时区名称定义,continent/region例如Europe/ParisAsia/KolkataAmerica/New_York

\n
ZoneId z = ZoneId.of( \xe2\x80\x9cAmerica/New_York\xe2\x80\x9d ) ;\nZonedDateTime zdt = instant.atZone( z ) ;\n
Run Code Online (Sandbox Code Playgroud)\n

如果必须序列化为文本以交换日期时间值,请使用标准 ISO 8601 格式。toString这些在 java.time和中默认使用parse

\n

要生成其他格式的字符串,请参阅DateTimeFormatter

\n

与尚未更新到java.time 的旧代码进行互操作的旧代码进行互操作,请调用添加到旧类中的新转换方法。

\n

ADate代表 UTC 中的一个时刻,因此它直接映射到Instant

\n
Instant instant = myJavaUtilDate.toInstant() ;\n
Run Code Online (Sandbox Code Playgroud)\n

和\xe2\x80\xa6

\n
java.util.Date myJavaUtilDate = java.util.Date.from( instant ) ;\n
Run Code Online (Sandbox Code Playgroud)\n

遗产GregorianCalendar映射到现代ZonedDateTime

\n
\n

关于java.time

\n

java.time框架内置于 Java 8 及更高版本中这些类取代了麻烦的旧遗留日期时间类,例如java.util.Date, Calendar, &SimpleDateFormat

\n

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

\n

Joda -Time项目目前处于维护模式,建议迁移到java.time类。

\n

您可以直接与数据库交换java.time对象。使用与JDBC 4.2或更高版本兼容的JDBC 驱动程序。不需要字符串,不需要类。Hibernate 5 和 JPA 2.2 支持java.timejava.sql.*

\n

从哪里获取 java.time 类?

\n\n

哪个 java.time 库与哪个版本的 Java 或 Android 一起使用的表

\n

  • @Samir 因为它们不是真实的时区名称。而且它们没有标准化。它们并不是唯一的:“IST”是指“爱尔兰标准时间”还是“印度标准时间”?是的,两者都有。当夏令时 (DST) 生效时,两者均无效。请改用“欧洲/都柏林”或“亚洲/加尔各答”。 (2认同)

mtk*_*mtk 6

您需要以下内容

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.formatDateFormat.parse

  • 此代码不会返回正确的 EST 时间。我尝试过这个 (3认同)
  • 在我的控制台中我仍然得到它作为 IST 伙计,问题是什么? (2认同)

om3*_*39a 2

更改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)