Java Code Examples for java.util.Calendar.compareTo()

The following are Jave code examples for showing how to use compareTo() of the java.util.Calendar class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: diary   File: Diary.java   View Source Code Vote up 7 votes
private void setDate(Calendar date)
{
    setTitleDate(date.getTime());

    int year = date.get(Calendar.YEAR);
    int month = date.get(Calendar.MONTH);
    int day = date.get(Calendar.DATE);

    Calendar calendar = GregorianCalendar.getInstance();
    Calendar today = new GregorianCalendar(calendar.get(Calendar.YEAR),
                                           calendar.get(Calendar.MONTH),
                                           calendar.get(Calendar.DATE));

    prevEntry = getPrevEntry(year, month, day);
    if ((prevEntry == null || today.compareTo(prevEntry) > 0) &&
            today.compareTo(date) < 0)
        prevEntry = today;
    currEntry = date;
    nextEntry = getNextEntry(year, month, day);
    if ((nextEntry == null || today.compareTo(nextEntry) < 0) &&
            today.compareTo(date) > 0)
        nextEntry = today;

    invalidateOptionsMenu();
}
 
Example 2
Project: springboot-security-wechat   File: DateUtils.java   View Source Code Vote up 7 votes
/**
 * 计算两个Date之间的工作日时间差
 *
 * @param start 开始时间
 * @param end   结束时间
 * @return int 返回两天之间的工作日时间
 */
public static int countDutyday(Date start, Date end) {
    if (start == null || end == null) return 0;
    if (start.after(end)) return 0;
    Calendar c_start = Calendar.getInstance();
    Calendar c_end = Calendar.getInstance();
    c_start.setTime(start);
    c_end.setTime(end);
    //时分秒毫秒清零
    c_start.set(Calendar.HOUR_OF_DAY, 0);
    c_start.set(Calendar.MINUTE, 0);
    c_start.set(Calendar.SECOND, 0);
    c_start.set(Calendar.MILLISECOND, 0);
    c_end.set(Calendar.HOUR_OF_DAY, 0);
    c_end.set(Calendar.MINUTE, 0);
    c_end.set(Calendar.SECOND, 0);
    c_end.set(Calendar.MILLISECOND, 0);
    //初始化第二个日期,这里的天数可以随便的设置
    int dutyDay = 0;
    while (c_start.compareTo(c_end) < 0) {
        if (c_start.get(Calendar.DAY_OF_WEEK) != 1 && c_start.get(Calendar.DAY_OF_WEEK) != 7)
            dutyDay++;
        c_start.add(Calendar.DAY_OF_YEAR, 1);
    }
    return dutyDay;
}
 
Example 3
Project: boohee_v5.6   File: SportRemindReceiver.java   View Source Code Vote up 7 votes
public static void start(Context ctx) {
    AlarmManager manager = (AlarmManager) ctx.getSystemService("alarm");
    PendingIntent pending = PendingIntent.getBroadcast(ctx, 152, new Intent(ctx,
            SportRemindReceiver.class), 134217728);
    Helper.showLog(TAG, "SportRemindReceiver start");
    Calendar calNow = Calendar.getInstance();
    Calendar calSet = (Calendar) calNow.clone();
    calSet.setTimeInMillis(System.currentTimeMillis());
    int remindHour = 20;
    int remindMinute = 30;
    if (!TextUtil.isEmpty(OnePreference.getPrefSportRemindTime())) {
        String[] times = OnePreference.getPrefSportRemindTime().split("#");
        if (times.length == 2) {
            remindHour = Integer.parseInt(times[0]);
            remindMinute = Integer.parseInt(times[1]);
        }
    }
    calSet.set(11, remindHour);
    calSet.set(12, remindMinute);
    calSet.set(13, 0);
    calSet.set(14, 0);
    if (calSet.compareTo(calNow) <= 0) {
        calSet.add(5, 1);
    }
    manager.setRepeating(0, calSet.getTimeInMillis(), a.h, pending);
}
 
