Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Detect Past Times In a String
String[] pastTimeIndicators = new String[]{"last sec", "last min", "last hr", "last hour", "yesterday", "last week", "last month", "last year"};
if(isStringContainAnyOfTheseWords(group.getText().toLowerCase(), pastTimeIndicators)){
Toast.makeText(getContext(),getString(R.string.past_time_warning),Toast.LENGTH_LONG).show();
Calendar opCalendar = Calendar.getInstance();
opCalendar.setTime(currentTime);
opCalendar.add(Calendar.SECOND,-1);
long lastSecTime = opCalendar.getTimeInMillis();
opCalendar.setTime(currentTime);
opCalendar.add(Calendar.MINUTE,-1);
long lastMinTime = opCalendar.getTimeInMillis();
opCalendar.setTime(currentTime);
opCalendar.add(Calendar.HOUR,-1);
long lastHrTime = opCalendar.getTimeInMillis();
opCalendar.setTime(currentTime);
opCalendar.add(Calendar.DAY_OF_YEAR,-1);//yesterday
long yesterdayTime = opCalendar.getTimeInMillis();
opCalendar.setTime(currentTime);
opCalendar.add(Calendar.WEEK_OF_YEAR,-1);//last week
long lastWeekTime = opCalendar.getTimeInMillis();
opCalendar.setTime(currentTime);
opCalendar.add(Calendar.MONTH,-1);//last month
long lastMonthTime = opCalendar.getTimeInMillis();
opCalendar.setTime(currentTime);
opCalendar.add(Calendar.YEAR,-1);//last year
long lastYearTime = opCalendar.getTimeInMillis();
long remindTimeLong = remindDates.get(i).getTime();
if(remindTimeLong == lastSecTime ||
remindTimeLong == lastMinTime ||
remindTimeLong == lastHrTime ||
remindTimeLong == yesterdayTime ||
remindTimeLong == lastWeekTime ||
remindTimeLong == lastMonthTime ||
remindTimeLong == lastYearTime){//if meets explicitly indicated past times, just ignore it
remindDates.remove(i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.