验证两个日期

时间:2013-12-18 18:58:31

标签: java android

请我是java的新手,我按照Android应用程序的预订系统教程,我希望以这样的方式验证我的returnDate应该是> = departDate。我尝试但它继续显示错误,我不想搞砸一切。请帮忙原谅我这个问题:

private void setDateForLayout(Calendar c, boolean isDepartLayout) {
    if (isDepartLayout) {
        departYear = c.get(Calendar.YEAR);
        departMonth = c.get(Calendar.MONTH);
        departDate = c.get(Calendar.DAY_OF_MONTH);
        departDay = c.get(Calendar.DAY_OF_WEEK);

        if (departDate < 10) {
            lblDepartDate.setText("0" + departDate);
            lblDepartDateOneWay.setText("0" + departDate);
        } else {
            lblDepartDate.setText("" + departDate);
            lblDepartDateOneWay.setText("" + departDate);
        }

        lblDepartDay.setText(days[departDay - 1]);
        lblDepartDayOneWay.setText(days[departDay - 1]);

        lblDepartMonth.setText(months[departMonth]);
        lblDepartMonthOneWay.setText(months[departMonth]);

    } else {
        returnYear = c.get(Calendar.YEAR);
        returnMonth = c.get(Calendar.MONTH);
        returnDate = c.get(Calendar.DAY_OF_MONTH);
        returnDay = c.get(Calendar.DAY_OF_WEEK);

        if (returnDate < 10) {
            lblReturnDate.setText("0" + returnDate);
        } else {
            lblReturnDate.setText("" + returnDate);
        }
        lblReturnDay.setText(days[returnDay - 1]);
        lblReturnMonth.setText(months[returnMonth]);
    }
}

private String[] getListDateBeforeAndAfter() {
    String[] result = new String[7];
    for (int i = -3; i < 4; i++) {
        GregorianCalendar calendar = new GregorianCalendar(departYear,
                departMonth, departDate);
        calendar.add(Calendar.DAY_OF_MONTH, i);

        result[i + 3] = calendar.get(Calendar.DATE) + "-"
                + months[calendar.get(Calendar.MONTH)];
    }
    return result;
}

private void setBackgroundTab(boolean tripType) {
    if (this.tripType != tripType) {
        this.tripType = tripType;
        if (tripType == TripType.ROUND_TRIP) {
            layoutSelectTabRoudTrip.setVisibility(View.VISIBLE);
            layoutSelectTabOneWay.setVisibility(View.GONE);
        } else {
            layoutSelectTabRoudTrip.setVisibility(View.GONE);
            layoutSelectTabOneWay.setVisibility(View.VISIBLE);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

使用after对象的Calendar功能。

http://www.tutorialspoint.com/java/util/calendar_after.htm

答案 1 :(得分:0)

Joda-Time 2.3有测试前后的方法。

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;

DateTimeZone parisTimeZone = DateTimeZone.forID( "Europe/Paris" );

DateTime januaryDateTime = new DateTime( 2013, 1, 2, 3, 4, 5, parisTimeZone );
DateTime decemberDateTime  = new DateTime( 2013, 12, 13, 14, 15, 16, parisTimeZone );

Boolean isDecemberAfterJanuary = decemberDateTime.isAfter( januaryDateTime );
Boolean isDecemberBeforeJanuary = decemberDateTime.isBefore( januaryDateTime );

System.out.println( "isDecemberAfterJanuary: " + isDecemberAfterJanuary );
System.out.println( "isDecemberBeforeJanuary: " + isDecemberBeforeJanuary );

跑步时......

isDecemberAfterJanuary: true
isDecemberBeforeJanuary: false