简体   繁体   English

如何在 Java 中使用 TimeZone 获取当前时间戳

[英]How to get current timestamp with TimeZone in Java

我想通过在Android中考虑Java中的TimeZone来获得timeStamp ,我尝试了很多方法,但仍然找不到解决方案。

tl;dr tl;博士

Use the ZoneId & ZonedDateTime classes found in the java.time package.使用java.time包中的ZoneIdZonedDateTime类。

ZonedDateTime.now()  // Implicitly applies the JVM’s current default time zone.

Better to specify your desired/expected time zone explicitly.最好明确指定您想要/预期的时区。

ZonedDateTime.now(
    ZoneId.of( "Africa/Tunis" ) 
)

java.time时间

The modern approach uses the java.time classes that years ago supplanted the terrible legacy classes such as Timestamp , Calendar , Date , TimeZone , and SimpleDateFormat .现代方法使用java.time类,这些类多年前取代了可怕的遗留类,例如TimestampCalendarDateTimeZoneSimpleDateFormat

Specify a proper time zone name in the format of Continent/Region , such as America/Montreal , Africa/Casablanca , or Pacific/Auckland .Continent/Region格式指定正确的时区名称,例如America/MontrealAfrica/CasablancaPacific/Auckland Never use the 2-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).永远不要使用ESTIST等 2-4 个字母的缩写,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "America/Montreal" ) ;  

Capture the current moment as seen through the wall-clock time used by the people of a particular region, that time zone named above.通过特定地区(上面命名的时区)的人们使用的挂钟时间捕捉当前时刻。

ZonedDateTime zdt = ZonedDateTime.now( z ) ;

Java 中用于日期时间处理的遗留类和现代类表


About java.time关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于 Java 8 及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .这些类取代麻烦的老传统日期时间类,如java.util.DateCalendar ,和SimpleDateFormat

To learn more, see the Oracle Tutorial .要了解更多信息,请参阅Oracle 教程 And search Stack Overflow for many examples and explanations.并在 Stack Overflow 上搜索许多示例和解释。 Specification is JSR 310 .规范是JSR 310

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.现在处于维护模式Joda-Time项目建议迁移到java.time类。

You may exchange java.time objects directly with your database.您可以直接与您的数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later.使用符合JDBC 4.2或更高版本的JDBC 驱动程序 No need for strings, no need for java.sql.* classes.不需要字符串,不需要java.sql.*类。

Where to obtain the java.time classes?从哪里获得 java.time 类?

You can get the timestamp with time zone with this method :您可以使用此方法获取带时区的时间戳:

public static long getTimestampWithGMT() {
    long timestamp = System.currentTimeMillis() / 1000;
    int offset = (TimeZone.getDefault().getRawOffset() + TimeZone.getDefault().getDSTSavings()) / 1000;

    return timestamp + offset;
}

You can use Calendar class to get current timestamp.您可以使用 Calendar 类来获取当前时间戳。 Then you can format this timestamp to display Date, Day, Month, Year, Time etc.然后您可以格式化此时间戳以显示日期、日、月、年、时间等。

Calendar c = Calendar.getInstance(); 

Check out this link: Calendar class in java with examples (GeeksForGeeks)查看此链接: 带有示例的 Java 中的日历类 (GeeksForGeeks)

You can use Calendar.getInstance().getTimeInMillis()?您可以使用 Calendar.getInstance().getTimeInMillis()? It default to device timezone, or if you want to change timeZone you can do something like the following它默认为设备时区,或者如果您想更改时区,您可以执行以下操作

Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
calendar.getTimeInMillis();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM