import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 处理日期时间工具类
* @author suny
*/
public class DateUtil {
private static String dateFormat = "yyyy-MM-dd HH:mm:ss";
/**
* @param date
* @return 将date转化为Calendar
*/
public static Calendar getCalendar(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
/**
* @param date
* @return 获得date的年份
*/
public static int getYear(Date date){
return getCalendar(date).get(Calendar.YEAR);
}
/**
* @return 获得今年
*/
public static int getThisYear(){
return getYear(new Date());
}
/**
* @param date
* @return 得到某年的第一天
*/
public static Date getFirstDayOfYear(Date date){
Calendar calendar = getCalendar(date);
int year = calendar.get(Calendar.YEAR);
calendar.set(year, 1, 1);
return calendar.getTime();
}
/**
* @param date
* @return 得到某年的最后一天
*/
public static Date getLastDayOfYear(Date date){
Calendar calendar = getCalendar(date);
int year = calendar.get(Calendar.YEAR);
calendar.set(year, 12, 31);
return calendar.getTime();
}
/**
* @param firstDate
* @param lastDate
* @return 获得两个日期相隔的年份
*/
public static int getYearsBetween(Date firstDate, Date lastDate){
return new Long(getDatesBetween(firstDate,lastDate)/365).intValue();
}
/**
* @param date
* @return 获得data的月份
*/
public static int getMonth(Date date){
return getCalendar(date).get(Calendar.MONTH)+1;
}
/**
* @param date
* @return 获得本月
*/
public static int getThisMonth(Date date){
return getMonth(new Date());
}
/**
* @param date
* @return 获得某月的第一天
*/
public static Date getFirstDayOfMonth(Date date){
Calendar calendar = getCalendar(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
calendar.set(year, month, 1);
return calendar.getTime();
}
/**
* @param date
* @return 获得某月的最后一天
*/
public static Date getLastDayOfMonth(Date date){
//下月第一天的上一天就是这月的最后一天
Calendar calendar = getCalendar(date);
calendar.add(Calendar.MONTH, 1);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
calendar.set(year, month, 1);
calendar.add(Calendar.DAY_OF_MONTH, -1);
return calendar.getTime();
}
/**
* @param firstDate
* @param lastDate
* @return 获得两个日期相隔的月份
*/
public static int getMonthsBetween(Date firstDate, Date lastDate){
return new Long(getDatesBetween(firstDate,lastDate)/30).intValue();
}
/**
* @param firstDate
* @param lastDate
* @return 获得两个日期相隔的天数
*/
public static long getDatesBetween(Date firstDate, Date lastDate){
Calendar calendar1 = getCalendar(firstDate);
Calendar calendar2 = getCalendar(lastDate);
long between = calendar2.getTimeInMillis()-calendar1.getTimeInMillis();
return between/(24*60*60*1000);
}
/**
* @param date 日期
* @param format 格式
* @return 按照格式返回一个日期的字符串
*/
public static String getStringOfDate(Date date, String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
/**
* @param date 日期
* @return 按照"YYYY-MM-DD"的格式返回一个日期的字符串
*/
public static String getStringOfDate(Date date){
return getStringOfDate(date,dateFormat);
}
/**
* @param string 日期字符串
* @param format 日期格式
* @return 将日期字符串按格式转换成日期
*/
public static Date getDate(String string, String format) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(string);
}
/**
* @param string 日期字符串
* @return 将日期字符串按默认格式(YYYY-MM-DD)转换成日期
*/
public static Date getDate(String string) throws ParseException{
return getDate(string,dateFormat);
}
/**
* @param year
* @param month
* @param date
* @return 通过year,month,date获得日期
*/
public static Date getDate(int year, int month, int date) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, date);
return calendar.getTime();
}
}
处理日期时间工具类
最新推荐文章于 2022-05-31 16:16:27 发布