获取当前时间
public static String getNowTime(String dateformat) {
try {
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);
String nowTime = dateFormat.format(now);
return nowTime;
} catch (Exception e) {
logger.error("getNowTime Exception = ", e);
}
return null;
}
得到几天前的时间
public static Date getDateBefore(Date date, int day) {
Calendar no = Calendar.getInstance();
no.setTime(date);
no.set(Calendar.DATE, no.get(Calendar.DATE) - day);
return no.getTime();
}
Date日期格式转为字符换
public static String date2String(Date date, String pattern) {
try {
if(date != null){
if ("yyyy-MM-dd".equals(pattern)) {
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
return sdf3.format(date);
} else if ("yyyy-MM-dd HH:mm:ss".equals(pattern)) {
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf2.format(date);
} else if ("yyyy-MM-dd HH:mm:ss.SSS".equals(pattern)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return sdf.format(date);
}
else {
SimpleDateFormat format = new SimpleDateFormat(pattern);
return format.format(date);
}
}
} catch (Exception e) {
logger.error("日期格式化成字符串异常:", e);
}
return null;
}
给指定日期添加天数
public static Date addDay(Date date, int addDays) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, addDays);
return calendar.getTime();
}
给指定日期添加小时数
public static Date addHour(Date date, int addHours) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, addHours);
return calendar.getTime();
}
给指定日期添加分钟数
public static Date addMinute(Date date, int minutes) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, minutes);
return calendar.getTime();
}
获取时间是一年中的第几天
public static int getDayOfYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_YEAR);
}
获取时间是一天中的第几个小时
public static int getHourOfDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.HOUR_OF_DAY);
}
得到两个日期间的间隔天数
public static double getTwoDay(String sj1, String sj2) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
double day = 0.5;
try {
java.util.Date date = myFormatter.parse(sj1);
java.util.Date mydate = myFormatter.parse(sj2);
day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000)+1;
day = day < 1 ? 0.5 : day;
} catch (Exception e) {
return day;
}
return day;
}
比较两个日期的大小
public static boolean compareDateTwo(String time1, String time2) {
boolean b = false;
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
try{
Date date1 = ft.parse( time1 );
Date date2 = ft.parse( time2 );
if(date1.getTime() > date2.getTime()){
b = true;
}
}catch(ParseException e){
e.printStackTrace();
}
return b;
}
获取随机日期
public static Date randomDate(String beginDate,String endDate){
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date start = format.parse(beginDate);
Date end = format.parse(endDate);
if(start.getTime() >= end.getTime()){
return null;
}
long date = random(start.getTime(),end.getTime());
return new Date(date);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
检查一个日期字符串是否能正确转换成给定的格式
public static boolean checkDateFormat(String dateStr, String pattern) {
try {
if ("yyyy-MM-dd".equals(pattern)) {
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
sdf3.parse(dateStr);
} else if ("yyyy-MM-dd HH:mm:ss".equals(pattern)) {
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf2.parse(dateStr);
} else if ("yyyy-MM-dd HH:mm:ss.SSS".equals(pattern)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
sdf.parse(dateStr);
}
else {
SimpleDateFormat format = new SimpleDateFormat(pattern);
format.parse(dateStr);
}
} catch (Exception e) {
logger.error("检查一个日期字符串是否能正确转换成给定的格式异常:", e);
return false;
}
return true;
}