public static void main(String[] args) {
Map map = userIncrease(1);
}
/**
*
* @param flag 1 月 2:周 3:日
* @return
*/
public static Map userIncrease(Integer flag) {
Map map = new HashMap();
Calendar time = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
//调用方法,指定x轴参数
judge(map, flag, time);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return map;
}
private static Map judge(Map map, Integer flag, Calendar time) {
String[] month = {"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};
String[] week = { "星期天","星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
String[] hour = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24"};
String[] x = new String[24];
List list = new ArrayList();
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(s.format(time.getTime()));
if (flag == 1) {
int len = time.get(Calendar.MONTH) + 1;
for (int i = 0; i <= len; i++) {
x[i] = month[i];
Calendar date = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
date.set(Calendar.MONTH,i-1);
//加了这个数据就错了,注意避免
if(date.get(Calendar.MONTH) > 0){
}
updateTime(date,2);
System.out.println(s.format(date.getTime()));
list.add(s.format(date.getTime()));
}
} else if (flag == 2) {
int len = time.get(Calendar.DAY_OF_WEEK);
for (int i = 0; i <= len; i++) {
x[i] = week[i];
Calendar date = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
updateTime(date,0);
date.set(Calendar.DAY_OF_MONTH,time.get(Calendar.DAY_OF_MONTH)-len+i);
list.add(s.format(date.getTime()));
}
} else if (flag == 3) {
int len = time.get(Calendar.HOUR_OF_DAY);
for (int i = 0; i < len; i++) {
x[i] = hour[i];
}
}
map.put("x",x);
map.put("list",list);
return map;
}
/**
* 修改时间
*
* @param time 当前时间
* @param count 判断修改数据参数 0:修改时分秒为0 1:修改月份-1和修改天数为1 2:修改天数为1 3:修改天数-1
* @return
*/
private static Calendar updateTime(Calendar time, int count) {
//count等于1,修改时间为上个月的第一天,如果count不为1,就修改时间为当前月的第一天
if (count == 1) {
//把月份-1
time.set(Calendar.MONTH, time.get(Calendar.MONTH) - 1);
//设置天
time.set(Calendar.DAY_OF_MONTH, 1);
}
if (count == 2) {
//设置天
time.set(Calendar.DAY_OF_MONTH, 1);
}
if (count == 3){
//设置天数-1
time.set(Calendar.DAY_OF_MONTH,Calendar.DAY_OF_MONTH-1);
}
//设置小时
time.set(Calendar.HOUR_OF_DAY, 00);
//设置分钟
time.set(Calendar.MINUTE, 00);
//设置秒
time.set(Calendar.SECOND, 00);
return time;
}
上面代码是我学Calendar时候写的,中间有一个BUG,不知道为什么会有
当这个条件判断有的时候,数据就会出错,输出结果:
当我注释了之后的输出结果:
找了很久都没有找到错哪了,如果有大佬知道的话,欢迎指教!!!