flavienlaurent/datetimepicker

How to get timePicker results as American standard time?

kkl260 opened this issue · 6 comments

Sorry if this is a stupid question but I can't figure out how to get the timePicker results to show as 7:00 PM instead of 19:00

can anyone help me out here?

According to documentation:
public static com.sleepbot.datetimepicker.time.TimePickerDialog newInstance(com.sleepbot.datetimepicker.time.TimePickerDialog.OnTimeSetListener callback, int hourOfDay, int minute, boolean is24HourMode, boolean vibrate) { /* compiled code */ }

For AM/PM use:
final TimePickerDialog timePickerDialog = TimePickerDialog.newInstance(this, calendar.get(Calendar.HOUR_OF_DAY) ,calendar.get(Calendar.MINUTE), false, false);

For 24 hour clock:
final TimePickerDialog timePickerDialog = TimePickerDialog.newInstance(this, calendar.get(Calendar.HOUR_OF_DAY) ,calendar.get(Calendar.MINUTE), true, false);

This worked for me!

I can get the timePicker to show in AM/PM mode, my issue is how do I get the results to show it in that mode?

For example, using the sample code, the timePicker shows in AM/PM mode, but the results (in the toast dialog, and if I put in a setText for the button) come back in 24 hour mode. So if I select 7:00 PM in the dialog, the results will still show 19:00

If timeIs24h && hourInt > 12 then hourInt -= 12

I can elaborate when I'm back at my computer. I'm writing from phone

Please elaborate when you have the time, I'm really new to programing. Really Appreciate your help!

Here is a function that I wrote about year and a half ago when I had very little Android development experience. Notice that it relies on some variables that are set in my class such as this.hourInt, this.isTime24Hours etc. But it should give you an idea how to format a string manually. Another option is utilizing class SimpleDateFormat but to make it work you may need to use timestamp.

Here is manual conversion:

    public String getTimeString() {

        String timeString, ampm;
        int tempHourInt = this.hourInt; //create a copy so that I don't change the original hour stored in object

        //create time
        if (this.isTime24Hours) {
            timeString = (tempHourInt < 10 ? "0" + tempHourInt : tempHourInt) + ":" + (this.minuteInt < 10 ? "0" + this.minuteInt : this.minuteInt);
        }
        else {
            //check if 12 or greater
            if (tempHourInt >= 12) {
                //if greater than 12 then subtract 12 (will not subtract of hour is something like 12:34 PM)
                if (tempHourInt > 12) {
                    tempHourInt -= 12;
                }
                ampm = "PM";
            }
            else {
                if (tempHourInt == 0) {
                    tempHourInt = 12;
                }
                ampm = "AM";
            }

            timeString = (tempHourInt < 10 ? "0" + tempHourInt : tempHourInt) + ":" + (this.minuteInt < 10 ? "0" + this.minuteInt : this.minuteInt) + " " + ampm;
        }

        return timeString;
    }

HERE IS BETTER CONVERSION that uses timestamp

    public String getTimeString() {
        Date d = new Date();
        SimpleDateFormat timeFormatter;
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);

        if (this.isTime24Hours) {
            timeFormatter = new SimpleDateFormat("HH:mm", Locale.getDefault());     //23:24
        }
        else {
            timeFormatter = new SimpleDateFormat("h:mm a", Locale.getDefault());    //11:23 AM
        }

        c.set(Calendar.HOUR_OF_DAY, this.hourInt);
        c.set(Calendar.MINUTE, this.minuteInt);

        long newTimestamp = c.getTimeInMillis();

        d.setTime(newTimestamp);

        String timeString = timeFormatter.format(d);
        return timeString;
    }

PS. I just gave away something that took me weeks to figure out and optimize. All I am asking for in return is that you help someone else once you learn more about programming!

Really appreciate your help. I'm not sure if this was the right approach but I ended up using a bunch of if/else statements to give me what I was looking for.

For example something like this:
if (!DateFormat.is24HourFormat(getActivity())){
if (hourOfDay> 12){
if (minute<= 9){
TimeButton.setText(hourOfDay-12 + ":" + "0" + minute + " pm");
}
else {
TimeButton.setText(hourOfDay-12 + ":" + minute + " pm");
}
} etc.....

I'm not sure if I'll regret doing it this way later on in development because I'm going to need to save the results in a backend.