Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Calendar date to string?

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());    
like image 312
Smitha Avatar asked Sep 06 '11 09:09

Smitha


People also ask

How do I convert a date to a string in Python?

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.


2 Answers

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.

like image 97
Jon Skeet Avatar answered Oct 19 '22 10:10

Jon Skeet


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.

like image 39
Andrey Atapin Avatar answered Oct 19 '22 08:10

Andrey Atapin