java - 如何在网络请求中将日期格式化为字符串

标签 java android datetime android-volley

如何在 Mysql DB 中保存我的日期,在我使用的代码下方,保存位置,并重复 x 次。

但现在我需要保存日期,我尝试像代码 fragment 中显示的那样。

这是更新我的代码。

public void startGeo(View view) {
    isRunning = true;
    ha.postDelayed(new Runnable() {

        @Override
        public void run() {

            StringRequest request = new StringRequest(Request.Method.POST, insertURL, new com.android.volley.Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                }
            }, new com.android.volley.Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> parameters = new HashMap<String, String>();
                    parameters.put("latitude", t_latitude.getText().toString());
                    parameters.put("longitude", t_longitude.getText().toString());


                    return parameters;

                }
                protected void sendLocationDataToWebsite(Location location) {
                    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    dateFormat.setTimeZone(TimeZone.getDefault());
                    Date date = new Date(location.getTime());
                    Map<String, String> parameters = new HashMap<String, String>();
                    parameters.put("date", URLEncoder.encode(dateFormat.format(date), "UTF-8");

                }
            };
            requestQueue.add(request);
            Toast.makeText(MapActivity.this, "Enviado", LENGTH_LONG).show();

            if (isRunning) {
                ha.postDelayed(this, 10000);
            }
        }
    }, 10000);
}

非常感谢

最佳答案

这是日期代码

private String getDateTime() {
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    Date date = new Date();
    return dateFormat.format(date);
}

此代码用于放入请求

parameters.put("date", getDateTime());

关于java - 如何在网络请求中将日期格式化为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48086445/

相关文章:

C# 将日期从 xml(格式可能因地区而异)转换为 DateTime 数据类型

java - Android 中的字符串到日期对象

android - 适用于移动安卓的 PayPal 快速结帐

c# - 如何从 MVC 中的 DateTime 变量中删除时间部分

java: Statement.RETURN_GENERATED_KEYS Err:S1000 Exp: 在结果集开始之前

java - 如何在片段中使用 setContentView 方法?

android - 用于集成测试的 Dagger Inject

java - 复制 View 以创建 View 的精确副本。相对于屏幕移动 View

java - 比较两种选择排序方法

java - Eclipse 没有像我在其他地方看到的那样在我的 Maven 项目中显示 src/main/resources