0

I have written a code to get Dubai current time. But i am getting the system current time in return

below is the code that i have been using

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(Asia/Dubai)).getTime();
cal.getTime();

cal.getTime() always returns system time.

I am currently in India. so this calendar instance always returns Indian time.

How can I get the Dubai current time

jin
  • 53
  • 8
  • 1
    you can refer this https://stackoverflow.com/a/36709870/4679107 – KIRAN CSN Dec 31 '19 at 09:41
  • @KIRAN CSN i want to use calendar instance itself – jin Dec 31 '19 at 09:53
  • This doesn’t come easy and naturally with the `Calendar` class. It’s poorly designed and now long outdated. Consider throwing it away and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. Use for example `ZonedDateTime.now(ZoneId.of("Asia/Dubai"))`. From the linked original questions see [this answer](https://stackoverflow.com/a/42217542/5772882) and [my answer here](https://stackoverflow.com/a/46173735/5772882). – Ole V.V. Jan 08 '20 at 03:26

2 Answers2

0

You can try like this

 Calendar localTime = Calendar.getInstance();



        Calendar time= new GregorianCalendar(TimeZone.getTimeZone("Asia/Dubai"));
        time.setTimeInMillis(localTime.getTimeInMillis());
        int hr = time.get(Calendar.HOUR);
        int min = time.get(Calendar.MINUTE);


        System.out.println(hours);
KIRAN CSN
  • 121
  • 9
  • still it gives Tue Dec 31 15:40:11 GMT+05:30 2019 ,which is my current time by using code - new GregorianCalendar(TimeZone.getTimeZone("Asia/Dubai")).getTime() @KIRAN CSN – jin Dec 31 '19 at 10:10
  • I checked 2 different timezone it was correct – KIRAN CSN Dec 31 '19 at 10:15
-1

Try this

Calendar currentTime = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        currentTime.set(Calendar.ZONE_OFFSET, TimeZone.getTimeZone("UTC").getRawOffset());
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, currentTime.get(Calendar.HOUR_OF_DAY));
        calendar.add(Calendar.HOUR, 4);
        Log.d(TAG, "time: " + calendar.getTimeInMillis());
oli khan
  • 72
  • 7