首页 > 代码库 > date 工具类

date 工具类

package lizikj.bigwheel.common.vo.merchandise.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

/**
* 时间帮助类
*
*/
public class DateUtil {

final static Logger logger = Logger.getLogger(DateUtil.class);

/**
* 判断是否是周末
* @return
*/
public static boolean isWeek(){
Calendar calendar = Calendar.getInstance();
int week = calendar.get(Calendar.DAY_OF_WEEK)-1;
if(week==6||week==0){
return true;
}
return false;
}
/**
* 判断时间是否在时间段内
*
* @param date
* 当前时间 yyyy-MM-dd HH:mm:ss
* @param strDateBegin
* 开始时间 00:00:00
* @param strDateEnd
* 结束时间 00:05:00
* @return
*/
public static boolean isTimePeriod(Date date, String strDateBegin,
String strDateEnd) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = sdf.format(date);
// 截取当前时间时分秒
int strDateH = Integer.parseInt(strDate.substring(11, 13));
int strDateM = Integer.parseInt(strDate.substring(14, 16));
int strDateS = Integer.parseInt(strDate.substring(17, 19));
// 截取开始时间时分秒
int strDateBeginH = Integer.parseInt(strDateBegin.substring(0, 2));
int strDateBeginM = Integer.parseInt(strDateBegin.substring(3, 5));
int strDateBeginS = Integer.parseInt(strDateBegin.substring(6, 8));
// 截取结束时间时分秒
int strDateEndH = Integer.parseInt(strDateEnd.substring(0, 2));
int strDateEndM = Integer.parseInt(strDateEnd.substring(3, 5));
int strDateEndS = Integer.parseInt(strDateEnd.substring(6, 8));
if ((strDateH >= strDateBeginH && strDateH <= strDateEndH)) {
// 当前时间小时数在开始时间和结束时间小时数之间
if (strDateH > strDateBeginH && strDateH < strDateEndH) {
return true;
// 当前时间小时数等于开始时间小时数,分钟数在开始和结束之间
} else if (strDateH == strDateBeginH && strDateM >= strDateBeginM
&& strDateM <= strDateEndM) {
return true;
// 当前时间小时数等于开始时间小时数,分钟数等于开始时间分钟数,秒数在开始和结束之间
} else if (strDateH == strDateBeginH && strDateM == strDateBeginM
&& strDateS >= strDateBeginS && strDateS <= strDateEndS) {
return true;
}
// 当前时间小时数大等于开始时间小时数,等于结束时间小时数,分钟数小等于结束时间分钟数
else if (strDateH >= strDateBeginH && strDateH == strDateEndH
&& strDateM <= strDateEndM) {
return true;
// 当前时间小时数大等于开始时间小时数,等于结束时间小时数,分钟数等于结束时间分钟数,秒数小等于结束时间秒数
} else if (strDateH >= strDateBeginH && strDateH == strDateEndH
&& strDateM == strDateEndM && strDateS <= strDateEndS) {
return true;
} else {
return false;
}
} else {
return false;
}
}

/**
* 判断当前日期是否在一个日期范围内
* @param fromDate
* @param toDate
* @return
*/
public static boolean isDate(Date fromDate, Date toDate){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String d1 = sdf.format(new Date());//第一个时间

try {
Date testDate = sdf.parse(d1);
//1、 交换开始和结束日期
if (fromDate.getTime() > toDate.getTime()) {
Date tempDate = fromDate;
fromDate = toDate;
toDate = tempDate;
}
//2、缩小范围
long testDateTime = testDate.getTime();
if ( (testDateTime > fromDate.getTime() && testDateTime > toDate.getTime())
|| testDateTime < fromDate.getTime() && testDateTime < toDate.getTime()) {
return false;
}
} catch (Exception e) {
logger.error("isDate is error", e);
}
return true;
}
//最近一周
public static String getWeek(int num){
String result="";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cl = Calendar.getInstance();
cl.add(Calendar.WEEK_OF_YEAR, -1); //一周
Date dateFrom = cl.getTime();
result=sdf.format(dateFrom);
//System.out.println(sdf.format(dateFrom)+"到"+sdf.format(dateNow));
return result;
}
//最近一月
public static String getMonth(int num){
String result="";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cl = Calendar.getInstance();
cl.add(Calendar.MONTH, -num); //一个月
Date dateFrom = cl.getTime();
result=sdf.format(dateFrom);
return result;
}
//当前时间的前一天
public static Date getNextDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
date = calendar.getTime();
return date;
}

//判断是否在时间前后
public static int compare_date(String DATE1, String DATE2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(DATE2);
if (dt1.getTime() > dt2.getTime()) {
System.out.println("dt1 在dt2前");
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
System.out.println("dt1在dt2后");
return -1;
} else {
return 0;
}
} catch (Exception e) {
logger.error("compare_date is error", e);
}
return 0;
}

/**
* 判断指定日期是否在一个日期范围内
* @param fromDate 范围开始日期
* @param toDate 范围结束日期
* @param testDate 测试日期
* @return 在范围内true,否则false
*/
public static boolean betweenDays(Date fromDate, Date toDate, Date testDate) {
if (fromDate == null || toDate == null || testDate == null) {
return false;
}

//1、 交换开始和结束日期
if (fromDate.getTime() > toDate.getTime()) {
Date tempDate = fromDate;
fromDate = toDate;
toDate = tempDate;
}

//2、缩小范围
long testDateTime = testDate.getTime();
if ( (testDateTime > fromDate.getTime() && testDateTime > toDate.getTime())
|| testDateTime < fromDate.getTime() && testDateTime < toDate.getTime()) {
return false;
}

return true;
}

public static Date getDateTime(long now){
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(now);
System.out.println(now + " = " + formatter.format(calendar.getTime()));
String result=formatter.format(calendar.getTime());
return calendar.getTime();
}

public static Date transferLongToDate(Long millSec){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date= new Date(millSec);
return date;
}

public static void main(String[] args) {
String now="1475921070450";
System.out.println(transferLongToDate(Long.valueOf(now)));
}
}

date 工具类