Example 4
Project: happylifeplat-transaction   File: DateUtils.java   View Source Code Vote up 6 votes
/**
 * 比较两个日期大小(比较年月日时分秒),com1大于com2返回1,反之返回-1,相等则返回0
 *
 * @param date1
 * @param date2
 * @return
 */
public static int compareDateOfSecond(Date date1, Date date2) {
    Calendar c1 = Calendar.getInstance();
    c1.setTime(date1);
    c1.set(Calendar.MILLISECOND, 0);
    Calendar c2 = Calendar.getInstance();
    c2.setTime(date2);
    c2.set(Calendar.MILLISECOND, 0);
    return c1.compareTo(c2);

}
 
Example 5
Project: kettle_support_kettle8.0   File: DateUtils.java   View Source Code Vote up 6 votes
/**
 * 获取年份时间段内的所有年
 * 
 * @param StartDate
 * @param endDate
 * @return
 */
public static List<String> getYearBy(String StartDate, String endDate) {
	DateFormat df = new SimpleDateFormat("yyyy");
	Date date1 = null; // 开始日期
	Date date2 = null; // 结束日期
	try {
		date1 = df.parse(StartDate);
		date2 = df.parse(endDate);
	} catch (ParseException e) {
		e.printStackTrace();
	}
	Calendar c1 = Calendar.getInstance();
	Calendar c2 = Calendar.getInstance();
	// 定义集合存放月份
	List<String> list = new ArrayList<String>();
	// 添加第一个月,即开始时间
	list.add(df.format(date1));
	c1.setTime(date1);
	c2.setTime(date2);
	while (c1.compareTo(c2) < 0) {
		c1.add(Calendar.YEAR, 1);// 开始日期加一个月直到等于结束日期为止
		Date ss = c1.getTime();
		String str = df.format(ss);
		list.add(str);
	}
	return list;
}
 
Example 6
Project: xbin-store   File: DateUtils.java   View Source Code Vote up 6 votes
/**
 * 根据单位字段比较两个日期
 * 
 * @param date
 *            日期1
 * @param otherDate
 *            日期2
 * @param withUnit
 *            单位字段,从Calendar field取值
 * @return 等于返回0值, 大于返回大于0的值 小于返回小于0的值
 */
public static int compareDate(Date date, Date otherDate, int withUnit) {
	Calendar dateCal = Calendar.getInstance();
	dateCal.setTime(date);
	Calendar otherDateCal = Calendar.getInstance();
	otherDateCal.setTime(otherDate);

	switch (withUnit) {
	case Calendar.YEAR:
		dateCal.clear(Calendar.MONTH);
		otherDateCal.clear(Calendar.MONTH);
	case Calendar.MONTH:
		dateCal.set(Calendar.DATE, 1);
		otherDateCal.set(Calendar.DATE, 1);
	case Calendar.DATE:
		dateCal.set(Calendar.HOUR_OF_DAY, 0);
		otherDateCal.set(Calendar.HOUR_OF_DAY, 0);
	case Calendar.HOUR:
		dateCal.clear(Calendar.MINUTE);
		otherDateCal.clear(Calendar.MINUTE);
	case Calendar.MINUTE:
		dateCal.clear(Calendar.SECOND);
		otherDateCal.clear(Calendar.SECOND);
	case Calendar.SECOND:
		dateCal.clear(Calendar.MILLISECOND);
		otherDateCal.clear(Calendar.MILLISECOND);
	case Calendar.MILLISECOND:
		break;
	default:
		throw new IllegalArgumentException("withUnit 单位字段 " + withUnit + " 不合法!!");
	}
	return dateCal.compareTo(otherDateCal);
}
 
Example 7
Project: xbin-store   File: DateUtils.java   View Source Code Vote up 6 votes
/**
 * 根据单位字段比较两个时间
 * 
 * @param date
 *            时间1
 * @param otherDate
 *            时间2
 * @param withUnit
 *            单位字段,从Calendar field取值
 * @return 等于返回0值, 大于返回大于0的值 小于返回小于0的值
 */
