duandang6111
2015-12-11 01:58
浏览 369

android mysql在textview中显示日期/时间

i am trying to display current date/time in a textview, i have managed to display the date but do not know how to display the time, the format of the date/time must be '2015-12-10 01:52:23' as it will be stored in a database field TIMESTAMP. Heres what i have as of yet (Prints date '2015-12-10' only)

        displayDate = (TextView) findViewById(R.id.DATE);

    final Calendar cal = Calendar.getInstance();
    yy = cal.get(Calendar.YEAR);
    mm = cal.get(Calendar.MONTH);
    dd = cal.get(Calendar.DAY_OF_MONTH);

    // set current date into textview
    displayDate.setText(new StringBuilder()
            // Month is 0 based, just add 1
            .append(yy).append(" ").append("-").append(mm + 1).append("-")
            .append(dd));

It is then passed onto this: final String datetime = displayDate.getText().toString().trim();

which is added into the database:

            @Override
        protected String doInBackground(Void... v) {
            HashMap<String,String> params = new HashMap<>();
            params.put(Config.KEY_EMP_ID, TextUserID);
            params.put(Config.KEY_LAT,lat);
            params.put(Config.KEY_LON,lon);

Config is a java file which has the keys and JSON tags to query the database using php:

//Keys that will be used to send the request to php scripts
public static final String KEY_EMP_ID = "UserID";
public static final String KEY_LAT = "Latitude";
public static final String KEY_LON = "Longitude";
public static final String KEY_TIMEINSERTED = "TimeInserted";

图片转代码服务由CSDN问答提供 功能建议

我试图在textview中显示当前日期/时间,我已设法显示日期但不知道 如何显示时间,日期/时间的格式必须为'2015-12-10 01:52:23',因为它将存储在数据库字段TIMESTAMP中。 继承了我的目标(打印日期'2015-12-10'仅限)

  displayDate =(TextView)findViewById(R.id.DATE); 
 \  n final Calendar cal = Calendar.getInstance(); 
 yy = cal.get(Calendar.YEAR); 
 mm = cal.get(Calendar.MONTH); 
 dd = cal.get(Calendar.DAY_OF_MONTH);  
 
 //将当前日期设置为textview 
 displayDate.setText(new StringBuilder()
 //月份基于0,只需添加1 
 .append(yy).append(“”)。append(“  - “)。append(mm + 1).append(” - “)
 .append(dd)); 
   
 
 

然后将其传递给: final String datetime = displayDate.getText()。toString()。trim();

添加到数据库中:

 <  code> @Override 
 protected String doInBackground(Void ... v){
 HashMap&lt; String,String&gt;  params = new HashMap&lt;&gt;(); 
 params.put(Config.KEY_EMP_ID,TextUserID); 
 params.put(Config.KEY_LAT,lat); 
 params.put(Config.KEY_LON,lon); \  n   
 
 

Config是一个java文件,它包含用于使用php查询数据库的密钥和JSON标记:

  /  /将用于将请求发送到php脚本的键
public static final String KEY_EMP_ID =“UserID”; 
 public static final String KEY_LAT =“Latitude”; 
 public static final String KEY_LON =“Longitude”; 
public static final 字符串KEY_TIMEINSERTED =“TimeInserted”; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doutan5798 2015-12-11 02:28
    已采纳

    I don't have the ability to test this at the moment, but you might want to consider using SimpleDateFormat.

    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    displayDate.setText(dateFormat.format(calendar.getTime()));
    

    You should also consider using a locale as well.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsfgdsjfd78773 2015-12-11 02:28

    You can get the time from the Calendar as well:

    hh12 = cal.get(Calendar.HOUR); -- 12 hour format
    hh24 = cal.get(Calendar.HOUR_OF_DAY); -- 24 hour format
    min = cal.get(Calendar.MINUTE);
    sec = cal.get(Calendar.SECOND);
    

    Then you can append it to your displayDate variable.

    Learn more about Calendar class here.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题