package com.example.demo; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; @RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests { @Test
public void contextLoads() { setMonth("2018年02月"); } private Date str2Date(String str) {
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月");
return df.parse(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* 设置月份
*/
private void setMonth(String Month) { Date month = str2Date(Month);
boolean isCurrentMonth = false;
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
//获取今天是多少号
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
int todayWeekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1; Date cM = str2Date(getMonthStr(new Date()));
//判断是否为当月
if (cM.getTime() == month.getTime()) {
isCurrentMonth = true;
int selectDay = currentDay;//当月默认选中当前日
} else {
isCurrentMonth = false;
int selectDay = 0;
}
System.out.println("设置月份:" + getMonthStr(month) + " 今天" + currentDay + "号, 是否为当前月:" + isCurrentMonth);
calendar.setTime(month);
int dayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
//第一行1号显示在什么位置(星期几)
int firstIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int lineNum = 1;
//第一行能展示的天数
int firstLineNum = 7 - firstIndex;
int lastLineNum = 0;
int shengyu = dayOfMonth - firstLineNum;
while (shengyu > 7) {
lineNum++;
shengyu -= 7;
}
if (shengyu > 0) {
lineNum++;
lastLineNum = shengyu;
}
System.out.println(getMonthStr(month) + "月一共有" + dayOfMonth + "天,第一天的索引是:" + firstIndex + " 有" + lineNum +
"行,第一行" + firstLineNum + "个,最后一行" + lastLineNum + "个"); int days[][] = new int[5][7]; String weeks[] = {"星期-", "星期二", "星期三", "星期四", "星期五"}; for (int day = 0; day < dayOfMonth; day++) { int column = (day + firstIndex) % 7;
int row = (day + firstIndex) / 7;
days[row][column] = day + 1;
} for (int i = 0; i < weeks.length; i++) { System.out.print(weeks[i] + "\t"); }
System.out.println();
for (int i = 0; i < days.length; i++) {
for (int j = 0; j < days[i].length; j++) {
if (days[i][j] == 0)
System.out.print("\t");
else
System.out.print(days[i][j] + "\t");
} System.out.println();
} } /**
* 获取月份标题
*/
private String getMonthStr(Date month) {
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月");
return df.format(month);
} }