2013-07-15 68 views
2

我正在获取系统当前日期并尝试在textview中显示它。从检索日期提取日期 - 月 - 年

尝试下面的代码

private OnClickListener listener1 = new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     long dtMili = System.currentTimeMillis(); 
     Date dt = new Date(dtMili); 
     gett.setText(dt.toString()); 

    } 
}; 

后我所得到的是

enter image description here

,但我只希望像15/7/2013

回答

3

我认为你需要这样的:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
String date = sdf.format(dt); 

另见Customizing Formats

+1

地狱雅。它的工作......正是我想要的......给予好评 –

1

试试这个

long dtMili = System.currentTimeMillis(); 
    Date dt = new Date(dtMili); 
    DateFormat df=new SimpleDateFormat("dd/MM/yyyy"); 
    gett.setText(df.format(dt).toString()); 
+0

TNX为answer.it还曾...给予好评 –

0

这样做

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); 
String dateStr=dateFormat.format(date); 
0

我大多喜欢使用JodaTime相反,对于具有处理日期的一切。在你的情况下,它看起来那么容易,因为这!

DateTime dt = new DateTime(new Date()); 
String st = dt.toString("dd/MM/yyyy");