[英]How to get current timestamp with TimeZone in Java
我想通过在Android中考虑Java中的TimeZone
来获得timeStamp
,我尝试了很多方法,但仍然找不到解决方案。
Use the ZoneId
& ZonedDateTime
classes found in the java.time package.使用java.time包中的
ZoneId
和ZonedDateTime
类。
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" )
)
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类,这些类多年前取代了可怕的遗留类,例如
Timestamp
、 Calendar
、 Date
、 TimeZone
和SimpleDateFormat
。
Specify a proper time zone name in the format of Continent/Region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
.以
Continent/Region
格式指定正确的时区名称,例如America/Montreal
、 Africa/Casablanca
或Pacific/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(!).永远不要使用
EST
或IST
等 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 ) ;
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.Date
, Calendar
,和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.