[英]how can i get Calendar.getInstance() based on Turkey timezone
I am developing an android application.我正在开发一个 android 应用程序。 What should I do to get the current time based on Turkish local time?我应该怎么做才能根据土耳其当地时间获取当前时间?
val now = Calendar.getInstance(TimeZone.getTimeZone("GMT+3"))
the result is:结果是:
2020-08-25T18:16:30
but this website result is different: https://www.timeanddate.com/worldclock/turkey/istanbul但是这个网站的结果是不同的: https : //www.timeanddate.com/worldclock/turkey/istanbul
2020-08-25T16:46:30
The output is printed using the following code snippet:使用以下代码片段打印输出:
DebugHelper.info("one now => ${now.getDisplayMonthNameDayTime(FULL_PATTERN)}")
Extention Function:扩展功能:
const val FULL_PATTERN = "yyyy-MM-dd'T'HH:mm:ss"
fun Calendar.getDisplayMonthNameDayTime(pattern: String = "dd MMM , HH:mm ") = SimpleDateFormat(
pattern,
Locale.getDefault()
).format(time).toUpperCase(Locale.getDefault())
If you want to use a modern and less troublesome API, then use java.time
, especially java.time.ZonedDateTime
.如果您想使用现代且不那么麻烦的 API,请使用java.time
,尤其是java.time.ZonedDateTime
。
See this minimal example:请参阅此最小示例:
public static void main(String[] args) {
ZonedDateTime istanbulDateTime = ZonedDateTime.now(ZoneId.of("Europe/Istanbul"));
System.out.println(istanbulDateTime);
}
Output (some seconds ago):输出(几秒钟前):
2020-08-25T16:32:56.069+03:00[Europe/Istanbul]
As an alternative, there is ZoneId.of("Asia/Istanbul")
, too, but the values only differ in the description of the continent.作为替代方案,也有ZoneId.of("Asia/Istanbul")
,但值仅在大陆的描述中有所不同。 Just a matter of taste, I think.我想只是口味问题。
EDIT编辑
After your edit I realized you aren't relying on a time zone but rather an offset.在您编辑之后,我意识到您不依赖于时区,而是依赖于偏移量。 That brings in another alternative from java.time
, that is java.time.OffsetDateTime
.这带来了java.time
另一个替代方案,即java.time.OffsetDateTime
。
For the sake of completeness, here's a possible solution which only takes a ZoneOffset
without the need to provide a zone by String
:为了完整起见,这里有一个可能的解决方案,它只需要一个ZoneOffset
而不需要通过String
提供一个区域:
public static void main(String[] args) {
OffsetDateTime utcPlusThreeDateTime = OffsetDateTime.now(ZoneOffset.ofHours(3));
System.out.println(utcPlusThreeDateTime);
}
which output (a few seconds ago)哪个输出(几秒钟前)
2020-08-25T16:53:14.490+03:00
... and yes, since there's API desugaring in Android , you can use it with a suitable gradle plugin. ...是的,由于Android 中有API 脱糖,您可以将它与合适的 gradle 插件一起使用。
Use ZoneId.of("Asia/Istanbul")
and a ZonedDateTime
from java.time, the modern Java date and time API, as demonstrated in the answer by deHaar.使用ZoneId.of("Asia/Istanbul")
和ZonedDateTime
的 ZonedDateTime,现代 Java 日期和时间 API,如 deHaar 的回答所示。
You problem is in this line:你的问题在这一行:
).format(time).toUpperCase(Locale.getDefault())
time
gives you the time of the Calendar
object as a Date
(another poorly designed and long outdated class that we should not use anymore). time
为您提供Calendar
对象的时间作为Date
(另一个设计不佳且过时的类,我们不应再使用它)。 A Date
hasn't got any time zone, so the time zone and offset information from the Calendar
is lost. Date
没有任何时区,因此Calendar
的时区和偏移量信息丢失。 So when you format this Date
, you are using the time zone of the SimpleDateFormat
, not the time zone of the Calendar
.因此,当您格式化此Date
,您使用的是SimpleDateFormat
的时区,而不是Calendar
的时区。
Your Calendar
's time zone was GMT+03:00
alright.您Calendar
的时区是GMT+03:00
好吧。 As others have mentioned, you should prefer Europe/Istanbul or Asia/Istanbul, though.正如其他人所说,您应该更喜欢欧洲/伊斯坦布尔或亚洲/伊斯坦布尔。
Turkey have an issue with real time in java...土耳其在 Java 中存在实时问题...
you need to do you own hack with GMT+03你需要用 GMT+03 做你自己的 hack
TimeZone.getTimeZone("GMT+03")
code:代码:
TimeZone timeZone = TimeZone.getTimeZone("GMT+03");
Calendar calendar = Calendar.getInstance(timeZone);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.