public static int compareTime(Date date, Date otherDate, int withUnit) {
	Calendar dateCal = Calendar.getInstance();
	dateCal.setTime(date);
	Calendar otherDateCal = Calendar.getInstance();
	otherDateCal.setTime(otherDate);

	dateCal.clear(Calendar.YEAR);
	dateCal.clear(Calendar.MONTH);
	dateCal.set(Calendar.DATE, 1);
	otherDateCal.clear(Calendar.YEAR);
	otherDateCal.clear(Calendar.MONTH);
	otherDateCal.set(Calendar.DATE, 1);
	switch (withUnit) {
	case Calendar.HOUR:
		dateCal.clear(Calendar.MINUTE);
		otherDateCal.clear(Calendar.MINUTE);
	case Calendar.MINUTE:
		dateCal.clear(Calendar.SECOND);
		otherDateCal.clear(Calendar.SECOND);
	case Calendar.SECOND:
		dateCal.clear(Calendar.MILLISECOND);
		otherDateCal.clear(Calendar.MILLISECOND);
	case Calendar.MILLISECOND:
		break;
	default:
		throw new IllegalArgumentException("withUnit 单位字段 " + withUnit + " 不合法!!");
	}
	return dateCal.compareTo(otherDateCal);
}
 
Example 8
Project: cim-processor   File: ModelParser.java   View Source Code Vote up 6 votes
private static AppliedOutages applyOutages(Calendar outageDate, int model, boolean logDetail) {
	AppliedOutages result = new AppliedOutages();
	result.setEffectiveStart(outageDate);
	
	// apply outages to model
	log.trace("Apply outages for " + formatter.format(outageDate.getTime()));
	// keep the list of applied outages for later evaluation
	for (Outage outage : outages) {
		result.incramentExaminedOutages();
		if (outage.getEffectiveStartHour().toGregorianCalendar().compareTo(outageDate) <= 0 && outageDate.compareTo(outage.getEffectiveEndHour().toGregorianCalendar()) <= 0) {
			result.incramentOutagesInDateRange();
			OutageModel.OutageMatch outageMatch = OutageModel.match(outage, model, logDetail);
			if (OutageModel.applyOutage(outage, outageMatch, logDetail)) {
				result.getAppliedOutages().add(outage);
				if (result.getEffectiveEnd() == null || result.getEffectiveEnd().compareTo(outage.getEffectiveEndHour().toGregorianCalendar()) > 0) result.setEffectiveEnd((GregorianCalendar)outage.getEffectiveEndHour().toGregorianCalendar().clone());
				result.incramentOutagesApplied();
			} else {
				result.incramentOutagesNotApplied();
			}
		}
	}
	log.trace("Examined " + result.getExaminedOutages() + " outage(s)");
	log.trace("" + result.getOutagesInDateRange() + " outage(s) in date range");
	log.trace("Applied " + result.getOutagesApplied() + " outage(s) in date range");
	log.trace("Did not apply " + result.getOutagesNotApplied() + " outage(s) in date range");

	return result;
}
 
Example 9
Project: PTEAssistant   File: AppointmentSearcher.java   View Source Code Vote up 6 votes
private boolean searchNormalEarliestAppointment(List<AvailableAppointment> availableAppointmentList) {
    AvailableAppointment earliestAvappt = null;
    Calendar minCalendar = null;
    for(AvailableAppointment avappt : availableAppointmentList) {
        Entry<Calendar, Map<Integer, List<Time>>> earliestYearMonthEntry = avappt.timeMap.entrySet().iterator().next();
        Calendar earliestYearMonth = earliestYearMonthEntry.getKey();
        if(!earliestYearMonthEntry.getValue().entrySet().isEmpty()) {
            Entry<Integer, List<Time>> earliestDateEntry = earliestYearMonthEntry.getValue().entrySet().iterator().next();
            int earliestDate = earliestDateEntry.getKey();
            if(!earliestDateEntry.getValue().isEmpty()) {
                Time earliestTime = earliestDateEntry.getValue().get(0);
                Calendar cal = Calendar.getInstance();
                cal.set(earliestYearMonth.get(Calendar.YEAR), earliestYearMonth.get(Calendar.MONTH), earliestDate,
                        earliestTime.getHour(), earliestTime.getMinute());
                
                if(minCalendar == null || cal.compareTo(minCalendar) < 0) {
                    minCalendar = cal;
                    earliestAvappt = avappt;
                }
            }
        }
    }
    if(minCalendar != null) {
        saveAsSearchResult(minCalendar, earliestAvappt.testCenter);
        return true;
    } else {
        return false;
    }
}
 
