简体   繁体   English

我如何检查期间(月)之间的2个日期

[英]How can i check that 2 dates between period(month)

i have 2 Date ( start_date , end_date ) with yyyy-MM-dd format and i want to check that if the month between in december 01 - march 31 then do something. 我有2个日期( start_dateend_date ),格式为yyyy-MM-dd,我想检查一下是否在12月1日至3月31日之间的月份进行操作。 For example my start_date is 2015-12-01 or 2016-02-01 and end_date 2016-02-12 then write something. 例如,我的开始start_date是2015-12-01或2016-02-01, end_date 2016-02-12,然​​后写点东西。 I have this 我有这个

public void meethod(){
     DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
     Date startDate = null;
     Date endDate = null;

     try {
         startDate = df.parse(tf_start_date.getText());
         endDate = df.parse(tf_end_date.getText());
     } catch (ParseException e) {
         e.printStackTrace();
     }

     if( /* what goes here? */ ) {
        System.out.println("its between december-march");
     } else {

     }
 } 

tf_start_date and tf_end_date is a TextField and the value of TextFields like 2015-02-03 tf_start_datetf_end_date是一个TextField,其值类似于2015-02-03

First, you should probably be using Calendar rather than Date . 首先,您可能应该使用Calendar而不是Date First, you'll need to construct the Calendar using a Calendar.Builder : 首先,你需要构建Calendar使用Calendar.Builder

Calendar startCal = Calendar.Builder().setInstant(startDate);
Calendar endCal = Calendar.Builder().setInstant(endDate);

Then you can just check their months to see if they are one of the months you're looking for: 然后,您可以检查他们的月份,看看他们是否是您要查找的月份之一:

int startMonth = startCal.get(Calendar.MONTH);
if (startMonth == Calendar.DECEMBER ||
    startMonth == Calendar.JANUARY ||
    startMonth == Calendar.FEBRUARY ||
    startMonth == Calendar.MARCH)

Similarly with endMonth . endMonth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM