Sure, here’s the translation: 如何根据土耳其时区获取Calendar.getInstance()?

huangapple go评论65阅读模式
英文:

how can i get Calendar.getInstance() based on Turkey timezone

问题

我正在开发一个Android应用程序。
要根据土耳其当地时间获取当前时间,我应该怎么做?

val now = Calendar.getInstance(TimeZone.getTimeZone("GMT+3"))

结果为:

2020-08-25T18:16:30

但是该网站的结果不同:
https://www.timeanddate.com/worldclock/turkey/istanbul

2020-08-25T16:46:30

输出是使用以下代码段打印的:

DebugHelper.info("one now => ${now.getDisplayMonthNameDayTime(FULL_PATTERN)}")

扩展函数:

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())
英文:

I am developing an android application.
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

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())

答案1

得分: 3

如果您想使用现代且不太麻烦的 API,可以使用 java.time,尤其是 java.time.ZonedDateTime

看一下这个简单的例子:

public static void main(String[] args) {
	ZonedDateTime istanbulDateTime = ZonedDateTime.now(ZoneId.of("Europe/Istanbul"));
	System.out.println(istanbulDateTime);
}

输出(几秒钟前):

2020-08-25T16:32:56.069+03:00[Europe/Istanbul]

作为替代,还有 ZoneId.of("Asia/Istanbul"),但这些值在大陆的描述上有所不同。这只是个人口味的问题,我想。

编辑
在您的编辑之后,我意识到您并不依赖于时区,而是依赖于偏移量。这带来了 java.time 的另一个选择,即 java.time.OffsetDateTime

为了完整起见,这里有一个可能的解决方案,它只使用 ZoneOffset 而不需要通过 String 提供时区:

public static void main(String[] args) {
	OffsetDateTime utcPlusThreeDateTime = OffsetDateTime.now(ZoneOffset.ofHours(3));
	System.out.println(utcPlusThreeDateTime);
}

输出(几秒钟前):

2020-08-25T16:53:14.490+03:00

... 是的,由于在 Android 中存在 API 次级支持,您可以使用适当的 Gradle 插件来使用它。

英文:

If you want to use a modern and less troublesome API, then use java.time, especially 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. 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.

For the sake of completeness, here's a possible solution which only takes a ZoneOffset without the need to provide a zone by 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.

答案2

得分: 1

解决方案

使用 ZoneId.of("Asia/Istanbul") 和来自java.time的ZonedDateTime,这是现代Java日期和时间API的一部分,如deHaar的答案中所示。

问题

您的问题在于这行代码:

).format(time).toUpperCase(Locale.getDefault())

timeCalendar 对象的时间作为 Date(另一个设计不佳且过时的类,我们不应再使用它)提供。Date 没有任何时区信息,因此来自 Calendar 的时区和偏移信息将丢失。因此,当您格式化此 Date 时,您实际上使用的是 SimpleDateFormat 的时区,而不是 Calendar 的时区。

您的 Calendar 的时区是 GMT+03:00。正如其他人提到的,您应该更倾向于使用Europe/Istanbul或Asia/Istanbul。

链接

英文:

The solution

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.

The problem

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). A Date hasn’t got any time zone, so the time zone and offset information from the Calendar is lost. So when you format this Date, you are using the time zone of the SimpleDateFormat, not the time zone of the Calendar.

Your Calendar’s time zone was GMT+03:00 alright. As others have mentioned, you should prefer Europe/Istanbul or Asia/Istanbul, though.

答案3

得分: 0

土耳其在使用Java中的实时操作方面存在问题...

你需要使用GMT+03进行自己的修改

    TimeZone.getTimeZone("GMT+03")

代码:

    TimeZone timeZone = TimeZone.getTimeZone("GMT+03");
    Calendar calendar = Calendar.getInstance(timeZone);
英文:

Turkey have an issue with real time in java...

you need to do you own hack with GMT+03

TimeZone.getTimeZone("GMT+03")

code:

TimeZone timeZone = TimeZone.getTimeZone("GMT+03");
Calendar calendar = Calendar.getInstance(timeZone);

huangapple
  • 本文由 发表于 2020年8月25日 21:18:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63579732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定