0

I have a date which is coming in the form of 21,23,24,25.....

What I want is to subtract 4 days from current day of the month.

My attempt is reported below but it is giving me dates in the form of dd/mm/yyyy. I just want the day.

Calendar cal=Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -4);
Date curdteMinusFourDays=cal.getTime();
String curdteMinusFourDaysStr=curdteMinusFourDays.toString();
System.out.println(curdteMinusFourDays);

tripDate.click();

List<WebElement> listofdates=CommonBrowserSetup.driver.findElements(By.tagName("td"));
for (int i=1;i<=listofdates.size();i++){
    System.out.println(listofdates.get(i).getText());
    String d1=listofdates.get(i).getText();
    if(d1.contains(curdteMinusFourDaysStr)){
        listofdates.get(i).click();
        break;
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Joe
  • 43
  • 1
  • 3
  • 7
  • After click on date, use `.getAttribute("value")` then only you will be able to get `day` not `date`. For more details on this refer this answer. https://stackoverflow.com/questions/42570128/how-to-capture-and-assert-the-value-populated-via-a-datepicker-using-selenium/42571151#42571151 – Jainish Kapadia Mar 30 '17 at 09:22
  • 1
    The `Calendar` class is very old and long outmoded, `Date` even more so. Any particular reason why you are still using them? [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/), is much nicer to work with. Use `LocalDate.now(ZoneId.of("America/Winnipeg")).minusDays(4).getDayOfMonth()`. – Ole V.V. Dec 19 '17 at 09:41
  • An aside, by using `d1.contains()`, aren’t you risking false positives? If 4 days ago was the 2nd, your condition will hold true for 12, 20, etc., too. – Ole V.V. Dec 19 '17 at 13:01

4 Answers4

1

Messages

  • Don’t use the long outmoded Calendar class for this. Use java.time, the modern Java date and time API.
  • Make a conscious choice of time zone and make it explicit.

Details

The gold-plated way of obtaining the day of month four days ago as a string is to use a formatter:

    DateTimeFormatter dayOfMonthFormatter = DateTimeFormatter.ofPattern("d");
    String day = LocalDate.now(ZoneId.of("America/Winnipeg"))
            .minusDays(4)
            .format(dayOfMonthFormatter);
    System.out.println(day);

Running this today (December 19) it prints 15.

I am using a format pattern string of d. This gives you strings 1, 2, 3, etc. If you need 01, 02, 03, use dd to get two-digit day-of-month with leading zero as needed.

Since getting today’s date is a time zone sensitive operation, decide in which time zone you want it, and make it explicit in your code. In other words, substitute your desired time zone if it doesn’t happen to be America/Winnipeg.

A bit more barebones it can also be done without the formatter, provided that you don’t need any formatting (like the leading zero):

    int dayInt = LocalDate.now(ZoneId.of("America/Winnipeg"))
            .minusDays(4)
            .getDayOfMonth();
    String day = String.valueOf(dayInt);

Getting familiar with DateTimeFormatter could be a good investment anyway, though.

java.time

The Calendar class is very old and long outmoded, Date even more so. java.time, the modern Java date and time API also known as JSR-310, is so much nicer to work with.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

you should do

System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); //after you have subtracted

for more variations have a look at https://stackoverflow.com/a/7226186/2551236

Community
  • 1
  • 1
Vihar
  • 3,626
  • 2
  • 24
  • 47
0

Try below code it will give result in form of day of the month

Ex: If today's date is 30 then below code will give output as 26.

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -4);
System.out.println(cal.get(Calendar.DAY_OF_MONTH));

Let me know if it helps you.

Akarsh
  • 967
  • 5
  • 9
0

just get the current day of month as an int. Then substract 4. If the result is negative, then you should add the number of days of the previous month.

import java.util.Calendar;

public class Main2 {

public static void main(String args[])
{


    Main2 m = new Main2();
    Calendar cal = Calendar.getInstance();
    int result;
    cal.set(Calendar.MONTH, Calendar.DECEMBER);
    cal.set(Calendar.DAY_OF_MONTH, 11);
    result = m.substractFromCurrentDay(cal, 4);
    System.out.println(result);//prints 7
    cal.set(Calendar.DAY_OF_MONTH, 1);
    result = m.substractFromCurrentDay(cal, 4);
    System.out.println(result);//prints 27
}

public int substractFromCurrentDay(Calendar cal, int daysToSubstract)
{
    int currentDay = cal.get(Calendar.DAY_OF_MONTH);
    int result;

    result=currentDay-daysToSubstract;
    if(result<=0)
    {
        cal.add(Calendar.MONTH, -1);//go to last month
        int lastMonthDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        result=result+lastMonthDays;
    }

    return result;
}
}
Newton fan 01
  • 484
  • 5
  • 14
  • Apart from using the long outmoded `Calendar` class aren’t you overcomplicating things? Is your answer accomplishing anything that the other answers don’t? – Ole V.V. Dec 19 '17 at 09:42