时间处理函数工具分享(时间戳计算)
本文导语: 代码如下:import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.TimeZone; /** * 时间处理函数 * * @20080509 15:50 */public class DateUtil { private static final String...
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
/**
* 时间处理函数
*
* @20080509 15:50
*/
public class DateUtil {
private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static final String TIME_YEAR = "yyyy";
public static final String TIME_MONEN = "MM";
public static final String TIME_DAY = "dd";
public static String getDate(String interval, Date starttime, String pattern) {
Calendar temp = Calendar.getInstance(TimeZone.getDefault());
temp.setTime(starttime);
temp.add(temp.MONTH, Integer.parseInt(interval));
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(temp.getTime());
}
/**
* 将字符串类型转换为时间类型
*
* @return
*/
public static Date str2Date(String str) {
Date d = null;
SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
try {
d = sdf.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
return d;
}
public static Date str2Date(String str, String pattern) {
Date d = null;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
d = sdf.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
return d;
}
/**
* 将时间格式化
*
* @return
*/
public static Date DatePattern(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
try {
String dd = sdf.format(date);
date = str2Date(dd);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
/**
* 将时间格式化
*/
public static Date DatePattern(Date date, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
String dd = sdf.format(date);
date = str2Date(dd, pattern);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
public static String date2Str(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
return sdf.format(date);
}
public static String date2Str(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
/**
* 获取昨天
*
* @param date
* @return
* @throws Exception
*/
public static Date getLastDate(Date date) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.setTime(date);
calendar.add(calendar.DATE, -1);
return str2Date(date2Str(calendar.getTime()));
}
/**
* 获取前几天
* @param date
* @return
*/
public static Date getBeforeDate(Date date,int dates) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.setTime(date);
calendar.add(calendar.DATE, -dates);
return str2Date(date2Str(calendar.getTime()));
}
/**
* 获取上周第一天(周一)
*
* @param date
* @return
* @throws Exception
*/
public static Date getLastWeekStart(Date date) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.setTime(date);
int i = calendar.get(calendar.DAY_OF_WEEK) - 1;
int startnum = 0;
if (i == 0) {
startnum = 7 + 6;
} else {
startnum = 7 + i - 1;
}
calendar.add(calendar.DATE, -startnum);
return str2Date(date2Str(calendar.getTime()));
}
/**
* 获取上周最后一天(周末)
*
* @param date
* @return
* @throws Exception
*/
public static Date getLastWeekEnd(Date date) {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
calendar.setTime(date);
int i = calendar.get(calendar.DAY_OF_WEEK) - 1;
int endnum = 0;
if (i == 0) {
endnum = 7;
} else {
endnum = i;
}
calendar.add(calendar.DATE, -(endnum - 1));
return str2Date(date2Str(calendar.getTime()));
}
/**
* 根据年和月得到天数
* @param num 月
* @param year 年
* @return
*/
public static int getday(int num,int year){
if(num==1 || num==3 || num==5 || num==7 || num==8 || num==10 || num==12){
return 31;
}else if(num==2){
//判断是否为闰年
if(year%400==0 || (year%4==0 && year%100!=0)){
return 29;
}else{
return 28;
}
}else{
return 30;
}
}
/*
* 计算当前日期距离下个月还有多少天
*/
public static int getdaymis(Date time){
int year = Integer.parseInt(
new SimpleDateFormat(TIME_YEAR).format(time));//年
int mm = Integer.parseInt(
new SimpleDateFormat(TIME_MONEN).format(time));//月
int dd = Integer.parseInt(
new SimpleDateFormat(TIME_DAY).format(time));//日
//获取当前年月的总天数
int sdd = getday(mm,year);
return sdd-dd;
}
/**
* 日期转秒数
* @param dateString
* @return
*/
public static long getTime(String dateString) {
long time = 0;
try {
Date ret = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ret = sdf.parse(dateString);
time = ret.getTime()/1000;
} catch (Exception e) {
}
return time;
}
/**
* 精确计算时间差,精确到日
* @param fistill 起始日期
* @param nowtime 结束日期
* @param type type为1返回年月日(如:2年3个月零5天) 否则返回总的天数
* @return
*/
public static String patienage(Date fistill,Date nowtime,Integer type){
int fyear = Integer.parseInt(
new SimpleDateFormat(TIME_YEAR).format(fistill));//起始年
int fmm = Integer.parseInt(
new SimpleDateFormat(TIME_MONEN).format(fistill));//起始月
int fdd = Integer.parseInt(
new SimpleDateFormat(TIME_DAY).format(fistill));//起始日
int nyear = Integer.parseInt(
new SimpleDateFormat(TIME_YEAR).format(nowtime));//结束年
int nmm = Integer.parseInt(
new SimpleDateFormat(TIME_MONEN).format(nowtime));//结束月
int ndd = Integer.parseInt(
new SimpleDateFormat(TIME_DAY).format(nowtime));//结束日
int cyear = nyear - fyear;
int cmm = nmm - fmm;
int cdd = ndd - fdd;
int zyear = 0;
int zmm = 0;
int zdd = 0;
int countddd = 0; //年月日累计天数
if(cdd