转换时间戳为当前日期的android [英] convert timestamp into current date in android

查看:260
本文介绍了转换时间戳为当前日期的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在显示日期的问题,我得到时间戳1379487711,但按这个实际的时间是2013年9月18号下午12时31分51秒,但它显示的时间为17-41-1970。如何将其显示为当前时间。

用于显示时间我都用下面的方法:

 私人字符串GETDATE(长毫秒){
    //在指定的显示日期创建DateFormatter对象
    // 格式。
    SimpleDateFormat的格式化=新的SimpleDateFormat(DD-MM-YYYY);
    //创建日历对象,该对象将在日期和时间值转换
    //毫秒日期。
    台历挂历= Calendar.getInstance();
    calendar.setTimeInMillis((INT)毫秒);
    返回formatter.format(calendar.getTime());
}
 

解决方案

 私人字符串GETDATE(时间长){
    日历CAL = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(时间);
    字符串日期= DateFormat.format(DD-MM-YYYY,CAL)的ToString();
    返回日期;
}
 

请注意,我把时间setTimeInMillis长,而不是如int,注意到我的日期格式有MM而不是毫米( mm的是分,而不是几个月,这就是为什么你有一个对41里的月应该是)

I have a problem in displaying the date,I am getting timestamp as 1379487711 but as per this the actual time is 9/18/2013 12:31:51 PM but it displays the time as 17-41-1970. How to show it as current time.

for displaying time I have used the following method:

private String getDate(long milliSeconds) {
    // Create a DateFormatter object for displaying date in specified
    // format.
    SimpleDateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
    // Create a calendar object that will convert the date and time value in
    // milliseconds to date.
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis((int) milliSeconds);
    return formatter.format(calendar.getTime());
} 

解决方案

private String getDate(long time) {
    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(time);
    String date = DateFormat.format("dd-MM-yyyy", cal).toString();
    return date;
}

notice that i put the time in setTimeInMillis as long and not as int, notice my date format has MM and not mm (mm is for minutes, and not months, this is why you have a value of "41" where the months should be)

这篇关于转换时间戳为当前日期的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