I have to compare two dates where first date is in Calendar format and other in string(DD-MMM-yyyy) format. So I want to convert one of the Calendar dates into String and use compareTo method.
I have tried using :
SimpleDateFormat formatter=new SimpleDateFormat("DD-MMM-yyyy");
String currentDate=formatter.format(view.getSelectedDay());
To convert Python datetime to string, use the strftime() function. The strftime() method is a built-in Python method that returns the string representing date and time using date, time, or datetime object.
Assuming view.getSelectedDay()
returns a Calendar
, you may simply want:
String currentDate = formatter.format(view.getSelectedDay().getTime());
(So that you have a Date
reference to pass in to format
.)
If that's not the problem, please give more information. I suspect you also want "dd" instead of "DD" by the way. "DD" is the day of year whereas "dd" is the day of month, as per the SimpleDateFormat
documentation.
here's your problem having been solved. Anyway, why comparing dates through their string representations? Wouldn't it be better to compare Date objects like here? You can get Date object with getTime() method of Calendar class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With