import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class DateFormat {
//时间格式
private static String DATA_FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
// private static String DATA_FORMAT_YMD = "yyyy-MM-dd";
private static String DATA_FORMAT_YMDHM = "yyyy-MM-dd HH:mm";

public static String convertStr(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(DATA_FORMAT_YMDHMS);
String dstr = sdf.format(date);
return dstr;
}

/**
* @function strConvertDate
* @description 字符串("yyyy-MM-dd HH:mm:ss")转日期
* @throws ParseException
* @author maohuidong
* @date 2017-05-02
*/
public static Date strConvertDate(String str) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat(DATA_FORMAT_YMDHMS);
Date date = sdf.parse(str);
return date;
}
public static String convertStr(Date date,String formatStr) {
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
String dstr = sdf.format(date);
return dstr;
}
//今日零点
public static String getZeroDay() {
long current = System.currentTimeMillis();//当前时间毫秒数
long zero = current / (1000*3600*24) * (1000*3600*24) - TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
String dstr = DateFormat.convertStr(new Date(zero));
return dstr;
}

//今日23点59分59秒
public static String getTwelveDay() {
long current = System.currentTimeMillis();//当前时间毫秒数
long twelve = current / (1000*3600*24) * (1000*3600*24) - TimeZone.getDefault().getRawOffset() + 24*60*60*1000 - 1;//今天23点59分59秒的毫秒数
String dstr = DateFormat.convertStr(new Date(twelve));
return dstr;
}

/**
* @function compareDate
* @description 比较两个时间的大小
* @param dateStr1 时间1
* @param datestr2 时间2
* @return boolean 时间1大于时间2,返回true
* @throws ParseException
* @author jiangqiwen
* @date 2017-02-23
*/
public static boolean compareDate(String dateStr1, String datestr2) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(DATA_FORMAT_YMDHMS);
Date dt1 = sdf.parse(dateStr1);
Date dt2 = sdf.parse(datestr2);
return dt1.after(dt2);
}

/**
* @function getMinutesDiff
* @description 比较两个时间的分钟差(参考网址 http://blog.csdn.net/jeffleo/article/details/52175998)
* @param fromDate 起始时间
* @param toDate 结束时间
* @return int 分钟差
* @throws ParseException
* @author jiangqiwen
* @date 2017-03-01
*/
public static int getMinutesDiff(String fromDate, String toDate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(DATA_FORMAT_YMDHM);
long from = sdf.parse(fromDate).getTime();
long to = sdf.parse(toDate).getTime();
int minutes = (int) ((to - from)/(1000 * 60));
return minutes;
}

/**
* @function getMinOrSecDiffWithUnits
* @description 比较两个时间差
* @param fromDate 起始时间
* @param toDate 结束时间
* @return String 有单位的时间差
* @throws ParseException
* @author jiangqiwen
* @date 2017-04-24
*/
public static String getMinOrSecDiffWithUnits(String fromDate, String toDate) throws ParseException {
String units;
SimpleDateFormat sdf = new SimpleDateFormat(DATA_FORMAT_YMDHMS);
long from = sdf.parse(fromDate).getTime();
long to = sdf.parse(toDate).getTime();
double timeDiff = (double) (to - from) / (1000 * 60);
if (timeDiff < 1) {
units = "秒";
return ((int)(timeDiff * 60)) + units;
} else {
units = "分钟";
return ((int)(timeDiff)) + units;
}
}
}

posted on 2017-12-18 09:39  毛会懂  阅读(254)  评论(0编辑  收藏  举报