基础了解
Calendar 的 month 从 0 开始,也就是全年 12 个月由 0 ~ 11 进行表示。
Calendar的星期从星期天开始它的Day_OF_WEEK如下所示。
而 Calendar.DAY_OF_WEEK 定义和值如下:
Calendar.SUNDAY = 1
Calendar.MONDAY = 2
Calendar.TUESDAY = 3
Calendar.WEDNESDAY = 4
Calendar.THURSDAY = 5
Calendar.FRIDAY = 6
Calendar.SATURDAY = 7
//获得一天的开始合结束时间
private static String getDayStartAndEnd(){SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar currentDate = Calendar.getInstance();
currentDate.set(Calendar.HOUR_OF_DAY, 0);
currentDate.set(Calendar.MINUTE, 0);
currentDate.set(Calendar.SECOND, 0);
String dayOfstart = simpleDateFormat.format(currentDate.getTime());
currentDate.set(Calendar.HOUR_OF_DAY, 23);
currentDate.set(Calendar.MINUTE, 59);
currentDate.set(Calendar.SECOND, 59);
String dayOfend = simpleDateFormat.format(currentDate.getTime());
return dayOfstart+" "+dayOfend;
}
//获得月初和月末时间。
private static String getMonthStartDateAndEndDate(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
System.out.println(simpleDateFormat.format(calendar.getTime()));
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
calendar.add(Calendar.DATE, 1-dayOfMonth);
String startMonth = simpleDateFormat.format(calendar.getTime());
calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
calendar.add(Calendar.DATE, -dayOfMonth);
String endMonth = simpleDateFormat.format(calendar.getTime());
return startMonth+""+endMonth;
}
//今天和本周一相差的天数
private static int getModayPlus(){
Calendar calendar = Calendar.getInstance();
//获得今天是一周的第几天,星期天是第一天,星期一是第二天。
int week = calendar.get(Calendar.DAY_OF_WEEK);
if(week == 1){
return 6;
}
return week-1;
}
//获得一周的开始和接收2016-04-17 2016-04-24
private static String getWeekStartDateAndEndDate(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
int modayPlus = getModayPlus();
calendar.add(Calendar.DATE, -modayPlus);
String startDate = simpleDateFormat.format(calendar.getTime());
calendar.add(Calendar.DATE, 7);
String endDate = simpleDateFormat.format(calendar.getTime());
return startDate+" "+endDate;
}
对于世界格式转换的设定
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:ddd E a z");