Calendar
Calendar类是一个抽象类,不可以使用new关键字直接创建实例。
Calendar可以为在某一特定时刻和一组之间的转换的方法 calendar fields(日历域) 如YEAR , MONTH , DAY_OF_MONTH , HOUR 等等,以及用于操纵该日历字段,如获取的日期下个星期。
import java.util.Calendar;
/**
* 使用默认时区和区域设置获取日历
*/
public class TestCalendar {
public static void main(String[] args) {
Calendar ca = Calendar.getInstance();
// get:返回给定日历字段的值。
// YEAR:年份
int year = ca.get(Calendar.YEAR);
System.out.println(year);
// MONTH:月份
System.out.println(ca.get(Calendar.MONTH)+1);
// DATE:本月的日期
System.out.println(ca.get(Calendar.DATE));
// DAY_OF_MONTH:一周中的日期
System.out.println(ca.get(Calendar.DAY_OF_MONTH));
// 一个月的第几周
System.out.println(ca.get(Calendar.DAY_OF_WEEK_IN_MONTH));
// 一个月的第几天
System.out.println(ca.get(Calendar.DAY_OF_MONTH));
// HOUR_OF_DAY:当天的小时数
System.out.println(ca.get(Calendar.HOUR_OF_DAY));
// MINUTE:分
System.out.println(ca.get(Calendar.MINUTE));
// SECOND:秒
System.out.println(ca.get(Calendar.SECOND));
// 注意:静态成员和静态方法不要使用名称来调用,必须使用名称来调用。
System.out.println(ca.get(ca.SECOND));
// set:设置日历字段的值
// set(int field, nt value);
// field -> 给定的日历字段
// value -> 要为给定日历字段设置的值
ca.set(Calendar.YEAR, 2020);
Date time = ca.getTime();
System.out.println(time);
// set(int year, int month, int date, int hourOfDay, int minute, int second);
// year -> 用于设置 YEAR日历字段的值。
// month -> 用于设置MONTH日历字段的值。 月值为0。 例如,1月份为0。
// date -> 用于设置 DAY_OF_MONTH日历字段的值。
// hourOfDay -> 用于设置 HOUR_OF_DAY日历字段的值。
// minute -> 用于设置 MINUTE日历字段的值。
// second -> 用于设置 SECOND日历字段的值。
}
}
1.获取当月时间范围
public static String getMonth(){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH,1);
String first = format.format(c.getTime());
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last = format.format(ca.getTime());
return first+" and "+last;
}
输出:
public static void main(String[] args) {
System.out.println(getMonth()); // 2019-01-01 and 2019-01-31
}
2.获取去年当月时间范围
public static String getMonthLastYear(){
SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
Calendar lastYearStart = Calendar.getInstance();
lastYearStart.add(Calendar.YEAR, -1);
lastYearStart.add(Calendar.MONTH, 0);
lastYearStart.set(Calendar.DAY_OF_MONTH,1);
String firstDayLastYear = format.format(lastYearStart.getTime());
Calendar lastYearEnd = Calendar.getInstance();
lastYearEnd.add(Calendar.YEAR, -1);
lastYearEnd.add(Calendar.MONTH, 0);
lastYearEnd.set(Calendar.DAY_OF_MONTH, lastYearEnd.getActualMaximum(Calendar.DAY_OF_MONTH));
String endDayLastYear = format.format(lastYearEnd.getTime());
return firstDayLastYear+" and "+endDayLastYear;
}
输出:
public static void main(String[] args) {
System.out.println(getMonthLastYear());// 20180101 and 20180131
}
3. 获取某年某月时间范围
public static String getMonthAround(String date){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM");
SimpleDateFormat format2=new SimpleDateFormat("yyyy-MM-dd");
Date monthDate = null;
try {
monthDate = format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar lastYearStart = Calendar.getInstance();
lastYearStart.setTime(monthDate);
lastYearStart.set(Calendar.DAY_OF_MONTH,1);
String firstDayLastYear = format2.format(lastYearStart.getTime());
Calendar lastYearEnd = Calendar.getInstance();
lastYearEnd.setTime(monthDate);
lastYearEnd.set(Calendar.DAY_OF_MONTH, lastYearEnd.getActualMaximum(Calendar.DAY_OF_MONTH));
String endDayLastYear = format2.format(lastYearEnd.getTime());
return firstDayLastYear+" and "+endDayLastYear;
}
输出:
public static void main(String[] args) {
System.out.println(getMonthAround("2019-06"));// 2019-06-01 and 2019-06-30
}
4.获取某日去年的时间,如果是闰年二月29号,前一年自动变成28号
public static String getDateDayLastYear(String dateStr){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.YEAR, -1);
cal.add(Calendar.MONTH, 0);
String lastDay = format.format(cal.getTime());
return lastDay;
}
输出:
public static void main(String[] args) {
System.out.println(getDateDayLastYear("2019-2-28"));// 2018-02-28
}
5.计算两个日期之间所包含的天数
public static int differentDays(String startDate,String endDate){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date2 = null;
Date date1 = null;
try {
date1 = format.parse(startDate);
date2 = format.parse(endDate);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
int day1= cal1.get(Calendar.DAY_OF_YEAR);
int day2 = cal2.get(Calendar.DAY_OF_YEAR);
int year1 = cal1.get(Calendar.YEAR);
int year2 = cal2.get(Calendar.YEAR);
if(year1 != year2) {
int timeDistance = 0 ;
for(int i = year1 ; i < year2 ; i ++) {
//闰年
if(i%4==0 && i%100!=0 || i%400==0){
timeDistance += 366;
} else{ //不是闰年
timeDistance += 365;
}
}
return timeDistance + (day2-day1)+1 ;
} else{ //同年
return day2-day1+1;
}
}
输出:
public static void main(String[] args) {
System.out.println(differentDays("2019-2-26","2019-2-28"));// 3
}
6.获取上个月的第一天和最后一天
public static String getLastMonth(){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
Calendar calendar1=Calendar.getInstance();
int month=calendar1.get(Calendar.MONTH);
calendar1.set(Calendar.MONTH, month-1);
calendar1.set(Calendar.DAY_OF_MONTH,
calendar1.getActualMaximum(Calendar.DAY_OF_MONTH));
String firstDay = format.format(calendar.getTime());
String lastDay = format.format(calendar1.getTime());
return firstDay+" and "+lastDay;
}
输出:
public static void main(String[] args) {
System.out.println(getLastMonth());// 2018-12-01 and 2018-12-31
}
7.获取某个时间的当周时间范围
public static String getWeek(String date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date time = null;
try {
time = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(time);
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
cal.setFirstDayOfWeek(Calendar.MONDAY);
int day = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
String imptimeBegin = sdf.format(cal.getTime());
cal.add(Calendar.DATE, 6);
String imptimeEnd = sdf.format(cal.getTime());
return imptimeBegin + " and " + imptimeEnd;
}
输出:
public static void main(String[] args) {
System.out.println(getWeek("2019-01-23"));// 2019-01-21 and 2019-01-27
}
8.指定日期加上天数后的日期
// @param num 为增加的天数
// @param newDate 创建时间
public static String plusDay(int num, String newDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dt = null;
try {
dt = sdf.parse(newDate);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt);
rightNow.add(Calendar.DAY_OF_YEAR, num);
Date dt1 = rightNow.getTime();
String reStr = sdf.format(dt1);
return reStr;
}
输出:
public static void main(String[] args) {
System.out.println(plusDay(2,"2019-01-23"));//2019-01-25
}
9.指定日期加上月数后的日期
public static String plusMonth(int num, String newDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dt = null;
try {
dt = sdf.parse(newDate);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt);
rightNow.add(Calendar.MONTH,num);//日期加3个月
Date dt1 = rightNow.getTime();
String reStr = sdf.format(dt1);
return reStr;
}
输出:
public static void main(String[] args) {
System.out.println(plusMonth(12,"2004-02-29"));//2005-02-28
}
10.获取上个月的今天
public static String getFirstDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cl = Calendar.getInstance();
cl.add(Calendar.MONTH, -1);
Date dateFrom = cl.getTime();
return sdf.format(dateFrom);
}
输出:
public static void main(String[] args) {
System.out.println(getFirstDate());//2018-12-23
}
11.获取某天的下一天
public static String getNextDate(String newDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dt = null;
try {
dt = sdf.parse(newDate);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt);
rightNow.add(Calendar.DATE,1);//日期加1天
Date dt1 = rightNow.getTime();
String reStr = sdf.format(dt1);
return reStr;
}
输出:
public static void main(String[] args) {
System.out.println(getNextDate("2019-02-20"));//2019-02-21
}