java – Taking the first and last date of the previous month


I'm using Selenium in Eclipse to automate the sending of commands to a website, through JAVA files. On this site, I need to make an appointment for dates. I did it like this to test:

element = driver.findElement("form:dtEmissao_input"));
element = driver.findElement("form:emissFim_input"));

However, I wanted him to always get the first and last day of the previous month. How can I do this? Need to import some library into my JAVA file?


It doesn't need any external library! Here is a possible solution

public static voi main(String[] args){
    Calendar c = Calendar.getInstance();
    Date d = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    d = c.getTime();

    //subtrai 1 do mês atual para pegar o anterior
    c.add(Calendar.MONTH, -1);

    //seta primeiro dia do mês
    c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
    d = c.getTime();
    System.out.println("Data do primeiro dia do mes passado: " + sdf.format(d));
    //seta ultimo dia do mês
    c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
    d = c.getTime();
    System.out.println("Data do ultimo dia do mes passado: " + sdf.format(d));
Scroll to Top