Example 10
Project: PTEAssistant   File: AppointmentSearcher.java   View Source Code Vote up 6 votes
private boolean searchRangeEarliestAppointment(List<AvailableAppointment> availableAppointmentList) {
    AvailableAppointment earliestAvappt = null;
    Calendar minCalendar = null;
    Time minTime = new Time(startCalendar.get(Calendar.HOUR_OF_DAY), startCalendar.get(Calendar.MINUTE));
    Time maxTime = new Time(endCalendar.get(Calendar.HOUR_OF_DAY), endCalendar.get(Calendar.MINUTE));
    for(AvailableAppointment avappt : availableAppointmentList) {
        Entry<Calendar, Map<Integer, List<Time>>> earliestYearMonthEntry = avappt.timeMap.entrySet().iterator().next();
        Calendar earliestYearMonth = earliestYearMonthEntry.getKey();
        if(!earliestYearMonthEntry.getValue().entrySet().isEmpty()) {
            Entry<Integer, List<Time>> earliestDateEntry = earliestYearMonthEntry.getValue().entrySet().iterator().next();
            int earliestDate = earliestDateEntry.getKey();
            if(!earliestDateEntry.getValue().isEmpty()) {
                Time earliestTime = null;
                for(Time time : earliestDateEntry.getValue()) {
                    if(minTime.compareTo(time)<1 && time.compareTo(maxTime)<1) {
                        earliestTime = time;
                        break;
                    }
                }
                if(earliestTime != null) {
                    Calendar cal = Calendar.getInstance();
                    cal.set(earliestYearMonth.get(Calendar.YEAR), earliestYearMonth.get(Calendar.MONTH), earliestDate,
                            earliestTime.getHour(), earliestTime.getMinute());
                    if(minCalendar == null || cal.compareTo(minCalendar) < 0) {
                        minCalendar = cal;
                        earliestAvappt = avappt;
                    }
                }
            }
        }
    }
    if(minCalendar != null) {
        saveAsSearchResult(minCalendar, earliestAvappt.testCenter);
        return true;
    } else {
        return false;
    }
}
 
Example 11
Project: mumu-core   File: DateUtils.java   View Source Code Vote up 6 votes
/**
 * 根据单位字段比较两个日期
 * 
 * @param date
 *            日期1
 * @param otherDate
 *            日期2
 * @param withUnit
 *            单位字段,从Calendar field取值
 * @return 等于返回0值, 大于返回大于0的值 小于返回小于0的值
 */
public static int compareDate(Date date, Date otherDate, int withUnit) {
	Calendar dateCal = Calendar.getInstance();
	dateCal.setTime(date);
	Calendar otherDateCal = Calendar.getInstance();
	otherDateCal.setTime(otherDate);

	switch (withUnit) {
	case Calendar.YEAR:
		dateCal.clear(Calendar.MONTH);
		otherDateCal.clear(Calendar.MONTH);
	case Calendar.MONTH:
		dateCal.set(Calendar.DATE, 1);
		otherDateCal.set(Calendar.DATE, 1);
	case Calendar.DATE:
		dateCal.set(Calendar.HOUR_OF_DAY, 0);
		otherDateCal.set(Calendar.HOUR_OF_DAY, 0);
	case Calendar.HOUR:
		dateCal.clear(Calendar.MINUTE);
		otherDateCal.clear(Calendar.MINUTE);
	case Calendar.MINUTE:
		dateCal.clear(Calendar.SECOND);
		otherDateCal.clear(Calendar.SECOND);
	case Calendar.SECOND:
		dateCal.clear(Calendar.MILLISECOND);
		otherDateCal.clear(Calendar.MILLISECOND);
	case Calendar.MILLISECOND:
		break;
	default:
		throw new IllegalArgumentException("withUnit 单位字段 " + withUnit + " 不合法!!");
	}
	return dateCal.compareTo(otherDateCal);
}
 
