将SimpleDateFormat转换为字符串问题

时间:2012-01-19 09:32:50

标签: java android datetime date

我正在尝试将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

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

3 个答案:

答案 0 :(得分:7)

尝试替换

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

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

答案 1 :(得分:1)

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);

答案 2 :(得分:-1)

    DateFormat.format("MM/dd/yy", System.currentTimeMillis()).toString();