一直以来糊里糊涂的使用Calendar类
近期学习了下基本用法
calendar.add(Calendar.DAY_OF_MONTH, +1);
根据第二个int值的正负代表天数加一或减一
package com.cy.manage.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* author:gkq
* date:2020/9/25
*/
public class TimeUtil {
/**
* 获取某一天的下一天
*
* @param time
* @return
*/
public static String getNextDate(String time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(toData(time));
calendar.add(Calendar.DAY_OF_MONTH, +1);
Date date = calendar.getTime();
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd",
Locale.CHINA);
return sdr.format(date);
}
/**
* 获取某一天的上一天
*
* @param time
* @return
*/
public static String getLastDate(String time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(toData(time));
calendar.add(Calendar.DAY_OF_MONTH, -1);
Date date = calendar.getTime();
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd",
Locale.CHINA);
return sdr.format(date);
}
/**
* 获取当前日期
*
* @return
*/
public static String getToday() {
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd",
Locale.CHINA);
return sdr.format(date);
}
/**
* 获取某一月的上一月
* @param time
* @return
*/
public static String getLastMonth(String time) {
String date;
Calendar cal = Calendar.getInstance();
cal.setTime(toData(time));
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd",
Locale.CHINA);
cal.add(Calendar.MONTH, -1);
date = df.format(cal.getTime());
return date;
}
/**
* 获取某一月的下一月
* @param time
* @return
*/
public static String getNextMonth(String time) {
String date;
Calendar cal = Calendar.getInstance();
cal.setTime(toData(time));
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd",
Locale.CHINA);
cal.add(Calendar.MONTH, 1);
date = df.format(cal.getTime());
return date;
}
/**
* 获取当前月的第一天
*
* @return
*/
public static String getMonthFirstDay() {
String firstDay;
Calendar cal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd",
Locale.CHINA);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
firstDay = df.format(cal.getTime());
return firstDay;
}
/**
* 获取某月的最后一天
*
* @return
*/
public static String getMonthLastDay(String time) {
String date;
Calendar cal = Calendar.getInstance();
cal.setTime(toData(time));
int firstDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最小天数
cal.set(Calendar.DAY_OF_MONTH, firstDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
date = sdf.format(cal.getTime());
return date;
}
/**
* 获取当前周的周一
* @return
*/
public static String getWeekFirstDay() {
String firstDay;
Calendar curStartCal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd",
Locale.CHINA);
Calendar cal = (Calendar) curStartCal.clone();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
firstDay = df.format(cal.getTime());
return firstDay;
}
/**
* 配合getWeekFirstDay获得周末
* @return
*/
public static String getWeekLastDay(String time) {
String date;
Calendar cal = Calendar.getInstance();
cal.setTime(toData(time));
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd",
Locale.CHINA);
cal.add(Calendar.DATE, 6);
date = df.format(cal.getTime());
return date;
}
/**
* 配合getWeekFirstDay获得上周
* @return
*/
public static String getLastWeekDate(String time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(toData(time));
calendar.add(Calendar.DAY_OF_MONTH, -7);
Date date = calendar.getTime();
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd",
Locale.CHINA);
return sdr.format(date);
}
/**
* 配合getWeekFirstDay获得下周
* @return
*/
public static String getNextWeekDate(String time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(toData(time));
calendar.add(Calendar.DAY_OF_MONTH, 7);
Date date = calendar.getTime();
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd",
Locale.CHINA);
return sdr.format(date);
}
private static Date toData(String time) {
SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd",
Locale.CHINA);
Date date = null;
try {
date = sdr.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}