我对Java / Android开发有点新,我需要比较两次。其中一个时间值存储在一个字符串中,另一个是系统时间的当前时间(长) - 如何找出两次之间的差异(以毫秒为单位)并将其存储为数字?
编辑:
尝试#1(对懒惰忍者的回应)
public class Rules extends Activity {
private String password;
private PendingIntent mPendingIntent;
String TIMELIMIT = "10";
TextView textSsid, textSpeed, textRssi, Time;
private static final int NOTIFY_ME_ID = 1337;
private int count = 0;
private NotificationManager notifyMgr = null;
public Handler mHandler = new Handler();
public long mStartRX = 0;
public long mStartTX = 0;
public long txBytes;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rules);
String NDEF_PREF = "prefs";
SharedPreferences prefs = getSharedPreferences(NDEF_PREF,
Context.MODE_PRIVATE);
String name = prefs.getString("name", "");
String code = prefs.getString("corename", "");
String time = prefs.getString("time", "");
String ssid = prefs.getString("restricted", "");
Time = (TextView) findViewById(R.id.Time);
Time.setText(time);
String dtStart = "09:27:37";
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
try {
Date date = format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
Long convertedLong = date.getTimeInMillis();
long currentTimeLong = System.currentTimeMillis();
long difference = currentTimeLong - convertedLong;
String strDiff = String.valueOf(difference);
问题:
日期无法解析Rules.java Java问题
答案 0 :(得分:1)
确定将您的字符串转换为日期时间:
String dtStart = "09:27:37";
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
try {
Date date = format.parse(dtStart);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
然后将其转换为millisec
long convertedLong = date.getTimeInMillis();
然后
long currentTimeLong = System.currentTimeMillis(); // will get you current time in milli
long difference = currentTimeLong - convertedLong;
String strDiff = String.valueOf(difference);
从那里数学currentTimeLong - convertedLong。