Example 12
Project: mumu   File: DateUtils.java   View Source Code Vote up 6 votes
/**
 * 根据单位字段比较两个时间
 * 
 * @param date
 *            时间1
 * @param otherDate
 *            时间2
 * @param withUnit
 *            单位字段,从Calendar field取值
 * @return 等于返回0值, 大于返回大于0的值 小于返回小于0的值
 */
public static int compareTime(Date date, Date otherDate, int withUnit) {
	Calendar dateCal = Calendar.getInstance();
	dateCal.setTime(date);
	Calendar otherDateCal = Calendar.getInstance();
	otherDateCal.setTime(otherDate);

	dateCal.clear(Calendar.YEAR);
	dateCal.clear(Calendar.MONTH);
	dateCal.set(Calendar.DATE, 1);
	otherDateCal.clear(Calendar.YEAR);
	otherDateCal.clear(Calendar.MONTH);
	otherDateCal.set(Calendar.DATE, 1);
	switch (withUnit) {
	case Calendar.HOUR:
		dateCal.clear(Calendar.MINUTE);
		otherDateCal.clear(Calendar.MINUTE);
	case Calendar.MINUTE:
		dateCal.clear(Calendar.SECOND);
		otherDateCal.clear(Calendar.SECOND);
	case Calendar.SECOND:
		dateCal.clear(Calendar.MILLISECOND);
		otherDateCal.clear(Calendar.MILLISECOND);
	case Calendar.MILLISECOND:
		break;
	default:
		throw new IllegalArgumentException("withUnit 单位字段 " + withUnit + " 不合法!!");
	}
	return dateCal.compareTo(otherDateCal);
}
 
Example 13
Project: myth   File: DateUtils.java   View Source Code Vote up 6 votes
/**
 * 比较两个日期大小(比较年月日时分秒),com1大于com2返回1,反之返回-1,相等则返回0
 *
 * @param date1
 * @param date2
 * @return
 */
public static int compareDateOfSecond(Date date1, Date date2) {
    Calendar c1 = Calendar.getInstance();
    c1.setTime(date1);
    c1.set(Calendar.MILLISECOND, 0);
    Calendar c2 = Calendar.getInstance();
    c2.setTime(date2);
    c2.set(Calendar.MILLISECOND, 0);
    return c1.compareTo(c2);

}
 
Example 14
Project: Remindy   File: TaskUtil.java   View Source Code Vote up 6 votes
public static Calendar getRepeatingReminderNextCalendar(RepeatingReminder repeatingReminder) {

        Calendar today = Calendar.getInstance();
        Calendar endDate = getRepeatingReminderEndCalendar(repeatingReminder);
        Calendar cal = CalendarUtil.getCalendarFromDateAndTime( repeatingReminder.getDate(), repeatingReminder.getTime());

        //TODO: Cant use getDateFieldFromRepeatType(), Gives off a weird warning
        //final int dateField = getDateFieldFromRepeatType(repeatingReminder.getRepeatType());


        while(true) {
            if (cal.compareTo(endDate) >= 0)    //If cal passed endDate, reminder is overdue, return null
                return null;
            if(cal.compareTo(today) >= 0) {
                return cal;
            }

            //TODO: Cant use getDateFieldFromRepeatType(), Gives off a weird warning
            //cal.add(dateField, repeatingReminder.getRepeatInterval()); break;
            switch (repeatingReminder.getRepeatType()) {
                case DAILY: cal.add(Calendar.DAY_OF_WEEK, repeatingReminder.getRepeatInterval()); break;
                case WEEKLY: cal.add(Calendar.WEEK_OF_YEAR, repeatingReminder.getRepeatInterval()); break;
                case MONTHLY: cal.add(Calendar.MONTH, repeatingReminder.getRepeatInterval()); break;
                case YEARLY: cal.add(Calendar.YEAR, repeatingReminder.getRepeatInterval()); break;
                default: throw new InvalidParameterException("Invalid RepeatType parameter in TaskUtil.getRepeatingReminderEndCalendar()");
            }
        }
    }
 
