[英]How to add timezone to the timestamp?
I've a timestamp which print date and time(24 hours format) 我有一个打印日期和时间的时间戳(24小时格式)
String timeStamp = new SimpleDateFormat("ddMMMyyyyHHmm").format(Calendar.getInstance().getTime());
When I print timeStamp the output is 当我打印timeStamp时,输出是
27Feb20180051
but I want something like this 但我想要这样的东西
27Feb20180051EST
Any idea how to proceed? 任何想法如何进行?
Try Following code, 尝试以下代码,
SimpleDateFormat dateFormatter = new SimpleDateFormat("ddMMMyyyyHHmmz");
String timeStamp = dateFormatter.format(Calendar.getInstance().getTime());
You can format your date according to your requirement using patterns. 您可以根据需要使用格式来格式化日期。 More information is included in following link. 以下链接中包含更多信息。 https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
试试下面的代码
String timeStamp = new SimpleDateFormat("ddMMMyyyyHHmmz").format(Calendar.getInstance().getTime());
String timeStamp = ZonedDateTime.now(ZoneId.of("America/Panama"))
.format(DateTimeFormatter.ofPattern("ddMMMuuuuHHmmzzz", Locale.ENGLISH));
System.out.println(timeStamp);
This just printed 这个刚印出来
27Feb20180408EST 2018年2月27日
Messages: 讯息:
ZoneId.systemDefault()
to use the JVM's time zone setting, only please be aware that this may be changed by other parts of your program or other programs running in the same JVM. 您可以指定ZoneId.systemDefault()
以使用JVM的时区设置,仅请注意,这可能会被程序的其他部分或在同一JVM中运行的其他程序更改。 Locale.ENGLISH
, your preference may differ. 我选英语为“ Feb”,并建议使用Locale.ENGLISH
,您的偏好可能有所不同。 Also the time zone abbreviation may be different in different locales. 同样,时区缩写在不同的地区可能会有所不同。 Calendar
, Date
and SimpleDateFormat
are long outdated and the last in particular notoriously troublesome, so I recommend you avoid them. Calendar
, Date
和SimpleDateFormat
早已过时,最后一个特别麻烦的是它,因此我建议您避免使用它们。 The modern API is so much nicer to work with. 现代的API更好用。 2018-02-27T04:08-05:00
. 推荐的格式是ISO 8601,例如2018-02-27T04:08-05:00
。 It's unambiguous, and since it's standard it will be widely understood. 它是明确的,并且由于它是标准的,因此将被广泛理解。 You can generate it easily in Java: OffsetDateTime.now(ZoneId.of("America/Panama")).toString()
. 您可以在Java中轻松生成它: OffsetDateTime.now(ZoneId.of("America/Panama")).toString()
。 Link: Oracle tutorial: Date Time explaining how to use java.time
. 链接: Oracle教程:Date Time解释了如何使用java.time
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.