/**
* 格式化时间 yyyy-MM-dd
*
* @param time
* @return
*/
public static String formatDate(Timestamp time) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(time);
}
/**
* 格式化时间 yyyy-MM-dd HH:mm:ss.S
*
* @param time
* @return
*/
public static String formatTime(Timestamp time) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
return formatter.format(time);
}
public static String formatTime(Date time) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
return formatter.format(time);
}
public static String formatTimes(Timestamp time) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return formatter.format(time);
}
public static String formatTimes(Date time) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(time);
}
public static String formatTimes(Timestamp time, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.format(time);
}
/**
* 将字符串格式的时间转成时间戳格式
*
* @param time
* @return
*/
public static Timestamp forTimestamp(String time) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
Timestamp ts = null;
try {
date = formatter.parse(time);
ts = new Timestamp(date.getTime());
return ts;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ts;
}
/**
* 字符串的日期格式的计算
*
* @throws ParseException
*/
public static int daysBetween(String smdate, String bdate) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return 0;
}
}