用Java实现处理日期的工具类——常用日期处理方法

时间:2022-04-29
本文章向大家介绍用Java实现处理日期的工具类——常用日期处理方法,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

日期处理是开发过程中经常遇到的问题,以下是总结了开发中常用的方法,代码如下:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
*
* @项目名 ssh
* @功能 处理日期的工具类
* @类名 DateUtils
* @作者 Java自学通
* @日期 Aug 30, 20113:35:30 PM
* @版本 1.0
*/
public final class DateUtils {
private static SimpleDateFormat format = new SimpleDateFormat(
"yyyy年MM月dd日HH时mm分ss�?");
private DateUtils() {
}
public static SimpleDateFormat getFormat() {
return format;
}
/**
* 根据日期得到星期中的天数
*
* @param date
* 日期
* @return 星期�?
*/
public static String formatWeek(String date) {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
Date d = null;
try {
d = sd.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return getFormatWeek(d);
}
/**
* 设置默认格式�?
*
* @param format
* 格式
*/
public static void setFormat(SimpleDateFormat format) {
DateUtils.format = format;
}
/**
* 得到当前日期
*
* @return
*/
public static Date getNowDate() {
return new Date();
}
/**
* 设置默认格式
*
* @param format
* 格式
*/
public static void setFormat(String format) {
DateUtils.format = new SimpleDateFormat(format);
}
/**
* 得到格式化后的日�?
*
* @param date
* 日期
* @return 日期
*/
public static String getFormatDate(Date date) {
return format.format(date);
}
/**
* 得到格式化后的日�?
*
* @param date
* @param foramt
* @return
*/
public static String getFormatDate(Date date, String foramt) {
return new SimpleDateFormat(foramt).format(date);
}
/**
* 得到格式化后的日�?
*
* @param date
* @return
* @throws ParseException
*/
public static String getFormatDate(String date) throws ParseException {
return format.format(format.parse(date));
}
/**
* 得到与当前小时间�?
*
* @param date
* 日期
* @return
*/
public static long getHourSpacing(Date date) {
long hour = 60l * 60l * 1000l;
return (new Date().getTime() - date.getTime()) / hour;
}
/**
* 得到两个日期的小时间�?
*
* @param before
* 之前的日�?
* @param after
* 之后的日�?
* @return 小时间隔
*/
public static long getHourSpacing(Date before, Date after) {
long hour = 60l * 60l * 1000l;
return (after.getTime() - before.getTime()) / hour;
}
/**
* 得到与当前日期小时间�?
*
* @param date
* @param fmt
* @return
* @throws ParseException
*/
public static long getHourSpacing(String date, String fmt)
throws ParseException {
long hour = 60l * 60l * 1000l;
setFormat(fmt);
Date temp = format.parse(date);
return (new Date().getTime() - temp.getTime()) / hour;
}
public static String passTime(Date oldTime) throws Exception {
long between = DateUtils.countTime(oldTime);
long day1 = between / (24 * 3600);
long hour1 = between % (24 * 3600) / 3600;
long minute1 = between % 3600 / 60;
long second1 = between;
String result = "刚刚";
if (day1 == 0 && hour1 == 0 && minute1 == 0 && second1 != 0) {
result = String.valueOf(second1) + "秒前";
}
if (day1 == 0 && hour1 == 0 && minute1 != 0) {
result = String.valueOf(minute1) + "分前";
}
if (day1 == 0 && hour1 != 0) {
result = String.valueOf(hour1) + "小时前";
}
if (day1 != 0) {
result = String.valueOf(day1) + "天前";
}
return result;
}
// 计算时间;
private static long countTime(Date oldTime) throws Exception {
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date begin = dfs.parse(dfs.format(oldTime).toString());
java.util.Date end = dfs.parse(dfs.format(new Date()).toString());
long between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
return between;
}
/**
* 得到時間間隔
*
* @param before
* @param after
* @param timeMillis
* @return
*/
public static long getTimeSpacing(Date before, Date after, long timeMillis) {
return before.getTime() - after.getTime() > 0 ? (before.getTime() - after
.getTime())
/ timeMillis
: (after.getTime() - before.getTime()) / timeMillis;
}
/**
* 得到時間間隔
*
* @param date
* @param timeMillis
* @return
*/
public static long getTimeSpacing(Date date, long timeMillis) {
Date da = new Date();
return da.getTime() - date.getTime() > 0 ? (da.getTime() - date
.getTime())
/ timeMillis : (date.getTime() - da.getTime()) / timeMillis;
}
/**
* 根據年月得到天數
*
* @param year
* @param month
* @return
*/
public static int daysOfMonth(int year, int month) {
int days[] = { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (2 == month && 0 == (year % 4)
&& (0 != (year % 100) || 0 == (year % 400))) {
days[2] = 29;
}
return (days[month]);
}
/**
* 將字符格式轉換成日期格式
*
* @param content
* @param format
* @return
* @throws ParseException
*/
public static Date formatDate(String content, String format)
throws ParseException {
SimpleDateFormat sf = new SimpleDateFormat(format);
return sf.parse(content);
}
/**
* 格式轉換
*
* @param content
* @param format1
* @return
*/
public static String getFormatDate(String content, String format1) {
SimpleDateFormat sf = new SimpleDateFormat(format1);
Date date;
try {
date = format.parse(content);
} catch (ParseException e) {
date = new Date();
}
return sf.format(date);
}
/**
* 根据日期得到星期�?
*
* @param date
* 日期
* @return 星期�?
*/
public static String getFormatWeek(Date date) {
SimpleDateFormat sdw = new SimpleDateFormat("E");
return sdw.format(date);
}
/**
* 根据当前时间获得昨天日期;
*
* @return
*/
public static Date getYesterday(Date time) {
Calendar cd = Calendar.getInstance();
cd.setTime(time);
cd.add(Calendar.DATE, -1);
return cd.getTime();
}
/**
* 根据当前时间获得明天日期;
*
* @return
*/
public static Date getTomorrow(Date time) {
Calendar cd = Calendar.getInstance();
cd.setTime(time);
cd.add(Calendar.DATE, 1);
return cd.getTime();
}
/**
* 当前时间加的天数;
*
* @param time
* 当前时间;
* @param day
* 相加的天数
* @return
*/
public static Date addNowTimeDay(Date time, int day) {
SimpleDateFormat e = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = time;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(calendar.DAY_OF_MONTH, +day);
date = calendar.getTime();
return date;
}
}