-1

This is my code for time(to select a time)..This displays a time in 24 hour format...i need to change that in 12 hours...Where i need to add the code...What code i have to add...Please help me to find out the code

this is my code for time

seekBar_startTime.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
                start_Time = String.valueOf(progresValue);
                textView_startTime.setText("Event Start Time :" + progresValue);
            }
        });

        seekBar_endTime.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {

                end_Time = String.valueOf(progresValue);
                textView_endTime.setText("Event End Time :" + progresValue);
            }
        });
Minzkraut
  • 2,149
  • 25
  • 31
Sathya
  • 77
  • 10

2 Answers2

0

Try this

TimeZone utc = TimeZone.getTimeZone("etc/UTC");
DateFormat inputFormat = new SimpleDateFormat("dd MMM, yyyy HH:mm",
                                              Locale.US);
inputFormat.setTimeZone(utc);
DateFormat outputFormat = new SimpleDateFormat("dd MMM, yyyy hh:mm aa",
                                              Locale.US);
outputFormat.setTimeZone(utc);

Date date = inputFormat.parse(input);
String output = outputFormat.format(date);

visit here for more help how to convert date and time to 12 hour format

Community
  • 1
  • 1
Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53
0

If the progresValue variable is integer in the range of 1-24 representing the hour part of time...this would do:

final String time;
if(progressValue > 12){
    time = string.format("%d:00 %s", progressValue - 12, "PM");
}
else if(progressValue == 12){
    time = "12:00 PM";
}
else{
    time = string.format("%d:00 %s", progressValue , "AM");
}
Leo
  • 14,625
  • 2
  • 37
  • 55