Here is my string:
2015,10,10,4,22,51
How can I convert this string to timestamp such as:
1447125771290
to compare with
long currentTime = System.currentTimeMillis();
Or any suggestions to compare with current system time?
Here is my string:
2015,10,10,4,22,51
How can I convert this string to timestamp such as:
1447125771290
to compare with
long currentTime = System.currentTimeMillis();
Or any suggestions to compare with current system time?
I think this would help you:
String timestamp = "2015,10,10,04,22,51";
DateFormat df = new SimpleDateFormat("yyyy,MM,dd,hh,mm,ss");
Date parsedDate = df.parse(timestamp);
And compare it with current date:
DateFormat dateFormat = new SimpleDateFormat("yyyy,MM,dd,hh,mm,ss");
Date date = new Date();
And here is how to compare:
public Date compareTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
And the use above method like this:
if (compareTime(parsedDate).equals(date)
Another way to compare dates using Date class:
switch (date.compareTo(parsedDate)) {
case -1: Log.i("CompareDates","today is sooner than parsedDate"); break;
case 0: Log.i("CompareDates","today and parsedDateare equal"); break;
case 1: Log.i("CompareDates","today is later than parsedDate"); break;
default: Log.i("CompareDates","Invalid results from date comparison"); break;
}