duandang6111 2015-12-11 01:58
浏览 478
已采纳

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";
  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100