两个日期相差多少天:
public int days(String startDate,String endDate) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
long to = 0,from = 0;
try {
to = df.parse(endDate).getTime();
from = df.parse(startDate).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
int num = (int) ((to - from) / (1000 * 60 * 60 * 24));
return num;
}
毫秒转日期
public static String toData(long milliscond){
Date date = new Date(milliscond);
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String data = simpleDateFormat.format(gc.getTime());
return data;
}
日期转毫秒数:
/**
* 将字符串数据转化为毫秒数
*/
public static long time(String dateTime) {
String time1 = dateTime.replaceAll("-","");
String time2 = time1.replaceAll(":","");
String time3 = time2.replaceAll(" ","");
Calendar c = Calendar.getInstance();
try {
c.setTime(new SimpleDateFormat("yyyyMMddHHmmss").parse(time3));
} catch (ParseException e) {
e.printStackTrace();
}
long msTime = c.getTimeInMillis();
return msTime;
}
获取当前系统时间:
long time = System.currentTimeMillis();
日期转换为星期:
/**
* 根据日期转换为星期
* @param sDate
* @return
*/
public static String getFullDateWeekTime(String sDate){
try{
String formater = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat format = new SimpleDateFormat(formater);
Date date=format.parse(sDate);
format.applyPattern("yyyy-MM-dd E HH:mm:ss");
return format.format(date);
}catch(Exception ex){
System.out.println("TimeUtil getFullDateWeekTime"+ex.getMessage());
return "";
}
}