如何从时间戳获取MM / DD / YY格式的日期


问题内容

我想MM/DD/YY从时间戳获取格式的日期。

我使用了下面的方法,但是没有给出正确的输出

final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(Long.parseLong(1306249409));    
Log.d("Date--",""+cal.DAY_OF_MONTH);    
Log.d("Month--",""+cal.MONTH);    
Log.d("Year--",""+cal.YEAR);

但其给出的输出如下

日期–5月–2年–1

时间戳记的正确日期是2010年5月24日-1306249409

注意-时间戳是由我的应用程序中使用的Web服务接收的。


问题答案:

更好的方法

简单使用
SimpleDateFormat

new SimpleDateFormat("MM/dd/yyyy").format(new Date(timeStampMillisInLong));

方法上的错误

DAY_OF_MONTH,,MONTH.. etc只是Calendar类内部使用的常量int值

你可以得到所代表的日期calcal.get(Calendar.DATE)