日期处理工具类
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
/**
* @Auther:
* @Date: 2019/4/1 10:05
* @Description:
*/
public class DateUtils {
private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
private final static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
private final static Calendar calendar = Calendar.getInstance();
/**
* 缺省日期格式
*/
public static final String DATE_FORMAT = "yyyy-MM-dd";
/**
* 缺省日期格式
*/
public static final String DATE_FORMATS = "yyyyMMdd";
/**
* 缺省日期格式
*/
public static final String DATE_FORMATSS = "yyyy-MM";
/**
* 缺省长日期格式,精确到秒
*/
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_TIME_FORMATS = "yyyyMMdd HH:mm:ss";
/**
* 缺省长日期格式,精确到秒
*/
public static final String DATE_TIME_FORMAT_WEI = "yyyy-MM-dd HH:mm:ss.sss";
/**
* 星期数组
*/
public static final String[] WEEKS = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
/**
* 取当前日期的字符串表示
*
* @return 当前日期的字符串 ,如2019-04-01 10:05:29
**/
public static String now() {
return today(DATE_TIME_FORMAT);
}
//当前日期的字符串 ,如20190401 10:05:29
public static String nows() {
return today(DATE_TIME_FORMATS);
}
/**
* 取当前日期的字符串表示
*
* @return 当前日期的字符串 ,如2019-04-01
**/
public static String today() {
return today(DATE_FORMAT);
}
/**
* 根据输入的格式得到当前日期的字符串
*
* @param strFormat 日期格式
* @return
*/
public static String today(String strFormat) {
return format(new Date(), strFormat);
}
/**
* 获取格式化对象
*
* @param strFormat 格式化的格式 如"yyyy-MM-dd"
* @return 格式化对象
*/
public static SimpleDateFormat getSimpleDateFormat(String strFormat) {
if (strFormat != null && !"".equals(strFormat.trim())) {
return new SimpleDateFormat(strFormat);
} else {
return new SimpleDateFormat();
}
}
public static Date parseDateg(String date) {
try {
return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(date);
} catch (ParseException e) {
return null;
}
}
public static String getDateString(String strFormat, String pp) throws ParseException {
SimpleDateFormat sdf2 = new SimpleDateFormat(pp);
Date d2 = sdf2.parse(strFormat);
SimpleDateFormat sdgg = new SimpleDateFormat(pp);
return sdgg.format(d2);
}
/**
* 将java.util.date型按照指定格式转为字符串
*
* @param date 源对象
* @param format 想得到的格式字符串
* @return 如:2010-05-28
*/
public static String format(Date date, String format) {
return getSimpleDateFormat(format).format(date);
}
/**
* 将java.util.date型按照缺省格式转为字符串
*
* @param date 源对象
* @return 如:2010-05-28
*/
public static String format(Date date) {
return format(date, DATE_TIME_FORMAT);
}
/**
* 将20100528 ---> 2010-05-28
*
* @param str 源对象
* @return 如:20100528 ---> 2010-05-28
*/
public static String dateConvertion(String str, String fromStr, String toStr) {
Date parse = null;
String dateString = "";
try {
parse = new SimpleDateFormat(fromStr).parse(str);
dateString = new SimpleDateFormat(toStr).format(parse);
} catch (ParseException e) {
dateString = null;
}
return dateString;
}
/**
* 将java.util.date型按照缺省格式转为字符串
*
* @param date 源对象
* @return 如:2010-05-28
*/
public static String formats(Date date) {
return format(date, DATE_FORMATS);
}
/**
* 将java.util.date型按照缺省格式转为字符串
*
* @param date 源对象
* @return 如:2010-05-28
*/
public static String formatDate(Date date) {
return format(date, DATE_FORMAT);
}
/**
* 强制类型转换 从串到日期
*
* @param strDate 源字符串,采用yyyy-MM-dd格式
* @param format ps
* @return 得到的日期对象
* @throws ParseException
*/
public static Date parse(String strDate, String format) throws ParseException {
return getSimpleDateFormat(format).parse(strDate);
}
public static Date parse(String strDate) throws ParseException {
return parse(strDate, DATE_TIME_FORMAT);
}
public static Date parseDate(String strDate) throws ParseException {
return parse(strDate, DATE_FORMAT);
}
public static Date parseDates(String strDate) throws ParseException {
return parse(strDate, DATE_FORMATS);
}
//计算两个时间相差的秒数
public static long getTime(String startTime, String endTime) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long eTime = df.parse(endTime).getTime();
long sTime = df.parse(startTime).getTime();
long diff = (eTime - sTime) / 1000;
return diff;
}
/**
* 日期时间计算
*
* @param date
* @param amount
* @param field
* @return
*/
public static Date add(Date date, int amount, int field) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(field, amount);
return calendar.getTime();
}
/**
* 获取过去第几天的日期
*
* @param past
* @return
*/
public static String getPastDate(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMATS);
String result = format.format(today);
return result;
}
public static String getPastDates(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
String result = format.format(today);
return result;
}
/**
* 设置月份
*
* @param date
* @param value
* @return
*/
public static Date setMonth(Date date, int value) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MONTH, value);
return calendar.getTime();
}
/**
* 设置日期
*
* @param date
* @param value
* @return
*/
public static Date setDate(Date date, int value) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DATE, value);
return calendar.getTime();
}
/**
* 月份计算
*
* @param date
* @param amount
* @return
*/
public static Date addMonth(Date date, int amount) {
return add(date, amount, Calendar.MONTH);
}
/**
* 日期计算
*
* @param date
* @param amount
* @return
*/
public static Date addDate(Date date, int amount) {
return add(date, amount, Calendar.DATE);
}
/**
* 日期计算
*
* @param date yyyy-MM-dd HH:mm:ss
* @param amount
* @return
*/
public static String addDates(String date, int amount)throws ParseException {
return format(add(parse(date, DATE_TIME_FORMAT), amount, Calendar.DATE));
}
/**
* 小时计算
*
* @param date
* @param amount
* @return
*/
public static Date addHour(Date date, int amount) {
return add(date, amount, Calendar.HOUR);
}
/**
* 分钟计算
*
* @param date
* @param amount
* @return
*/
public static Date addMinute(Date date, int amount) {
return add(date, amount, Calendar.MINUTE);
}
/**
* 时间差(天)
*
* @param date1
* @param date2
* @return
*/
public static int diffDay(Date date1, Date date2) {
long min = Math.min(date1.getTime(), date2.getTime());
long max = Math.max(date1.getTime(), date2.getTime());
long day = (max - min) / (24 * 60 * 60 * 1000);
return Math.toIntExact(day);
}
/**
* 时间差(小时)
*
* @param date1
* @param date2
* @return
*/
public static int diffHour(Date date1, Date date2) {
long min = Math.min(date1.getTime(), date2.getTime());
long max = Math.max(date1.getTime(), date2.getTime());
long day = (max - min) / (60 * 60 * 1000);
return Math.toIntExact(day);
}
/**
* 时间差(分)
*
* @param date1
* @param date2
* @return
*/
public static int diffMinute(Date date1, Date date2) {
long min = Math.min(date1.getTime(), date2.getTime());
long max = Math.max(date1.getTime(), date2.getTime());
long day = (max - min) / ( 60 * 1000);
return Math.toIntExact(day);
}
/**
* 时间差(秒)
*
* @param date1
* @param date2
* @return
*/
public static int diffSecond(Date date1, Date date2) {
long min = Math.min(date1.getTime(), date2.getTime());
long max = Math.max(date1.getTime(), date2.getTime());
long day = (max - min) / (1000);
return Math.toIntExact(day);
}
/**
* 计算俩个日期之间的天数
* @param smdate
* @param bdate
* @return
* @throws ParseException
*/
public static int daysBetween(String smdate,String bdate) throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 时间差(月)
*
* @param date1
* @param date2
* @return
*/
public static int diffMonth(Date date1, Date date2) {
// int day = diffDay(date1, date2);
BigDecimal day = BigDecimal.valueOf(diffDay(date1, date2));
// double month = (double) day / 30;
BigDecimal month = day.divide(BigDecimal.valueOf(30), 1, BigDecimal.ROUND_HALF_UP);
// BigDecimal bigDecimal = BigDecimal.valueOf(month);
return month.intValue();
}
/**
* 时间差(年)
*
* @param date1
* @param date2
* @return
*/
public static double diffYear(Date date1, Date date2) {
BigDecimal day = BigDecimal.valueOf(diffDay(date1, date2));
BigDecimal year = day.divide(BigDecimal.valueOf(365), 1, BigDecimal.ROUND_HALF_UP);
return year.doubleValue();
}
/**
* 时间差(年)
*
* @param date1
* @param date2
* @return
*/
public static int diffYear2(Date date1, Date date2) {
BigDecimal day = BigDecimal.valueOf(diffDay(date1, date2));
BigDecimal year = day.divide(BigDecimal.valueOf(365), 1, BigDecimal.ROUND_HALF_UP);
return year.intValue();
}
/**
* 获取当月最大日期
*
* @param date
* @return
*/
public static Date maxMonthDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
/**
* 获取指定月第一天
*
* @param month
* @return
*/
public static Date getMinDateMonth(String month) {
try {
Date nowDate = sdf.parse(month);
Calendar instance = Calendar.getInstance();
instance.setTime(nowDate);
instance.set(Calendar.DAY_OF_MONTH, instance.getActualMinimum(Calendar.DAY_OF_MONTH));
return instance.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/*
* 输入日期字符串,返回当月最后一天的Date
*/
public static Date getMaxDateMonth(String month) {
try {
Date nowDate = sdf.parse(month);
Calendar instance = Calendar.getInstance();
instance.setTime(nowDate);
instance.set(Calendar.DAY_OF_MONTH, instance.getActualMaximum(Calendar.DAY_OF_MONTH));
return instance.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static int getMonthForString(String month) {
try {
Date nowDate = sdf.parse(month);
return nowDate.getMonth();
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
/*
* 输入日期字符串,返回当月最后一天的Date
*/
public static String getMaxDateMonthByDate(Date month) {
Calendar instance = Calendar.getInstance();
instance.setTime(month);
instance.set(Calendar.DAY_OF_MONTH, instance.getActualMaximum(Calendar.DAY_OF_MONTH));
Date a = instance.getTime();
SimpleDateFormat sdgg = new SimpleDateFormat("yyyy-MM-dd");
return sdgg.format(a);
}
public static int get(Date date, int field) {
Calendar instance = Calendar.getInstance();
instance.setTime(date);
return instance.get(field);
}
public static int getMonth(Date date) {
return get(date, Calendar.MONTH);
}
public static int getYear(Date date) {
return get(date, Calendar.YEAR);
}
public static String getMaxDateLastMonth(String mat) {
SimpleDateFormat format = new SimpleDateFormat(mat);
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, 0);
String last = format.format(ca.getTime());
return last;
}
public static Date localDateToDate(LocalDate localDate) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
Date date = Date.from(instant);
return date;
}
public static LocalDate dateTolocalDate(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
LocalDate localDate = localDateTime.toLocalDate();
return localDate;
}
/*
* 返回工作日集合,只排除周末
*/
public static List<Date> getWorkDates(int year, int month) {
List<Date> dates = new ArrayList<>();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DATE, 1);
while (cal.get(Calendar.YEAR) == year &&
cal.get(Calendar.MONTH) < month) {
int day = cal.get(Calendar.DAY_OF_WEEK);
if (!(day == Calendar.SUNDAY || day == Calendar.SATURDAY)) {
dates.add((Date) cal.getTime().clone());
}
cal.add(Calendar.DATE, 1);
}
return dates;
}
/**
* @param dateList
* @return 返回日期字符串集合
*/
public static List<String> getDateString(List<Date> dateList) {
// 储存日期字符串
List<String> dateString = new ArrayList<>();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
for (Date date : dateList) {
String date2 = simpleDateFormat.format(date);
dateString.add(date2);
}
return dateString;
}
/**
* @return 返回法定节假日集合
*/
public static List<String> getHolidays() {
List<String> holidays = new ArrayList<>();
holidays.add("2021-01-01");
holidays.add("2019-01-14");
holidays.add("2019-02-11");
holidays.add("2019-03-21");
holidays.add("2019-04-29");
holidays.add("2019-04-30");
holidays.add("2019-05-01");
holidays.add("2019-05-02");
holidays.add("2019-05-03");
holidays.add("2019-05-04");
holidays.add("2019-05-05");
holidays.add("2019-05-06");
holidays.add("2019-07-15");
holidays.add("2019-08-11");
holidays.add("2019-08-12");
holidays.add("2019-09-16");
holidays.add("2019-09-23");
holidays.add("2019-10-14");
holidays.add("2019-10-22");
holidays.add("2019-11-03");
holidays.add("2019-11-04");
holidays.add("2019-11-23");
return holidays;
}
/**
* 获取本周第一天
*
* @return
*/
public static String getTimeDays() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK, 2);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
String mon = dateFormat.format(calendar.getTime());
System.out.println("获取本周第一天" + mon);
return mon;
}
public static String getLastTimeDays() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, -7);
Date d = c.getTime();
String day = format.format(d);
System.out.println("过去七天:" + day);
return day;
}
public static List<String> getSpecifiedWeekList(String mon) {
List<String> timeList = new ArrayList();
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(mon));
//判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
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);//根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
timeList.add(sdf.format(cal.getTime()));//周一
cal.add(Calendar.DATE, 1);
timeList.add(sdf.format(cal.getTime()));//周二
cal.add(Calendar.DATE, 1);
timeList.add(sdf.format(cal.getTime()));//周三
cal.add(Calendar.DATE, 1);
timeList.add(sdf.format(cal.getTime()));//周四
cal.add(Calendar.DATE, 1);
timeList.add(sdf.format(cal.getTime()));//周五
cal.add(Calendar.DATE, 1);
timeList.add(sdf.format(cal.getTime()));//周六
cal.add(Calendar.DATE, 1);
timeList.add(sdf.format(cal.getTime()));//周日
return timeList;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 条件来源程序判断:
* 获取当前月份的所有日期
*
* @param date "2020-11-20"
* @return 如果是当前月,返回到当月截止今天的日期集合 如果是之前月返回之前月的日期集合
* <p>
* 来源:https://www.cnblogs.com/jinzhiming/p/6224860.html
*/
public static List<String> getMonthFullDayWorkingDay(String date) {
List<String> fullDayList = new ArrayList<String>();
int year = Integer.parseInt(date.substring(0, 4));
int month = Integer.parseInt(date.substring(5, 7));
int day = 1;// 所有月份从1号开始
Calendar cal = Calendar.getInstance();// 获得当前日期对象
// 获取当前月的月份,和日期
int current_month = cal.get(Calendar.MONTH) + 1; // 当前月
int current_date = cal.get(Calendar.DATE); // 当前天
cal.clear();// 清除信息
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);// 1月从0开始
cal.set(Calendar.DAY_OF_MONTH, day);
// 判断出入的时间中的月份是否在当前时间
// 如果是过去的月份,则,获取传入的月份 整个月的工作日
//if (month < current_month) {
int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 获取所在月份的最大天数
for (int j = 0; j <= (count - 1); ) {
// 判断,如果是最后一个月份,跳出循环
if (sdf1.format(cal.getTime()).equals(getLastDay(year, month)))
break;
// 否则,天数++
cal.add(Calendar.DAY_OF_MONTH, j == 0 ? +0 : +1);
j++;
//=========================== 追加条件 Start ============================
// 追加日期
fullDayList.add(sdf1.format(cal.getTime()));
//=========================== 追加条件 End ============================
}
//}
// 如果相等,获取 1号 到 今天的一个时间
// else if (month == current_month) {
// int count = current_date; // 设置当前月份的当前天数
// for (int j = 0; j <= (count - 1); ) {
// // 判断,如果是最后一个月份,跳出循环
// if (sdf1.format(cal.getTime()).equals(getCurrentDay(year, month, current_date)))
// break;
// // 否则,天数++
// cal.add(Calendar.DAY_OF_MONTH, j == 0 ? +0 : +1);
// j++;
// //=========================== 追加条件 Start ============================
// // 追加日期
// fullDayList.add(sdf1.format(cal.getTime()));
// //=========================== 追加条件 End ============================
// }
// }
return fullDayList;
}
/**
* 获取上个月月份
*
* @return
*/
public static final String getLastMonth() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
Date date = new Date();
Calendar calendar = Calendar.getInstance();
// 设置为当前时间
calendar.setTime(date);
calendar.add(Calendar.MONTH, -1);
// 设置为上一个月
date = calendar.getTime();
return format.format(date);
}
/**
* 获取当前日期
*
* @return
*/
public static String getTheCurrentDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
return formatter.format(date);
}
/**
* 获取当前日期
*
* @return
*/
public static Date getTheCurrentDate1() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
return date;
}
/**
* 获取当前日期(精确到时分秒)
*
* @return
*/
public static String getTheCurrent() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
return formatter.format(date);
}
public static String getCurrentDay(int year, int month, int data) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, data);
return sdf1.format(cal.getTime());
}
/**
* 获取所在月的最后一天
*
* @param year 2020
* @param month 10
* @return "2020-10-31 08:49:29"
*/
public static String getLastDay(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, 0);
return sdf1.format(cal.getTime());
}
/**
* 获取前一年年份
*
* @param
* @return
*/
public static int TheYearBefore() {
Calendar c = Calendar.getInstance();
int year1 = c.get(Calendar.YEAR) - 1;
return year1;
}
/**
* 获取今年年年份
*
* @param
* @return
*/
public static int TheCurrentYear() {
Calendar c = Calendar.getInstance();
int year1 = c.get(Calendar.YEAR);
return year1;
}
/**
* 遍历所有月份
*
* @return
*/
public static List<String> getYear(int year) {
List<String> fullDayList = new ArrayList<String>();
Calendar c = Calendar.getInstance();
// 获取当前的年份
//int year1 = c.get(Calendar.YEAR)-1;
// 定义时间格式
Date endDate = null;
try {
// 开始日期为当前年拼接1月份
Date startDate = sdf.parse(year + "-01");
// 结束日期为当前年拼接12月份
endDate = sdf.parse(year + "-12");
// 设置calendar的开始日期
c.setTime(startDate);
} catch (ParseException e) {
e.printStackTrace();
}
// 当前时间小于等于设定的结束时间
while (c.getTime().compareTo(endDate) <= 0) {
String time = sdf.format(c.getTime());
fullDayList.add(sdf.format(c.getTime()));
// 打印日期
System.out.println(time);
// 当前月份加1
c.add(Calendar.MONTH, 1);
}
return fullDayList;
}
/**
* 根据年月获取所有月份的最后一天
*
* @param eDate
* @return
*/
public static String getLastYear(String eDate) {
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
// 定义日期字段
Date cxdate1 = null;
// 转换前台传入字符串为日期格式
try {
cxdate1 = sdf1.parse(eDate);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(cxdate1);
//获取年份
int jzYear = cal1.get(Calendar.YEAR);
//获取月份
int jzMonth = cal1.get(Calendar.MONTH) + 1;
//设置年份
cal1.set(Calendar.YEAR, jzYear);
//设置月份
cal1.set(Calendar.MONTH, jzMonth - 1);
//获取某月最大天数
int lastDay = cal1.getActualMaximum(Calendar.DATE);
//设置日历中月份的最大天数
cal1.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期为年月日类型字符串
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
//获取传入月份最后一天
String dataTime = sdf2.format(cal1.getTime());
return dataTime;
}
/**
* 获取当前日期(精确到时分秒)不要格式
*
* @return
*/
public static String getCurrentWithoutFormat() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
return formatter.format(date);
}
}