1

When I am selecting time 06.30 Am or 06.30 (24 hour format), I get out put as 18.30. And when I am selecting time 18.00 or 06.00PM , I getting 06.00 as output. My code is this...

private void handleTimeButton() {
    final Calendar calendar = Calendar.getInstance();
    int HOUR = calendar.get(Calendar.HOUR);
    int MINUTE = calendar.get(Calendar.MINUTE);

    TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker timePicker, int hour, int minute) {
            Calendar calendar1 = Calendar.getInstance();
            calendar1.set(Calendar.HOUR, hour);
            calendar1.set(Calendar.MINUTE, minute);
            String text = (String) DateFormat.format("HH:mm", calendar1);
            etTime.setText(text); //this is my output
        }
    }, HOUR, MINUTE, true); // tried both true and false
    timePickerDialog.show();
}

I want my output to be in 24 hours format.

0009laH
  • 1,960
  • 13
  • 27
Suthar
  • 91
  • 2
  • 8
  • What I get is that i am recieving time correct but while formatting is getting currupted so i used manual formater which solve this problem `boolean isPM = (hour >= 12); String text = String.format("%02d:%02d %s", (hour == 12 || hour == 0) ? 12 : hour % 12, minute, isPM ? "PM" : "AM"); etTime.setText(text); ` from here ...[Link](https://stackoverflow.com/a/33799417/11742376) – Suthar Jul 22 '21 at 14:39
  • 2
    FYI, you are using terrible date-date classes that were supplanted years ago by the modern *java.time* classes defined in JSR 310. – Basil Bourque Jul 22 '21 at 17:13

2 Answers2

4

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice from the home page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API: Create a LocalTime with hour and minute, and get the value of LocalTime#toString to be set to etTime.

Demo:

import java.time.LocalTime;

public class Main {
    public static void main(String[] args) {
        // Test
        String text = getTimeString(6, 30);
        System.out.println(text);

        text = getTimeString(18, 30);
        System.out.println(text);
    }

    static String getTimeString(int hour, int minute) {
        return LocalTime.of(hour, minute).toString();
    }
}

Output:

06:30
18:30

ONLINE DEMO

The modern Date-Time API is based on ISO 8601 and thus the LocalTime#toString returns the string in ISO 8601 format (which is also your desired format).

Learn more about the modern Date-Time API from Trail: Date Time.

Just for the sake of completeness:

Just for the sake of completeness, given below is the solution using the legacy API:

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {
    public static void main(String[] args) {
        // Test
        String text = getTimeString(6, 30);
        System.out.println(text);

        text = getTimeString(18, 30);
        System.out.println(text);
    }

    static String getTimeString(int hour, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);

        return new SimpleDateFormat("HH:mm").format(calendar.getTime());
    }
}

Output:

06:30
18:30

ONLINE DEMO


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

You can do either this,

` val hour = calendar.get(Calendar.HOUR)
    val minute = calendar.get(Calendar.MINUTE)
    val timePickerDialog = TimePickerDialog(this@MainActivity, this, hour, minute,
            DateFormatis24HourFormat(this))
    timePickerDialog.show()`

or

You can refer here for your AM PM confusions

24-hour format to 12-hour format

12 hour format timepicker android

How to set Time with the help of Timepicker? with 12 hour format

Aasima Jamal
  • 119
  • 7
  • 1
    I think this is what I have done. I think I don't have any AM PM confusion. – Suthar Jul 22 '21 at 13:53
  • I think in 24 hour format it should return the same value. It's not returning what is suppose to be ! I am not getting where is the mistake? – Suthar Jul 22 '21 at 13:56
  • You are just repeating the error from the question. Don’t worry, the `Calendar` class is so confusing and poorly designed, and this error (as many other errors) has been made a lot of times before. Or on the other hand, do worry, you are using a class, `Calendar`, that no one should be using anymore. – Ole V.V. Jul 22 '21 at 17:24
  • See ...You had created instance of Calender class and assigned to a variable, then created another instance assigned to another because since it is unaccessible??? why to get More complications to have the timePicker dialog, Do prefer the simplest way as there are many more examples. – Aasima Jamal Jul 23 '21 at 05:27