import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class DateFormatUtil {
private static final Logger log = LoggerFactory.getLogger(DateFormatUtil.class);
/**
* Date >>>> String (yyyy-MM-dd HH:mm:ss)
* @param date
* @return
*/
public static String date2String(Date date){
//设置要获取到什么样的时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//获取String类型的时间
String stringDate = sdf.format(date);
return stringDate;
}
/**
* String >>>> Date (yyyy-MM-dd HH:mm:ss)
* @param str
* @return
*/
public static Date string2Date(String str){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = null;
try {
parse = dateFormat.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}
/**
* Date >>>> String
* @param date
* @return
*/
public static String date2StringDay(Date date){
//设置要获取到什么样的时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//获取String类型的时间
String stringDate = sdf.format(date);
return stringDate;
}
/**
* String >>>> Date (yyyy-MM-dd)
* @param str
* @return
*/
public static Date string2DateDay(String str){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parse = null;
try {
parse = dateFormat.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}
/**
* Date >>>> long
* @param date
* @return
*/
public static long date2Long(Date date){
//获得long类型的时间戳(毫秒)
return date.getTime();
}
/**
* long >>>> Date
* @param lo
* @return
*/
public static Date long2Date(Long lo){
return new Date(lo);
}
/**
* long >>>> String
* @param lo
* @return
*/
public static String long2String(long lo){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(lo);
return sdf.format(date);
}
/**
* String >>>> long (yyyy-MM-dd HH:mm:ss)
* @param str
* @return
*/
public static Long string2Long(String str){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long time = 0;
try {
Date parse = dateFormat.parse(str);
time = parse.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return time;
}
/**
* 获得系统时间的时间戳
* @return
*/
public static long systemTimestamp(){
//得到系统当前时间的时间戳(毫秒)
return System.currentTimeMillis();
}
/**
* 求两个时间之间相差的天数(不足24小时不计算天数)
* @param begin
* @param end
* @return
*/
public static long daysBetween(Date begin, Date end) {
long difference = (end.getTime() - begin.getTime())/86400000;
return Math.abs(difference);
}
/**
* 获取两个日期之间的所有日期(yyyy-MM-dd)
* @param begin
* @param end
* @return
*/
public static List<Date> getBetweenDates(Date begin, Date end) {
List<Date> result = new ArrayList<Date>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(begin);
while(begin.getTime()<=end.getTime()){
result.add(tempStart.getTime());
tempStart.add(Calendar.DAY_OF_YEAR, 1);
begin = tempStart.getTime();
}
result.add(end);
return result;
}
public static void main(String[] args) {
Calendar c1 = Calendar.getInstance();
c1.setTime(new Date());
// 获得年份
int year = c1.get(Calendar.YEAR);
log.info("year===="+year);
// 获得月份
int month = c1.get(Calendar.MONTH) + 1;
log.info("month===="+month);
// 获得日期
int day = c1.get(Calendar.DATE);
log.info("day===="+day);
// 获得小时
int hour = c1.get(Calendar.HOUR_OF_DAY);
log.info("hour===="+hour);
// 获得分钟
int minute = c1.get(Calendar.MINUTE);
log.info("minute===="+minute);
// 获得秒
int second = c1.get(Calendar.SECOND);
log.info("second===="+second);
// 获得星期几(注意(这个与Date类是不同的):1代表星期日、2代表星期1、3代表星期二,以此类推)
int week = c1.get(Calendar.DAY_OF_WEEK);
log.info("week===="+week);
long l = daysBetween(new Date(), string2Date("2018-9-10 05:09:22"));
log.info("两个时间相差天数===="+l);
List<Date> betweenDates = getBetweenDates(new Date(), string2Date("2018-9-10 05:09:22"));
log.info("betweenDates===="+betweenDates.toString());
}
}