-1

getting wrong results after converting 12hrs time format to utc.

 String inputPa = "hh:mm a";
            String OutPa = "yyyy-MM-dd'T'HH:mm:ss'Z'";
            SimpleDateFormat inputPatter = new SimpleDateFormat(inputPa);
            SimpleDateFormat outputPatter = new SimpleDateFormat(OutPa);

            Date date1 = null;
            String str1 = null;

            try {
                date1 = inputPatter.parse(txtStartTime.getText().toString());
                str1 = outputPatter.format(date1);
                Log.d("mycheck", str1);

            } catch (ParseException e) {
                e.printStackTrace();
            }
  • 2
    You need to post the error you are getting – Lazycoder-007 May 27 '20 at 05:45
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, 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. – Ole V.V. May 28 '20 at 18:58
  • What is your expected result given that the input doesn’t seem to have a date? And how does your observed result differ? – Ole V.V. May 28 '20 at 19:00

2 Answers2

1

java.time and ThreeTenABP

I assumed you wanted today’s date in your time zone.

    String inputPa = "hh:mm a";
    DateTimeFormatter timeFormatter12Hours = DateTimeFormatter.ofPattern(inputPa, Locale.ENGLISH);
    ZoneId zone = ZoneId.of("America/Detroit");

    String timeString = "11:34 AM";

    LocalTime time = LocalTime.parse(timeString, timeFormatter12Hours);
    Instant inst = LocalDate.now(zone).atTime(time).atZone(zone).toInstant();

    System.out.println(inst);

Output from this example is:

2020-05-30T15:34:00Z

I would not bother converting the Instant to a string explicitly. It prints in UTC in your desired format when you print it, thus implicitly invoking its toString method (the format is ISO 8601, the international standard).

Please fill in your desired time zone. To rely on the device’ time zone setting set zone to ZoneId.systemDefault().

I am of course happy to use java.time, the modern Java date and time API. You can do that on your Android version too, see the details below.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both 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 non-Android 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
0
public tring getCurrentUTC(Date time) {
    SimpleDateFormat outputFmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    outputFmt.setTimeZone(TimeZone.getTimeZone("UTC"));
    return outputFmt.format(time);
}

You can use this code to get current UTC time.

For you question, you just need convert the time string to the Date format.

youDaily
  • 1,372
  • 13
  • 21