你的位置:首页 > Java教程

[Java教程]java日常时间处理


private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss" };

/**
* 得到当前日期字符串 格式(yyyy-MM-dd)
*/
public static String getDate() {
return getDate("yyyy-MM-dd");
}

/**
* 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String getDate(String pattern) {
return DateFormatUtils.format(new Date(), pattern);
}

/**
* 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String formatDate(Date date, Object... pattern) {
String formatDate = null;
if (pattern != null && pattern.length > 0) {
formatDate = DateFormatUtils.format(date, pattern[0].toString());
} else {
formatDate = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
}
return formatDate;
}

/**
* 得到当前时间字符串 格式(HH:mm:ss)
*/
public static String getTime() {
return formatDate(new Date(), "HH:mm:ss");
}

/**
* 得到当前日期和时间字符串 格式(yyyy-MM-dd HH:mm:ss)
*/
public static String getDateTime() {
return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
}

/**
* 得到当前年份字符串 格式(yyyy)
*/
public static String getYear() {
return formatDate(new Date(), "yyyy");
}

/**
* 得到当前月份字符串 格式(MM)
*/
public static String getMonth() {
return formatDate(new Date(), "MM");
}

/**
* 得到当天字符串 格式(dd)
*/
public static String getDay() {
return formatDate(new Date(), "dd");
}

/**
* 得到当前星期字符串 格式(E)星期几
*/
public static String getWeek() {
return formatDate(new Date(), "E");
}

/**
* 得到当前星期字符串 格式(HH)小时
*/
public static String getHour() {
return formatDate(new Date(), "HH");
}

/**
* 日期型字符串转化为日期 格式("yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss" )
*/
public static Date parseDate(String str) {
try {
return parseDate(str, parsePatterns);
} catch (ParseException e) {
return null;
}
}

/**
* 获取过去的天数
* @param date
* @return
*/
public static long pastDays(Date date) {
long t = new Date().getTime()-date.getTime();
return t/(24*60*60*1000);
}

/**
* 获得指定日期的前一天
*/
public static String getSpecifiedDayBefore(String specifiedDay, int number) {
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day - number);

String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayBefore;
}

/**
* 获得指定日期的后一天
*/
public static String getSpecifiedDayAfter(String specifiedDay, int number) {
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day + number);

String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayAfter;
}

/**
* 获得当天时间的零点
*/
public static String getTimeOf24() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime());
return time;
}

/**
* 格式时间
*
* @param date
* @param format
* @return
*/
public static String getFormatDate(Date date, String format) {
if (date != null) {
SimpleDateFormat f = new SimpleDateFormat(format);
return f.format(date);
} else {
return null;
}
}

// 计算两个日期之间有多少天
public static int getDaysBetween(Date startDate, Date endDate) {
Calendar d1 = Calendar.getInstance();
d1.setTime(startDate);
Calendar d2 = Calendar.getInstance();
d2.setTime(endDate);
if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
java.util.Calendar swap = d1;
d1 = d2;
d2 = swap;
}
int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
int y2 = d2.get(Calendar.YEAR);
if (d1.get(Calendar.YEAR) != y2) {
d1 = (Calendar) d1.clone();
do {
days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);// 得到当年的实际天数
d1.add(Calendar.YEAR, 1);
} while (d1.get(Calendar.YEAR) != y2);
}
return days;
}


/** 计算两个时间戳之间的天,时,分,秒数 */
public static String getDetailsDaysBetween(Long startTime,Long endTime){
Long interval = endTime - startTime;

int ss = 1000;
int mi = ss * 60;
int hh = mi * 60;
int dd = hh * 24;

long day = interval / dd;
long hour = (interval - day * dd) / hh;
long minute = (interval - day * dd - hour * hh) / mi;
long second = (interval - day * dd - hour * hh - minute * mi) / ss;
long milliSecond = interval - day * dd - hour * hh - minute * mi - second * ss;

String strDay = day < 10 ? "0" + day : "" + day; //天
String strHour = hour < 10 ? "0" + hour : "" + hour;//小时
//String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
//String strSecond = second < 10 ? "0" + second : "" + second;//秒
String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒
strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;
return strDay + "天" + strHour + "小时";
}