java工具类------日期工具类
1.不多说直接上代码,小伙伴们可以看注释,代码如下:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @ClassName: DateUtil
* @Description: 日期工具类
* @author wangXi
* @date
*/
public class DateUtil
{
/**
* @Title: formatDate
* @Description: 格式化日期
* @param date
* @param formatString
* @return String
*/
public static String formatDate(Date date, String formatString)
{
SimpleDateFormat format = new SimpleDateFormat(formatString);
return format.format(date);
}
/**
* @Title: fromStringToDate
* @Description: 自定义日期,注意 日期字符串需和fomat保持一致
* @param fomat
* 例:yyyy-MM-dd
* @param dateStr
* 例:2015-01-01
* @return Date
* @throws ParseException
*/
public static Date fromStringToDate(String fomat, String dateStr)
throws ParseException
{
Date date = null;
date = new SimpleDateFormat(fomat).parse(dateStr);
return date;
}
/**
* @Title: currentDayMinus
* @Description: 根据系统参数对当前日期进行相减操作
* @param formatString 日期格式
* @param days
* @return String 当前日期相减后的日期
*/
@SuppressWarnings("static-access")
public static String currentDayMinus(int days, String formatString)
{
DateFormat format = new SimpleDateFormat(formatString);
Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(c.DATE, -days);
Date tempDate = c.getTime();
String currentDayMinus = format.format(tempDate);
return currentDayMinus;
}
/**
* @Title: currentDayMinus
* @Description: 根据系统参数对当前日期进行相加操作
* @param formatString
* 日期格式
* @param days
* @return String 当前日期相加后的日期
*/
@SuppressWarnings("static-access")
public static String currentDayAddition(int days, String formatString)
{
DateFormat format = new SimpleDateFormat(formatString);
Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(c.DATE, +days);
Date tempDate = c.getTime();
String currentDayMinus = format.format(tempDate);
return currentDayMinus;
}
/**
* @Title: currentDayAdditionReturnDate
* @Description: 当前日期加n天,返回Date类型
* @param days
* @return Date
*/
public static Date currentDayAdditionReturnDate(Date date, int days)
{
// Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, +days);
Date tempDate = c.getTime();
// String currentDayMinus = format.format(tempDate);
return tempDate;
}
/**
* @Title: currentSecondPlus
* @Description: 对当前日期进行秒的加减运算
* @param second
* 与当前时间的差
* @param formatString
* @return String
*/
@SuppressWarnings("static-access")
public static String currentSecondPlus(int second, String formatString)
{
DateFormat format = new SimpleDateFormat(formatString);
Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(c.SECOND, second);
Date tempDate = c.getTime();
String restul = format.format(tempDate);
return restul;
}
/**
* @Title: currentDateMinusMin
* @Description: 当前日期
* @param minutes
* @return Date
*/
public static Date currentDateMinusMin(int minutes)
{
Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MINUTE, -minutes);
Date tempDate = c.getTime();
return tempDate;
}
/**
* @Title: getCurrentDate
* @Description: 获取当前日期,不带时间
* @return
* @throws ParseException
*/
public static Date getCurrentDate() throws ParseException
{
Date today = new Date();
String todayStr = DateUtil.formatDate(today, "yyyy.MM.dd");
Date currentDate = DateUtil.fromStringToDate("yyyy.MM.dd", todayStr);
return currentDate;
}
/**
* @Title: removeTimeInDate
* @Description: 删除日期中的时间
* @param date
* @return Date
* @throws ParseException
*/
public static Date removeTimeInDate(Date date) throws ParseException
{
String dateStr = DateUtil.formatDate(date, "yyyy.MM.dd");
Date newDate = DateUtil.fromStringToDate("yyyy.MM.dd", dateStr);
return newDate;
}
/**
* @Title: dateMinus
* @Description: 两日期相减
* @param endDate
* @param currentDate
* @return double
*/
public static double dateMinus(Date endDate, Date currentDate)
{
double longTime = endDate.getTime() - currentDate.getTime();
double day = longTime / 1000 / 60 / 60 / 24;
return day;
}
/**
* @Title: getCurrentDateTime
* @Description: 获取当前时间
* @return String
*/
public static String getCurrentDateTime()
{
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(new Date());
}
/**
* @Title: getCurrentDateStr
* @Description: 获取当前日期串
* @return
*/
public static String getCurrentDateStr()
{
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(new Date());
}
/**
* @Title: getCurrentDateTime
* @Description: 获取当前时间
* @return String
*/
public static String getCurrentDateTimeByFomat(String fomat)
{
DateFormat format = new SimpleDateFormat(fomat);
return format.format(new Date());
}
/**
* @Title: getFirstDayOfLastMonth
* @Description: 获取某日期 前n个月的第一天
* @return
* @throws ParseException
*/
public static Date getFirstDayOfLastMonth(Date date, int n)
throws ParseException
{
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// 获取前月的第一天
Calendar cal_1 = Calendar.getInstance();// 获取当前日期
cal_1.setTime(date);
cal_1.add(Calendar.MONTH, -n);
cal_1.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
Date result = cal_1.getTime();
// String firstDay = format.format(cal_1.getTime());
return removeTimeInDate(result);
}
}
至此,日期方法总结完毕了,该工具类基本上能够满足小伙伴们在开发中遇到的日期问题,希望对大家有帮助!
还没有评论,来说两句吧...