DatePicker - both for current/future dates - Compareto method displays the value 1 | Selenium Forum
M
Manikanta Posted on 31/07/2019

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DatePicker {


	public static void main(String[] args) {
		// TODO Auto-generated method stub

		datapicker("31/07/2017");
		datapicker("31/07/2019");
		datapicker("31/07/2021");		
	}
	
	public static void datapicker(String date) {		
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");				
			Date dateToBeSelected;
			try {
				dateToBeSelected = sdf.parse(date);
				Date currentdate = new Date();
				
				System.out.println(currentdate.compareTo(dateToBeSelected));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
					
	}	

}

A
Ashish Thakur Replied on 31/07/2019

Please note that compareTo() function returns the data in -1, 0 and 1, which represent few things as shown below on the java doc.

Responsive image

public static void datapicker(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		Date dateToBeSelected;
		try {
			dateToBeSelected = sdf.parse(date);
			Date currentdate = new Date();
			System.out.println("Date to be selected is " + dateToBeSelected);
			System.out.println("Current date is " + currentdate);
			if (0 == currentdate.compareTo(dateToBeSelected)) {
				System.out.println("The date and time matches");
			} else if (0 > currentdate.compareTo(dateToBeSelected)) {
				System.out.println("The date is greater than the current date and time");
			} else {
				System.out.println("The date is lesser than current date and time");
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

 


M
Manikanta Replied on 01/08/2019

Can u please check for the value 0 if the argument Date is equal to this Date

its not working

 

 


A
Ashish Thakur Replied on 02/08/2019

Please run the code I've sent you. This will make everything clear to you.