【问题标题】:How to find expiry date from current and given date如何从当前日期和给定日期查找到期日期
【发布时间】:2012-01-03 16:54:41
【问题描述】:

我正在开发具有 15 天免费订阅期的应用程序,然后它将到期消息发送给用户。 我在登录时得到了到期日期(yyyy-MM-dd 格式),必须检查当前日期。

我尝试过使用两个日期对象,第一个对象具有当前日期,第二个对象具有到期日期。 但我只有在两个日期与DateTimeUtilities.isSameDate(date1, date2); 函数相同时才会得到 true,但只有在两个日期相同时才会返回 true。

请任何人帮助我解决我的问题。

【问题讨论】:

    标签: blackberry


    【解决方案1】:

    您可以通过以下方式找到答案

    SimpleDateFormat  formatter = new SimpleDateFormat("yyyy-MM-dd");//Your format type
    Date todayDate = new Date(HttpDateParser.parse(formatter.formatLocal(System.currentTimeMillis())));//It converts system date in your given format & store in todayDate object
    Date expiryDate = new Date(HttpDateParser.parse(expiryDateString));//It Store your expiry date in your expiryDate format
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(todayDate);
    cal2.setTime(expiryDate);
    
    if(cal1.before(cal2))
    {
    // Current date is less than your Expiry date
    }
    else
    {
    // Current date is equals and greater your Expiry date
    }
    

    【讨论】:

    • 你不觉得使用HttpDateParserCalendar 有点矫枉过正吗?无参数的Date 构造函数为您处理检索本地时间,您不需要Calendar 来比较两个Date 对象。
    【解决方案2】:

    您可以执行以下操作

    boolean expired = (currentDate.getTime()>expiryDate.getTime());
    if(expired)
        Status.show("Expired");
    

    【讨论】:

    • 这两个Date对象怎么填?
    • OP 已经尝试过 date1,date2 所以我假设他有两个日期对象
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    相关资源
    最近更新 更多