您的位置:首页 > 编程语言 > Java开发

java 中关于日历的一些方法

2016-06-12 16:12 459 查看
获取当月的所有天http://www.cnblogs.com/maowang1991/archive/2012/12/29/2838919.html

private List<String> getDays() {
List<String> list = new ArrayList<>();
GregorianCalendar now = new GregorianCalendar();
int month = now.get(Calendar.MONTH);
int today = now.get(Calendar.DAY_OF_MONTH);
Log.d("Debug","当月:"+month+",当天:"+today);
int intent = 0;
now.set(Calendar.DAY_OF_MONTH, 1);
int week = now.get(Calendar.DAY_OF_WEEK);
int firstDayOfWeek = now.getFirstDayOfWeek();
while (firstDayOfWeek != week) {
++intent;
now.add(Calendar.DAY_OF_MONTH, -1);
week = now.get(Calendar.DAY_OF_WEEK);
}
String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
do {
System.out.printf("%4s", weekdayNames[week]);
now.add(Calendar.DAY_OF_MONTH, 1);
week = now.get(Calendar.DAY_OF_WEEK);
} while (week != firstDayOfWeek);
System.out.println();
Log.d("debug","这个月改缺少:"+intent+"天");
for (int i = 0; i < intent; ++i) {
System.out.print("    ");
}

now.set(Calendar.DAY_OF_MONTH, 1);
week = now.get(Calendar.DAY_OF_WEEK);
int day = now.get(Calendar.DAY_OF_MONTH);
do {
System.out.printf("%3s", day);
list.add(day + "");
if (today == day) {
System.out.print("*");
} else {
System.out.print(" ");
}
now.add(Calendar.DAY_OF_MONTH, 1);
week = now.get(Calendar.DAY_OF_WEEK);
day = now.get(Calendar.DAY_OF_MONTH);
if (week == firstDayOfWeek) {
System.out.println();
}
} while (month == now.get(Calendar.MONTH));
if (week != firstDayOfWeek) {
System.out.println();
}
return list;
}


根据年月日获取指定月份的所有天

private void  sss(){
String date = "2016-06-12";
int[] daysInMonth = getMonthDays(date,0);
Log.d("Debug","日期的长度:"+daysInMonth.length);
for(int value: daysInMonth){
Log.d("Debug",value + " ");
}
}

private static int[] getMonthDays(String date,int deleOrAdd) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, Integer.parseInt(date.substring(0, 4)));
calendar.set(Calendar.MONTH, Integer.parseInt(date.substring(5, 7)) -1+deleOrAdd);
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int[] days = new int[maxDay];
for(int i = 1; i <= maxDay; i++){
days[i-1] = i;
}
return days;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: