jkk*_*jkk 43 java android timestamp date
我在显示日期时遇到问题,我的时间戳为1379487711,但实际时间是9/18/2013 12:31:51 PM,但它显示时间为17-41-1970.如何将其显示为当前时间.
为了显示时间我使用了以下方法:
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());
} 
Len*_*Bru 82
private String getDate(long time) {
    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(time * 1000);
    String date = DateFormat.format("dd-MM-yyyy", cal).toString();
    return date;
}
注意我把时间放在setTimeInMillis中而不是int,注意我的日期格式有MM而不是mm(mm是分钟,而不是几个月,这就是为什么你的值为"41",其中几个月应该是)
Kau*_*hik 10
用于将时间戳转换为当前时间
Calendar calendar = Calendar.getInstance();
TimeZone tz = TimeZone.getDefault();
calendar.add(Calendar.MILLISECOND, tz.getOffset(calendar.getTimeInMillis()));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
java.util.Date currenTimeZone=new java.util.Date((long)1379487711*1000);
Toast.makeText(TimeStampChkActivity.this, sdf.format(currenTimeZone), Toast.LENGTH_SHORT).show();
将时间戳转换为当前日期:
private Date getDate(long time) {    
    Calendar cal = Calendar.getInstance();
       TimeZone tz = cal.getTimeZone();//get your local time zone.
       SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
       sdf.setTimeZone(tz);//set time zone.
       String localTime = sdf.format(new Date(time) * 1000));
       Date date = new Date();
       try {
            date = sdf.parse(localTime);//get local date
        } catch (ParseException e) {
            e.printStackTrace();
        }
      return date;
    }
  DateFormat df = new SimpleDateFormat("HH:mm", Locale.US);
  final String time_chat_s = df.format(time_stamp_value);
time_stamp_value 变量类型为long 
使用您的代码,它将看起来像这样:
private String getDate(long time_stamp_server) {
    SimpleDateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
    return formatter.format(time_stamp_server);
} 
我将“毫秒”更改为 time_stamp_server。考虑将毫秒的名称更改为“c”或更全局的名称。“c”确实很好,因为它与时间和计算相关,比毫秒更具有全局性。因此,您不一定需要日历对象来转换,它应该就这么简单。
| 归档时间: | 
 | 
| 查看次数: | 68080 次 | 
| 最近记录: |