Calendar时间操作
2016-04-26 19:31
609 查看
基础了解
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");
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");
相关文章推荐
- java 面向对象编程(OOP)-个人理解
- 为什么 Python 不支持函数重载?
- vi编辑器的使用方法
- iOS Crash文件的解析(一)
- mysql修改编码格式
- 第六次作业
- ZOJ 3938-Defuse the Bomb【模拟,题看着挺长】(2016浙江省大学生程序设计竞赛)
- 【框架】RefreshListView下拉刷新
- 公式加括号的所有可能
- 给大家共享一个个人认为非常好的动画网站
- keras代码阅读-Activition层
- 设计模式:访问者模式(Visitor)
- BZOJ1179: [Apio2009]Atm
- 超客车,行驶学校路段
- span问题
- POJ 2739 Sum of Consecutive Prime Numbers(Two pointers)
- Java虚拟机如何加载Class文件
- HDU 1495 - 非常可乐
- python遍历数组的两种方法
- ZOJ 3936-Apples and Ideas【模拟】(2016浙江省大学生程序设计竞赛)