简体   繁体   English

如何将时区添加到时间戳?

[英]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: 讯息:

  1. Specify explicit time zone. 指定明确的时区。 Please substitute your desired time zone if it didn't happen to be America/Panama. 如果不是美国/巴拿马,请替换您所需的时区。 You may specify 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中运行的其他程序更改。
  2. Specify explicit locale. 指定显式语言环境。 I took 'Feb' for English and suggested Locale.ENGLISH , your preference may differ. 我选英语为“ Feb”,并建议使用Locale.ENGLISH ,您的偏好可能有所不同。 Also the time zone abbreviation may be different in different locales. 同样,时区缩写在不同的地区可能会有所不同。
  3. Use java.time, the modern Java date and time API. 使用java.time,现代的Java日期和时间API。 Calendar , Date and SimpleDateFormat are long outdated and the last in particular notoriously troublesome, so I recommend you avoid them. CalendarDateSimpleDateFormat早已过时,最后一个特别麻烦的是它,因此我建议您避免使用它们。 The modern API is so much nicer to work with. 现代的API更好用。
  4. Avoid the three and four letter time zone abbreviations if you can. 如果可以,请避免使用三个字母和四个字母的时区缩写。 While EST may(!) not be understood as something else than North American Eastern Standard Time (UTC-5), many, possibly most abbreviations are ambiguous and hence prone to be misinterpreted at the other end. 尽管EST(!)可能不被理解为北美东部标准时间(UTC-5),但许多缩写可能是模棱两可的,因此很容易在另一端被误解。 A recommended format is ISO 8601, like 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.

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