【问题标题】:Convert SimpleDateFormat to String Issues将 SimpleDateFormat 转换为字符串问题
【发布时间】:2012-01-19 09:32:49
【问题描述】:

我正在尝试将 SimpleDateFormat 转换为 String,但结果不正确。

Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Log.d("current date",dateFormat.format(cal.getTime()));
cal.add(Calendar.YEAR,-3);
Log.d("3 years previous date",dateFormat.format(cal.getTime()));

String valnow = dateFormat.format(new Date());

输出:

01-19 05:34:52.148: DEBUG/current date(556): 19/01/2012
01-19 05:34:52.148: DEBUG/3 years previous date(556): 19/01/2009

但是valnow 值 = 19/01/2012

有人可以告诉我这样做的方法吗?如果您有任何通过示例,那将是一个真正的帮助!

【问题讨论】:

  • 您能解释一下您面临的问题吗?您正在给出 dateFormat.format(new Date()) 并且它正在给出当前日期
  • 你的预期输出是什么?
  • 我希望得到 String valnow =19/01/2009。但问题是 valnow=19/01/2012

标签: java android datetime date


【解决方案1】:

尝试替换

String valnow = dateFormat.format(new Date());

String valnow = dateFormat.format(cal.getTime());

【讨论】:

    【解决方案2】:
    long lo = System.currentTimeMillis();
    //    long lo = Long.parseLong(date);
        Log.e("Try", "Check "+lo);
    
        SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMMM d 'at' hh:mm a 'in the year' yyyy G");  
        String formattedDateString = formatter.format(lo);  
        Log.e("Try", "Check Ti`enter code here`me Date "+formattedDateString);
    

    【讨论】:

      【解决方案3】:
          DateFormat.format("MM/dd/yy", System.currentTimeMillis()).toString();
      

      【讨论】:

      猜你喜欢
      • 2018-10-31
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-09
      相关资源
      最近更新 更多