开发者

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());    


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.


Are you comparing the values to determine whether one date is prior to another, or for sorting? If you are you may run afoul of some sort order gotchas due to lexicographical sorting.

String s  = "12-11-2001";
String s2 = "13-11-2000";

int i = s.compareTo(s2);

System.out.println(i);

the output of this is -1, where it should be 1, as s2 as a DATE is prior to s, but s2 is lexicographically after s when sorted in ascending order.

You may find it more sensible to convert your string date to a Date object, and then use before() or after().


The best would be to compare Date objects or Calendar objects I think. Very decomposed, it gives this:

  • Comparing Date objects

    final Calendar calendarDate = your_date_as_a_Calendar;
    
    final String stringDate = your_date_as_a_String;
    final SimpleDateFormat format = new SimpleDateFormat("DD-MMM-yyyy");
    
    final Date dateA = calendarDate.getTime(); // this gives the absolute time, that actually embeds the date!
    final Date dateB = format.parse(stringDate);
    
    final int comparison = dateA.compareTo(dateB);
    
  • Comparing Calendar objects

    final Calendar calendarA = your_date_as_a_Calendar;
    
    final String stringDate = your_date_as_a_String;
    final SimpleDateFormat format = new SimpleDateFormat("DD-MMM-yyyy");
    
    final Calendar calendarB = new GregorianCalendar();
    calendarB.setTime(format.parse(stringDate));
    
    final int comparison = calendarA.compareTo(calendarB);
    

And then comparison will be < 0 if A < B, > 0 if A > B, and == 0 if they are equal, according to the documentation of Date or of Calendar.

The only thing you have to be careful with are:

  • The time of the day: should your Calendar bet set to the same day as your String, but with a different hour this will not work (we are comparing instants, here)

  • The SimpleDateFormat's pattern: it should match the format of your String, or you will have strange results

  • The locale: your dates may refer to the same instant but be different if they are expressed in different time zones! Should you have to handle that you will have to take care of the TimeZone when using Calendar (see the doc of Calendar and of TimeZone for further details).


When comparing dates as strings you should use SimpleDateFormat("yyyy-MM-dd"). Comparing them using SimpleDateFormat("dd-MM-yyyy") format will be wrong most of the time due to the least significant number being checked first and the most significant being checked last.
If you must use the dd-MM-yyyy format then you could write a function that splits the strings then compares the Year/Month/Day in the correct order and returns positive, negative, or a zero.

// Compares first date to second date and returns an integer
// can be used in a similar manner as String.CompareTo()
Public Static int CompareDates(String Date1, String Date2) {
    String[] splitDate1 = Date1.split("-");
    String[] splitDate2 = Date2.split("-");
    int ret = -1;

    if (splitDate1[2].CompareTo(splitDate2[2]) == 0) {
        if (spliDatet1[1].CompareTo(splitDate2[1]) == 0) {
            if (splitDate1[0].CompareTo(splitDate2[0]) == 0) {
                ret = 0;
            }
            else if (splitDate1[0].CompareTo(splitDate2[0]) > 0) {
                ret = 1;
            }
        }
        else if (splitDate1[1].CompareTo(splitDate2[1]) > 0) {
            ret = 1;
        }
    }
    else if (splitDate1[2].CompareTo(splitDate2[2]) > 0) {
        ret = 1;
    }
    Return ret;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