Android:时间戳显示

时间:2011-12-08 04:44:47

标签: android timestamp

我有问题,我想在我的数据库中插入时间戳。每次用户点击发送按钮时,显示的时间戳应该是用户点击按钮的时间。我在这里有代码:

btnSend.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String phoneNo = editTextRecipient.getText().toString();
            String message = editTextNewMessage.getText().toString(); 
            Log.d(phoneNo, message);
            saveState(phoneNo, message, details);
            boolean split = false;

            SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
            String date = s.format(new Date());

            /*Bundle bundle = new Bundle();

            bundle.putString(MessagesDBAdapter.KEY_RECIPIENT, editTextRecipient.getText().toString());
            bundle.putString(MessagesDBAdapter.KEY_MESSAGE, editTextNewMessage.getText().toString());
            bundle.putString(MessagesDBAdapter.KEY_DETAILS, tv_details.getText().toString());
            if (mRowId != null) {
                bundle.putLong(MessagesDBAdapter.KEY_ROWID, mRowId);
            }*/

            final Toast toast = Toast.makeText(getBaseContext(), 
                     "Your message " + "\"" + message + "\"" + " is sent to " +"\""+ phoneNo+"\"", 
                      Toast.LENGTH_SHORT);

            Runnable showToastRunnable = new Runnable() {
              public void run() {
                  dialog.cancel();
                  toast.show();
              }
          };

            if (phoneNo.length()>0 && message.length()>0)  {
                showProgress();
                if (count == 0) {
                      handler.postDelayed(showToastRunnable, 0);
                  }
                  else if (count == 1) {
                      handler.postDelayed(showToastRunnable, 15000);
                  }
                  else if (count == 2) {
                      handler.postDelayed(showToastRunnable, 30000);
                  }
                  else if (count == 3) {
                      handler.postDelayed(showToastRunnable, 60000);
                  }
            }

               // sendSMS(phoneNo, message, split); */
            else
                Toast.makeText(getBaseContext(), 
                    "Please enter both phone number and message.", 
                    Toast.LENGTH_SHORT).show();
        }
    });      
     mDbHelper.close();
}

 private void populateFields() {
    if (mRowId!=null) {
        Cursor message=mDbHelper.fetchNote(mRowId);
        startManagingCursor(message);
        editTextRecipient.setText(message.getString(message.getColumnIndexOrThrow(MessagesDBAdapter.KEY_RECIPIENT)));
        editTextNewMessage.setText(message.getString(message.getColumnIndexOrThrow(MessagesDBAdapter.KEY_MESSAGE)));
        tv_details.setText(message.getString(message.getColumnIndexOrThrow(MessagesDBAdapter.KEY_DETAILS)));
    }
 }

 private void saveState(String phoneNo, String message, String details) {
     Log.i(phoneNo, message);

    if (mRowId==null) {
        mDbHelper.createNote(phoneNo, message, details);
    }
 }

时间戳应在24小时制显示(12:30等)。我做得对吗,我错过了什么?任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您可以使用Calendar课程。

Calendar now = Calendar.getInstance();
final int hour = now.get(Calendar.HOUR_OF_DAY);
final int minute = now.get(Calendar.MINUTE);