java – 如何从当月开始订购月份名称

说这是用于存储月份的数组,

String months[] = {"January", "February", "March", "April",
            "May", "June", "July", "August", "September",
            "October", "November", "December"};

我用这种方法来获得当前月份

int monthName=calendar.get(Calendar.MONTH);

从当前月份开始,将数组重新排序为新数组或arraylist的最佳方法是什么.

例如:当前月份是八月,所以所需的阵列或Arraylist将是

months[] = {"August", "September", "October", "November",
            "December", "January", "February", "March", "April",
            "May", "June", "July"};
最佳答案
您可以使用Collections.rotate.要向左旋转,您可以使用元素的负值.

String months[] = {"January", "February", "March", "April",
        "May", "June", "July", "August", "September",
        "October", "November", "December"};

int monthIndex = LocalDate.now().getMonth().ordinal();//indexed from 0

Collections.rotate(Arrays.asList(months), -monthIndex); //negative value rotates to left

System.out.println(Arrays.toString(months));

产出:[8月,9月,10月,11月,12月,1月,2月,3月,4月,5月,6月,7月]

转载注明原文:java – 如何从当月开始订购月份名称 - 代码日志