0

I had two edit text for user to enter time (24 hours format). How could i get those times from Edittext and deduct 5 hours and 30 minutes from user selected time.

Reason for deducting time : Converting IST to UTC timezone.

My final output should be like - HH:MM:SS:Milleseconds (Ex : 18:25:30:245635).

Thanks in advance.

Eswar
  • 199
  • 1
  • 13
  • This is so easy to do, what have you tried ? – Jon Goodwin Sep 12 '19 at 16:44
  • I've tried using SimpleDate format and Date class for parsing. But the result is not as expected. – Eswar Sep 12 '19 at 16:45
  • 1
    Consider [java.time, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) rather than the poorly designed and old `SimpleDateFormat` and `Date`. Yes, you can use java.time on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Sep 12 '19 at 16:47
  • My concern is that subtracting 5:30 will not always be correct. It has been India’s offset from UTC since WWII, but not always and it may not always stay that way. Can we assume a date? – Ole V.V. Sep 12 '19 at 16:48
  • @OleV.V. - So, what do you suggest me to get desired result. Anything example will be helpful for me. – Eswar Sep 12 '19 at 16:50

1 Answers1

1

java.time

    ZoneId zone = ZoneId.of("Asia/Kolkata");
    String timeString = "18:25:30.245635";
    LocalTime time = LocalTime.parse(timeString);
    LocalTime utcTime = LocalDate.now(zone)        // Today
            .atTime(time)                          // Today at the time in question
            .atZone(zone)                          // Date and time in IST
            .toOffsetDateTime()                    // Convert to OffsetDateTime for the next step
            .withOffsetSameInstant(ZoneOffset.UTC) // Convert to UTC
            .toLocalTime();                        // Extract only the time of day
    System.out.println("UTC time: " + utcTime);

Output is:

UTC time: 12:55:30.245635

I have assumed today’s date. Please check if this assumption is right for you. While the UTC offset for Asia/Kolkata hasn’t changed much recently, other time zones change their offset twice a year.

I changed your time format to have a period (point) rather than a colon between the seconds and fraction of second as is customary. If you do require a colon there, you need a DateTimeFormatter for parsing.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161