2013-11-13 3 views
0

У меня есть приложение для выбора времени, которое показывает 24-часовой формат после выбора времени. Как изменить его на 12-часовой формат? У меня есть пример кода, показанный ниже, может ли кто-нибудь помочь мне с этим? благодаряИзменение выбора времени до 12-часового формата

ява код

package com.botskool.TimePicker; 

import java.util.Calendar; 

import android.app.Activity; 
import android.app.Dialog; 
import android.app.TimePickerDialog; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.TimePicker; 
import android.widget.Toast; 

public class TimePickerActivity extends Activity { 
    /** Private members of the class */ 
    private TextView displayTime; 
    private Button pickTime; 

    private int pHour; 
    private int pMinute; 
    /** This integer will uniquely define the dialog to be used for displaying time picker.*/ 
    static final int TIME_DIALOG_ID = 0; 

    /** Callback received when the user "picks" a time in the dialog */ 
    private TimePickerDialog.OnTimeSetListener mTimeSetListener = 
     new TimePickerDialog.OnTimeSetListener() { 
      public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
       pHour = hourOfDay; 
       pMinute = minute; 
       updateDisplay(); 
       displayToast(); 
      } 
     }; 

    /** Updates the time in the TextView */ 
    private void updateDisplay() { 
     displayTime.setText(
      new StringBuilder() 
        .append(pad(pHour)).append(":") 
        .append(pad(pMinute))); 
    } 

    /** Displays a notification when the time is updated */ 
    private void displayToast() { 
     Toast.makeText(this, new StringBuilder().append("Time choosen is ").append(displayTime.getText()), Toast.LENGTH_SHORT).show(); 

    } 

    /** Add padding to numbers less than ten */ 
    private static String pad(int c) { 
     if (c >= 10) 
      return String.valueOf(c); 
     else 
      return "0" + String.valueOf(c); 
    } 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_time_picker); 

     /** Capture our View elements */ 
     displayTime = (TextView) findViewById(R.id.timeDisplay); 
     pickTime = (Button) findViewById(R.id.pickTime); 

     /** Listener for click event of the button */ 
     pickTime.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       showDialog(TIME_DIALOG_ID); 
      } 
     }); 

     /** Get the current time */ 
     final Calendar cal = Calendar.getInstance(); 
     pHour = cal.get(Calendar.HOUR_OF_DAY); 
     pMinute = cal.get(Calendar.MINUTE); 

     /** Display the current time in the TextView */ 
     updateDisplay(); 
    } 

    /** Create a new dialog for time picker */ 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case TIME_DIALOG_ID: 
      return new TimePickerDialog(this, 
        mTimeSetListener, pHour, pMinute, false); 
     } 
     return null; 
    } 
} 

XML код

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <TextView android:id="@+id/timeDisplay" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Time will appear here after being selected" 
    android:textSize="30sp"/> 
    <Button android:id="@+id/pickTime" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Change the time"/> 
</LinearLayout> 
+0

вы можете обратиться к http://stackoverflow.com/questions/2659954/timepickerdialog-and-am-or-pm – Ashish

ответ

0

Вы можете использовать SimpleDateFormat. Попробуйте следующее:

final String inputTime = "17:34"; 

try { 
    final SimpleDateFormat sdf = new SimpleDateFormat("H:mm"); 
    final Date dateObj = sdf.parse(time); 
    String outputTime = new SimpleDateFormat("K:mm a").format(dateObj); 
} catch (final ParseException e) { 
    e.printStackTrace(); 
} 
Смежные вопросы