2013-07-13 163 views
1

我有一部分代码可以查找用户以前保存的以毫秒为单位的保存时间。当从内存中取出时间时,由于某些原因,时间增加1分钟,转换为简单日期格式。我无法弄清楚为什么会发生这种情况。以下是用于检索的时间并将其转换长时间转换为1分钟

time = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getFloat(String.valueOf(id)+"TIME", 0); 
      String dateString = new SimpleDateFormat("hh:mm a").format(new Date((long) time)); 
      mPickedTimeText.setText(dateString); 

,并在那里的时间被提前保存

this.time = time.getTimeInMillis(); 
    PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
    SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
    Editor editor=prefs.edit(); 
    editor.putFloat(String.valueOf(id)+"TIME", time); 
    editor.commit(); 
    finish(); 

感谢的代码部分的代码!

回答

3

float数据类型没有足够的精度来表示时间到一分钟的准确度。请将时间存储为long

editor.putLong(String.valueOf(id)+"TIME", time); 

time = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getLong(String.valueOf(id)+"TIME", 0); 
String dateString = new SimpleDateFormat("hh:mm a").format(new Date(time)); 
相关问题