/**
* 获取当前时间的 毫秒数
* 参数格式:yyyy-MM-dd HH:mm:ss
* @param time
* @return
* @throws ParseException
*/
public static long getTimeInMillis(String time) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(time));
long timeInMillis = cal.getTimeInMillis();
return timeInMillis;
}
/**
* 获取当前日期 格式: yyyy-mm-dd HH:mm :ss (年-月-日 时:分:秒)
*/
public static String getDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}
/**
*
* 得到当前系统日期 格式: yyyy-MM-dd (年-月-日)
*/
public static String getDate1() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());
}
/**
* 得到当前系统时间
*
* @return 格式为yyyyMMdd 20120301
*/
public static String getDate2() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(date);// 系统的当前时间
}
/**
* 得到当前系统的 HH:mm:ss
*/
public static String getTime() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(new Date());
}
/**
* 得到系统当前时间
* @return 格式为yyyyMMddHHmm
*/
public static String getTime2() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(new Date());
}
/*
* 获取当前日期是 星期几
* **/
public static String getWeek() {
String currentWeek = null;
Calendar c = Calendar.getInstance();
String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
if (mWay.equals("1")) {
currentWeek = "星期天";
}else if (mWay.equals("2")) {
currentWeek = "星期一";
}else if (mWay.equals("3")) {
currentWeek = "星期二";
}else if (mWay.equals("4")) {
currentWeek = "星期三";
}else if (mWay.equals("5")) {
currentWeek = "星期四";
}else if (mWay.equals("6")) {
currentWeek = "星期五";
}else if (mWay.equals("7")) {
currentWeek = "星期六";
}
return currentWeek;
}
/**
* 计算两个日期之间相差的天数
*
* @param smdate
* 较小的时间
* @param bdate
* 较大的时间
* @return 相差天数
* @throws ParseException
*/
public static int daysBetween(Date smdate, Date bdate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 字符串的日期格式的计算
* 时间格式:yyyyMMdd
* @param smdate
* 较小的时间
* @param bdate
* 较大的时间
* @return 相差天数
*/
public static int daysBetweenString(String smdate, String bdate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
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);
// System.out.println(between_days+"-------------");
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 字符串的日期格式的计算
* 时间格式:yyyyMMddHHmmss
* @param smdate
* 较小的时间
* @param bdate
* 较大的时间
* @return 相差天数
*/
public static int daysBetween(String smdate, String bdate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
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);
// System.out.println(between_days+"-------------");
return (int) between_days;
}
/**
* 字符串的日期格式的计算
* 时间格式:yyyyMMddHHmmss
* @param currentTime 当前的时间
* @param beginTime 开始的时间
* @param endTime 结束的时间
* @return boolean 当前日期是否在给定开始和结束日期之间
*/
public static boolean isCurrentTimeBetween(String currentTime,String beginTime,String endTime) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(currentTime));
long currentTimeTimeInMillis = cal.getTimeInMillis();
cal.setTime(sdf.parse(beginTime));
long beginTimeInMillis = cal.getTimeInMillis();
cal.setTime(sdf.parse(endTime));
long endTimeInMillis = cal.getTimeInMillis();
if (currentTimeTimeInMillis > beginTimeInMillis && currentTimeTimeInMillis < endTimeInMillis) {
return true;
}
return false;
}
}