如何以12小时格式而不是24小时格式显示时间 - timeEt.setIs24HourView(false);不管用

时间:2013-03-25 18:14:17

标签: java android string time timepicker

我已经创建了一个数据库,用于存储来自时间戳的currentHour和currentMinute,但我希望以12小时格式而不是24格式显示它以及我目前的实现

timeEt.setIs24HourView(false);

没有工作。

P.S。

当前分钟也缺少0整数,因此时间显示为14:3而不是14:03 [正确的24小时格式]或2:03 [正确的12小时格式]

帮助!

NoobNinja

import android.app.Activity;
import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TimePicker;

public class AddEditCountry extends Activity {

     private long rowID; 
     private EditText nameEt;
     private EditText capEt;
     private EditText codeEt;
     private TimePicker timeEt;

       @Override
       public void onCreate(Bundle savedInstanceState) 
       {
          super.onCreate(savedInstanceState); 
          setContentView(R.layout.add_country);

          nameEt = (EditText) findViewById(R.id.nameEdit);
          capEt = (EditText) findViewById(R.id.capEdit);
          codeEt = (EditText) findViewById(R.id.codeEdit);
          timeEt = (TimePicker) findViewById(R.id.timeEdit);




          Bundle extras = getIntent().getExtras(); 

          if (extras != null)
          {
             rowID = extras.getLong("row_id");
             nameEt.setText(extras.getString("name"));  
             capEt.setText(extras.getString("cap"));  
             codeEt.setText(extras.getString("code"));    
             String time = extras.getString("time");
             String[] parts = time.split(":");
             timeEt.setCurrentHour(Integer.valueOf(parts[0]));
             timeEt.setCurrentMinute(Integer.valueOf(parts[1]));
             timeEt.setIs24HourView(false);
          }

          Button saveButton =(Button) findViewById(R.id.saveBtn);
          saveButton.setOnClickListener(new OnClickListener() {

              public void onClick(View v) 
              {
                 if (nameEt.getText().length() != 0)
                 {
                    AsyncTask<Object, Object, Object> saveContactTask = 
                       new AsyncTask<Object, Object, Object>() 
                       {
                          @Override
                          protected Object doInBackground(Object... params) 
                          {
                             saveContact();
                             return null;
                          }

                          @Override
                          protected void onPostExecute(Object result) 
                          {
                             finish();
                          }
                       }; 

                    saveContactTask.execute((Object[]) null); 
                 }

                 else
                 {
                    AlertDialog.Builder alert = new AlertDialog.Builder(AddEditCountry.this);
                    alert.setTitle(R.string.errorTitle); 
                    alert.setMessage(R.string.errorMessage);
                    alert.setPositiveButton(R.string.errorButton, null); 
                    alert.show();
                 }
              } 
         });
       }


           private void saveContact() 
           {
              DatabaseConnector dbConnector = new DatabaseConnector(this);

              if (getIntent().getExtras() == null)
              {
                  dbConnector.insertContact(nameEt.getText().toString(),
                          capEt.getText().toString(),
                          timeEt.getCurrentHour().toString() + ":"
                              + timeEt.getCurrentMinute().toString(),
                          codeEt.getText().toString());
              }
              else
              {
                 dbConnector.updateContact(rowID,
                         nameEt.getText().toString(),
                            capEt.getText().toString(),
                              timeEt.getCurrentHour().toString() + ":"
                                  + timeEt.getCurrentMinute().toString(),
                              codeEt.getText().toString());
              }
           }
}

1 个答案:

答案 0 :(得分:1)

要获得显示分钟的前导0,您可以使用以下内容:

DecimalFormat df =   new DecimalFormat  ("00");
String minutes = df.format(min);

要计算12小时格式,为什么不在必要时减去12小时呢?