-4

I need to find wednesday for the two dates given by the user. example: Inputs are: from date:07-Feb-2013 To date:13-feb-2013 The gap between the from date and To date is 7 days always. Expected Output:12-feb-2013

public String getAutoDayExpiryDateAndToDate(String instrmentId,String deliveryAutoFromDate)
    throws SystemException, FunctionalException,ParseException
    {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(deliveryAutoFromDate));
        Date fromDate=calendar.getTime();
        SimpleDateFormat sf1 = new SimpleDateFormat("dd-MMM-yyyy");
        String formatedDate = sf1.format(fromDate);
        calendar.add(Calendar.WEEK_OF_MONTH, 1);
        calendar.add(Calendar.DAY_OF_WEEK,-1);
        Date time = calendar.getTime();
        SimpleDateFormat sf = new SimpleDateFormat("dd-MMM-yyyy");
        String formatedDate1 = sf.format(time);
    }

after these i need to find Wednesday which is present between formatedDate and formatedDate1 .how can i do it??

4

2 に答える 2

4

Calendarクラスを使用します。最初の日付に設定し、 を呼び出して現在の曜日が水曜日かどうかを確認しcalendar.get(Calendar.DAY_OF_WEEK)ます。このチェックをループで実行し、反復ごとに現在の日付に 1 日追加します。これには 7 つ以上の手順が必要になることはないため、それ以上の複雑なことを行う必要はありません。

于 2013-02-11T13:14:32.777 に答える
0

これは、指定された日付以降の (または等しい) 最初の水曜日を見つける必要があります

    GregorianCalendar c = new GregorianCalendar(2013, 1, 7);
    if (c.get(Calendar.DAY_OF_WEEK) <= Calendar.WEDNESDAY) {
        c.add(Calendar.DAY_OF_YEAR, Calendar.WEDNESDAY - c.get(Calendar.DAY_OF_WEEK));
    } else {
        c.add(Calendar.DAY_OF_YEAR, 11 - c.get(Calendar.DAY_OF_WEEK));          
    }
    System.out.println(c.getTime());

版画

Wed Feb 13 00:00:00 EET 2013

必要なものが得られるかどうかをテストできます

于 2013-02-11T15:18:36.190 に答える