package util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @Title: DateUtil.java
* @Description: 日期辅助类
*/
public class DateUtil {
/**
* @Title: getDateString
* @Description: 使用"yyyy-MM-dd HH:mm:ss"格式化日期
* @param date
* @return String 返回类型
*/
public static String getDateString(Date date) {
return getDateString(date, "yyyy-MM-dd HH:mm:ss");
}
/**
*
* @param date
* @param format
* @return
*/
public static String getCurDateString(Date date){
return getDateString(date, "yyyy-MM-dd");
}
/**
* @Title: getDateString
* @Description: 使用"yyyy-MM-dd HH:mm:ss"格式化日期
* @param long time
* @return String 返回类型
*/
public static String getDateString(long time) {
Date date = new Date();
date.setTime(time);
return getDateString(date, "yyyy-MM-dd HH:mm:ss");
}
/**
* @Title: getDateString
* @Description: 格式化日期
* @param date
* 日期
* @param format
* 模式
* @return String 返回类型
*/
public static String getDateString(Date date, String format) {
if (date != null) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
String dateString = formatter.format(date);
return dateString;
}
return null;
}
/**
* @Title: parseDate
* @Description: 解析Date,string to java.util.Date
* @param strDate
* @param format
* @return Date 返回类型
*/
public static Date parseDate(String strDate, String format) {
if (strDate != null) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
Date date = null;
try {
date = formatter.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
return null;
}
/**
* 字符串时间转为long的时间
* @param strDate
* @param format
* @return
*/
public static int parseDateToLong(String strDate, String format) {
if (strDate != null) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
Date date = null;
try {
date = formatter.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
return (int)(date.getTime()/1000);
}
return (int) (new Date().getTime()/1000);
}
/***
*
* @Title: getDaysBetween
* @Description: 计算两个日期之间的天数(date1<date2)
* @author wujl
* @param @param date1
* @param @param date2
* @param @return 设定文件
* @return int 返回类型
*/
public static int getDaysBetween(Date date1, Date date2) {
long from = date1.getTime();
long to = date2.getTime();
return (int) (to - from) / (1000 * 60 * 60 * 24);
}
public static int getHour(Date time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
int hourTime = calendar.get(Calendar.HOUR_OF_DAY);
return hourTime;
}
public static int getDayOfMonth(Date time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
int hourTime = calendar.get(Calendar.DAY_OF_MONTH);
return hourTime;
}
public static int getDayOfYear(Date time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
int hourTime = calendar.get(Calendar.DAY_OF_YEAR);
return hourTime;
}
public static int getMonth(Date time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
int hourTime = calendar.get(Calendar.MONTH);
return hourTime;
}
public static int getMinute(Date time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
int hourTime = calendar.get(Calendar.MINUTE);
return hourTime;
}
public static int getYear(Date time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
int hourTime = calendar.get(Calendar.YEAR);
return hourTime;
}
public static int getNowTimeStamp() {
return (int) (System.currentTimeMillis() / 1000);
}
/**
* 在date上面加减天数
* @param date
* @param num
* @return
*/
public static Date addDayByDate(Date date, int num){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_YEAR, num);
return calendar.getTime();
}
}
日期辅助类
最新推荐文章于 2021-07-23 23:57:30 发布