Example 15
Project: item-reaper   File: EditItemFragment.java   View Source Code Vote up 5 votes
private void expiryDateCustomSelected() {
    final Calendar today = Calendar.getInstance();
    final Calendar yesterday = Calendar.getInstance();
    yesterday.add(Calendar.DAY_OF_YEAR, -1);
    final Calendar calendar = Calendar.getInstance();
    int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
    int currentMonth = calendar.get(Calendar.MONTH);
    int currentYear = calendar.get(Calendar.YEAR);
    DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(),
            new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                    clearExpiryDateString();
                    calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
                    calendar.set(Calendar.MONTH, datePicker.getMonth());
                    calendar.set(Calendar.YEAR, datePicker.getYear());
                    mExpiryDate = calendar;
                    if (calendar.compareTo(today) == 0) {
                        mExpiryDateSpinner.setSelection(
                                mExpiryDateAdapter.getPosition(
                                        getString(R.string.edit_date_today)));
                    } else if (calendar.compareTo(yesterday) == 0) {
                        mExpiryDateSpinner.setSelection(
                                mExpiryDateAdapter.getPosition(
                                        getString(R.string.edit_date_yesterday)));
                    } else {
                        mExpiryDateString = getSimpleDateFormat().format(calendar.getTime());
                        mExpiryDateAdapter.insert(mExpiryDateString, 0);
                        mExpiryDateSpinner.setSelection(0);
                    }
                    if (mExpiryDateListener) {
                        mPresenter.itemChanged();
                    }
                }
            }, currentYear, currentMonth, currentDay);
    datePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialogInterface) {
            mExpiryDateSpinner.setSelection(mPreviousExpiryDateOption);
        }
    });
    datePickerDialog.show();
}
 
Example 16
Project: item-reaper   File: WidgetListService.java   View Source Code Vote up 5 votes
@Override
public RemoteViews getViewAt(int position) {
    RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
    if (position < mItems.size()) {
        String category = mItems.get(position).getCategory();
        if (category == null || category.equals("")) {
            rv.setTextViewText(R.id.category, getString(R.string.edit_category_empty));
        } else {
            rv.setTextViewText(R.id.category, category);
        }
        String type = mItems.get(position).getType();
        if (type == null || type.equals("")) {
            rv.setTextViewText(R.id.type, getString(R.string.edit_type_empty));
        } else {
            rv.setTextViewText(R.id.type, type);
        }
        String colour = mItems.get(position).getMainColour();
        if (colour == null || colour.equals("")) {
            rv.setTextViewText(R.id.colour, getString(R.string.edit_main_colour_empty));
        } else {
            rv.setTextViewText(R.id.colour, colour);
        }
        long expiry = mItems.get(position).getExpiry();
        if (expiry == -1) {
            setTextColourPrimary(rv);
            rv.setTextViewText(R.id.expiry, getString(R.string.edit_expiry_date_empty));
        } else {
            Calendar expiryDate = Calendar.getInstance();
            expiryDate.setTimeInMillis(expiry);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(getString(
                    R.string.edit_date_format),
                    Locale.ENGLISH);
            String expiryString = simpleDateFormat.format(expiryDate.getTime());
            rv.setTextViewText(R.id.expiry, expiryString);
            Calendar almostExpiredDate = Calendar.getInstance();
            almostExpiredDate.add(Calendar.MONTH, 1);
            if (expiryDate.compareTo(Calendar.getInstance()) < 1) {
                rv.setInt(R.id.expiry, "setTextColor", ContextCompat.getColor(mContext,
                        R.color.red));
            } else if (expiryDate.compareTo(almostExpiredDate) < 1) {
                rv.setInt(R.id.expiry, "setTextColor", ContextCompat.getColor(mContext,
                        R.color.orange));
            } else {
                setTextColourPrimary(rv);
            }
        }
        int price = mItems.get(position).getPricePaid();
        String priceString;
        if (price == -1) {
            priceString = getString(R.string.edit_price_paid_empty);
        } else {
            priceString = getString(R.string.edit_price_paid_prefix)
                    + getPriceFromTotalCents(price);
        }
        rv.setTextViewText(R.id.paid, priceString);

        Intent fillInIntent = new Intent();
        fillInIntent.putExtra(EXTRA_ITEM_ID, mItems.get(position).getId());
        rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
    }
    return rv;
}
 
