简体   繁体   中英

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

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

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.

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.

Links

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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