Home » excel » java – Sort data based on date in excel

java – Sort data based on date in excel

Posted by: admin May 14, 2020 Leave a comment


Through some of the blogs (just one among them https://blog.cometdocs.com/how-to-sort-data-by-date-in-excel), I understood that to sort a Date column in excel, month should be in the integer format not the text format ( eg: jan or feb).
Am I correct?

By default ( unless you change in the system) the date format will be always mm/dd/yyyy? Is there any doc supporting this?

How to&Answers:

The confusion comes from formatting Excel applies to a cell that will convert a number to look like a text date and date that is entered as text. visually there is no way to distinguish.

You can test the cell by say =ISNUMBER(cell) or =ISTEXT(cell) you can determine if its a date stored as a number but displayed as TEXT, or if it is just TEXT. An other test is changing the formatting for the cell to general and seeing if the cells displayed information changes in appearance.

If it is a number, you can simply sort the column and it will do so numerically. If it is not a number but text, it will sort the date alphabetically.

On a side note, the integer portion of the date represents number of days starting January 1st 1900 (on the mac this might be 1904). The decimal portion of the number represents time as a fraction of a day. 0.5 would be 12 noon.

As for the format, it will take your system settings for display purposes only. You can also apply a custom format to the excel cell for display purposes after loading. yy/mm/dd is the system I prefer to use. You have many options for formatting your custom display

yyyy - four digits of year
yy   - last 2 digits of year
m    - minimal digits for displaying month
mm   - leading zero included for single digit months
mmm  - 3 letters for month is displayed as text
mmmm - full name of month is displayed as text
d    - minimal digits for displaying day
dd   - leading zero included for single digit days
ddd  - 3 letters for day is displayed as text
dddd - full name of the day is displayed as text