一:

//如果超过此时间,则不能进入程序

if (isExpiredLocal(2013, 7, 07)) {

Toast.makeText(this, "EXPIRED!", Toast.LENGTH_SHORT).show();
this.finish();(this为进入程序的Activity)
return;
}

 

/**
* 判断本机日期是否超过 指定的日期,
* 
* @param year
* @param month
* @param day
* @return
*/
public static boolean isExpiredLocal(int year, int month, int day) {
Calendar endtime = Calendar.getInstance();
endtime.set(year, month - 1, day);
Calendar currenttime = Calendar.getInstance();
return currenttime.after(endtime);
}

二:

//正常
private static final int TRIAL = 2;
//即将过期
private static final int EXPIRE_SOON= 1;
//过期
private static final int BOMB = 0;

// 时间炸弹
        if (isExpiredLocal(2) == TRIAL) {
            // 正常时的操作
        } else if (isExpiredLocal(2) == EXPIRE_SOON) {
            // 即将过期时的操作
        } else if (isExpiredLocal(2) == BOMB) {
            // 过期时的操作
        }


/**
     * 判断本机日期是否超过 指定的日期,
     * 
     * @return
     */
    public int isExpiredLocal(int months) {
        // 创建将要写入时间炸弹日期的文件路径
        String filePath = "/mnt/sdcard/Dreye";
        String dirPath = "/mnt/sdcard/Dreye/timeBomb.txt";
        // 如果文件不存在
        if (!new File(dirPath).exists()) {
            // 将时间炸弹的日期写入“timeBomb.txt”文件,参数2表示当前时期加上2个月之后的时间,即:两个月后
            String sdate = nDaysAftertoday(months);
            FileUtil.writeSd(filePath, dirPath, sdate);
        }
        // 从文件中读取时间炸弹的日期
        String timeBomb = FileUtil.readSd(dirPath);
        Date d = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("今天的日期:" + df.format(d));
        String currenttime = df.format(d);
        int betweenDays = nDaysBetweenTwoDate(currenttime, timeBomb);
        //即将到期
        if ((betweenDays < 10) & (betweenDays > 0)) {
            return EXPIRE_SOON;
        //到期
        } else if ((betweenDays == 0)|| (betweenDays < 0)) {
            return BOMB;
        }

//正常
        return TRIAL;
    }

    /**
     * 获取n个月后的日期
     * 
     * @param nMonths
     * @return yyyy-MM-dd
     */
    public String nDaysAftertoday(int nMonths) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", this
                .getResources().getConfiguration().locale);
        Calendar rightNow = Calendar.getInstance();
        // rightNow.add(Calendar.DAY_OF_MONTH,-1);
        rightNow.add(Calendar.MONTH, +nMonths);
        return df.format(rightNow.getTime());
    }

    /**
     * 计算两个日期相隔的天数
     * @param firstString
     * @param secondString
     * @return 相隔的天数
     */
    public int nDaysBetweenTwoDate(String firstString, String secondString) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date firstDate = null;
        Date secondDate = null;
        try {
            firstDate = df.parse(firstString);
            secondDate = df.parse(secondString);
        } catch (Exception e) {
            // 日期型字符串格式错误
        }

        int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
        return nDay;
    }