Example 17
Project: item-reaper   File: ItemsFragment.java   View Source Code Vote up 5 votes
@Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            if (mLargeScreen) {
                String imageUrl = mItems.get(position).getImageUrl();
                if (imageUrl != null) {
                    if (!imageUrl.equals(mImageUrl)) {
                        mImageUrl = imageUrl;
                    }
                    holder.mItemImage.setVisibility(View.VISIBLE);
//                    EspressoIdlingResource.increment();
                    Glide.with(getContext())
                            .load(imageUrl)
                            .crossFade()
                            .centerCrop()
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .into(new GlideDrawableImageViewTarget(holder.mItemImage) {
                                @Override
                                public void onResourceReady(GlideDrawable resource,
                                                            GlideAnimation<? super GlideDrawable> animation) {
                                    super.onResourceReady(resource, animation);
//                                    EspressoIdlingResource.decrement();
                                }
                            });

                } else {
                    holder.mItemImage.setVisibility(View.GONE);
                }
            }
            String category = mItems.get(position).getCategory();
            if (category == null || category.equals("")) {
                holder.mCategory.setText(getString(R.string.edit_category_empty));
            } else {
                holder.mCategory.setText(category);
            }
            String type = mItems.get(position).getType();
            if (type == null || type.equals("")) {
                holder.mType.setText(getString(R.string.edit_type_empty));
            } else {
                holder.mType.setText(mItems.get(position).getType());
            }
            String colour = mItems.get(position).getMainColour();
            if (colour == null || colour.equals("")) {
                holder.mColour.setText(getString(R.string.edit_main_colour_empty));
            } else {
                holder.mColour.setText(mItems.get(position).getMainColour());
            }
            long expiry = mItems.get(position).getExpiry();
            if (expiry == -1) {
                setTextColourPrimary(holder);
                holder.mExpiry.setText(getString(R.string.edit_expiry_date_empty));
            } else {
                Calendar expiryDate = Calendar.getInstance();
                expiryDate.setTimeInMillis(expiry);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(getString(
                        R.string.edit_date_format), Locale.ENGLISH);
                String expiryString = simpleDateFormat.format(expiryDate.getTime());
                holder.mExpiry.setText(expiryString);
                Calendar almostExpiredDate = Calendar.getInstance();
                almostExpiredDate.add(Calendar.MONTH, 1);
                if (expiryDate.compareTo(Calendar.getInstance()) < 1) {
                    holder.mExpiry.setTextColor(ContextCompat.getColor(getContext(), R.color.red));
                } else if (expiryDate.compareTo(almostExpiredDate) < 1) {
                    holder.mExpiry.setTextColor(ContextCompat.getColor(getContext(), R.color.orange));
                } else {
                    setTextColourPrimary(holder);
                }
            }
            int price = mItems.get(position).getPricePaid();
            String priceString;
            if (price == -1) {
                priceString = getString(R.string.edit_price_paid_empty);
            } else {
                priceString = getString(R.string.edit_price_paid_prefix)
                        + getPriceFromTotalCents(price);
            }
            holder.mPaid.setText(priceString);
        }
 
