【问题标题】:String date conversion error in AndroidAndroid中的字符串日期转换错误
【发布时间】:2014-08-13 11:57:13
【问题描述】:

我正在开发一个 Android 应用程序,它有两个 EditText 框和一个按钮。 在第一个 EditText 框中,我将值设置为 13/08/2014。按下按钮后,第一个 EditText 的值将转换为 Date,然后插入到第二个 EditText 中。

我使用以下代码。

            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
            String ss=todaysdate.getText().toString();
            Date d1 = df.parse(todaysdate.getText().toString());

按下按钮后,ss 和 d1 的值变为

ss = 13/08/2014
d1 = Thu Jan 08 00:00:00 IST 2015

看到变化,变成了 2015 年 1 月 8 日

为什么会这样,错误是什么?

【问题讨论】:

  • 您想要的最终格式是什么?我假设 13/08/2014 是初始格式
  • 最终格式为MM/dd/yyyy
  • 使用 yyyy-MM-dd'T'hh:mm:ss
  • 字符串 date1 = "";日期 date3 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss") .parse(date1); DateFormat df6 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a"); System.out.println("输出:" + "" + " " +date1+"::" +df6.format(date3))
  • 你得到答案了吗??

标签: android date


【解决方案1】:

喜欢:-

  String date1 = "15/08/2014";
                Date date3 = new SimpleDateFormat("dd/MM/yyyy")
                        .parse(date1);
                Calendar newCalendar = Calendar.getInstance();
                newCalendar.setTime(date3);
                            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

                System.out.println(newCalendar.getTime());

                String formatted = format1.format(newCalendar.getTime());
                System.out.println(formatted);

【讨论】:

    【解决方案2】:
    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date date = new Date();
    String datestring= f.format(date).toString()
    

    【讨论】:

      猜你喜欢
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多