Convert calendar date to string?

I need to compare two dates when the first date is in calendar format and the other in string format (DD-MMM-yyyy). Therefore, I want to convert one of the calendar dates to String and use the compareTo method.

I tried using:

SimpleDateFormat formatter=new SimpleDateFormat("DD-MMM-yyyy"); String currentDate=formatter.format(view.getSelectedDay()); 
+6
source share
5 answers

Assuming view.getSelectedDay() returns a Calendar , you might just want:

 String currentDate = formatter.format(view.getSelectedDay().getTime()); 

(So ​​you have a Date link to go to format .)

If this is not a problem, provide additional information. I suspect you also want "dd" instead of "DD". "DD" is the day of the year, while "dd" is the day of the month, according to the documentation of SimpleDateFormat .

+9
source

here your problem has been resolved. In any case, why date comparisons through their string representations? Isn't it better to compare Date objects like here ? You can get a Date object with the getTime () method of the Calendar class.

+1
source

Do you compare values ​​to determine if one date is preceding another or for sorting? If you are located, you may encounter some sort of gotchas due to lexicographic sorting.

 String s = "12-11-2001"; String s2 = "13-11-2000"; int i = s.compareTo(s2); System.out.println(i); 

the output of this value is -1, where it must be 1, since s2 precedes s as DATE, but s2 is lexicographically after s when sorted in ascending order.

You might find it more reasonable to convert the string date to a Date object, and then use before () or after ().

+1
source

It would be best to compare Date objects or Calendar objects that I think. Very decomposed, it gives the following:

  • 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); 
  • Compare 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 the comparison will be < 0 if A < B , > 0 , if A > B and == 0 , if they are equal, according to the documentation of the Date or Calendar .

The only thing you need to be careful with is:

  • Time of day: if the Calendar rate is set on the same day as your String , but with a different hour this will not work (we compare the moments, here)

  • SimpleDateFormat template: it must match the format of your String , or you will have strange results

  • Locale: your dates may refer to the same moment, but be different if they are expressed in different time zones! If you need to handle this, you will have to take care of TimeZone when using Calendar (see the Calendar and TimeZone document for more details).

0
source

When comparing dates as strings, you should use SimpleDateFormat("yyyy-MM-dd") . Comparing them using the SimpleDateFormat("dd-MM-yyyy") format SimpleDateFormat("dd-MM-yyyy") will be an error in most cases because of the least significant number that will be checked first, and most importantly, the last. If you must use the dd-MM-yyyy format, then you can write a function that breaks the lines and then compares the year / month / day in the correct order and returns positive, negative, or 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
source

Source: https://habr.com/ru/post/896693/


All Articles