Example 18
Project: item-reaper   File: EditItemFragment.java   View Source Code Vote up 5 votes
private void purchaseDateCustomSelected() {
    final Calendar today = Calendar.getInstance();
    final Calendar yesterday = Calendar.getInstance();
    yesterday.add(Calendar.DAY_OF_YEAR, -1);
    final Calendar calendar = Calendar.getInstance();
    int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
    int currentMonth = calendar.get(Calendar.MONTH);
    int currentYear = calendar.get(Calendar.YEAR);
    DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(),
            new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                    clearPurchaseDateString();
                    calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
                    calendar.set(Calendar.MONTH, datePicker.getMonth());
                    calendar.set(Calendar.YEAR, datePicker.getYear());
                    mPurchaseDate = calendar;
                    if (calendar.compareTo(today) == 0) {
                        mPurchaseDateSpinner.setSelection(
                                mPurchaseDateAdapter.getPosition(
                                        getString(R.string.edit_date_today)));
                    } else if (calendar.compareTo(yesterday) == 0) {
                        mPurchaseDateSpinner.setSelection(
                                mPurchaseDateAdapter.getPosition(
                                        getString(R.string.edit_date_yesterday)));
                    } else {
                        mPurchaseDateString = getSimpleDateFormat().format(calendar.getTime());
                        mPurchaseDateAdapter.insert(mPurchaseDateString, 0);
                        mPurchaseDateSpinner.setSelection(0);
                    }
                    if (mPurchaseDateListener) {
                        mPresenter.itemChanged();
                    }
                }
            }, currentYear, currentMonth, currentDay);
    datePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialogInterface) {
            mPurchaseDateSpinner.setSelection(mPreviousPurchaseDateOption);
        }
    });
    datePickerDialog.show();
}
 
Example 19
Project: Remindy   File: AlarmManagerUtil.java   View Source Code Vote up 5 votes
public static void updateAlarms(Context context) {
    Log.d(TAG, "Updating alarms.");

    List<Integer> triggeredTasks = SharedPreferenceUtil.getTriggeredTaskList(context);
    Log.d(TAG, "TriggeredTask IDs = " + TextUtils.join(",", triggeredTasks));

    //Get next task to trigger
    TaskTriggerViewModel task;
    try {
        task = new RemindyDAO(context).getNextTaskToTrigger(triggeredTasks);
    } catch (CouldNotGetDataException e ) {
        task = null;
    }

    if(task != null) {
        Log.d(TAG, "Got Task ID " + task.getTask().getId() + ". Title:" + task.getTask().getTitle());

        int triggerMinutesBeforeNotification = SharedPreferenceUtil.getTriggerMinutesBeforeNotification(context).getMinutes();

        Calendar triggerTime = Calendar.getInstance();
        CalendarUtil.copyCalendar(task.getTriggerDateWithTime(), triggerTime);
        triggerTime.add(Calendar.MINUTE, -triggerMinutesBeforeNotification);

        //Build intent
        Intent triggerIntent =  new Intent(context, TriggerTaskNotificationReceiver.class);
        triggerIntent.putExtra(TriggerTaskNotificationReceiver.TASK_ID_EXTRA, task.getTask().getId());

        if(triggerTime.compareTo(Calendar.getInstance()) <= 0) {
            //Trigger now
            Log.d(TAG, "Triggering now!");

            context.sendBroadcast(triggerIntent);
        } else {
            //Set Alarm
            Log.d(TAG, "Programming alarm for " + triggerTime.getTime());

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, triggerIntent, 0);

            AlarmManager manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            manager.set(AlarmManager.RTC_WAKEUP, triggerTime.getTimeInMillis(), pendingIntent);
        }
    }
    Toast.makeText(context, "Alarms updated!", Toast.LENGTH_SHORT).show();

}
 
Example 20
Project: Remindy   File: CalendarPeriod.java   View Source Code Vote up 5 votes
public boolean isInPeriod(Calendar date) {
    return (date.compareTo(start) >= 0 && date.compareTo(end) <= 0);
}