Sorting a map by date key in Java

风流意气都作罢 提交于 2019-12-02 12:33:34

See http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html.

Use y for year, M for month in year, and d for day in month. Specifically, lowercase m is minute in hour, while uppercase M is month in year.

It looks like the lines

data = line.split(":"); map.put(df.parse(data[1]), line);

Are effectively parsing out only the month. Line.split(":") is going to produce an array split by : . If you have dates formatted in your data file dd:mm:yyyy then the resulting array "data" should be {[dd], [mm], [yyyy]}. So data[1] is going to simply be the month.

I could be wrong, but I suspect this is why you are only getting the 12 key-value pairs; you are parsing only for the month, and any time you get a new month key, you're overwriting the old key.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!