`
- 浏览:
48902 次
- 性别:
- 来自:
福建
-
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
*
* 日期操作的工具类
*
* @author huwei(jshuwei.org.cn)
* @since 1.0
*
*/
public class DateUtil {
private static String defDtPtn = "yyyy-MM-dd HH:mm:ss";// 缺省日期格式
/**
* 计算给定时间至今的天数
*
* @since 1.1
* @param date
*给定的时间
* @return 给定时间至今的天数
*/
public static long date2day(String date) {
long day = 0;
DateFormat df = DateFormat.getDateInstance();
try {
long old = df.parse(date).getTime();
long now = new java.util.Date().getTime();
long secs = now - old;
day = secs / (1000 * 24 * 60 * 60);
} catch (ParseException e) {
e.printStackTrace();
}
return day;
}
/**
* 格式化给定时间后day天的时间
*
* @since 1.0
* @param date
*需要格式化的时间
* @param day
*增加的天数
* @return 给定时间的后day天的格式化字符串(如:2008-11-22)
*/
public static String formatDate(java.util.Date date, Integer day) {
String str = "";
str = new Date(date.getTime() day * 24 * 60 * 60).toString();
return str;
}
/**
* 格式化给定时间
*
* @param date
*需要格式化的时间
* @return 给定时间的格式化字符串(如:2008-11-22)
*/
public static String formatDate(java.util.Date date) {
return new Date(date.getTime()).toString();
}
/**
* 得到当前年
*
* @since 1.0
* @return 返回当前年(YYYY)
*/
public static int getYear() {
return Calendar.getInstance().get(Calendar.YEAR);
}
/**
* 得到当前月
*
* @since 1.0
* @return 返回当前月(1~12)
*/
public static int getMonth() {
return Calendar.getInstance().get(Calendar.MONTH) 1;
}
/**
* 得到当前日
*
* @since 1.0
* @return 返回当前日(1~31)
*/
public static int getDay() {
return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
}
/**
* 得到当前年
*
* @since 1.0
* @return 返回当前年(YY)
*/
public static String getYear2() {
int year = getYear();
return StringUtil.Integer2String(year, "1986").substring(2, 4);
}
/**
* 得到当前月
*
* @since 1.0
* @return 返回当前月(01~12)
*/
public static String getMonth2() {
int month = getMonth();
if (month < 10) {
return "0" StringUtil.Integer2String(month, "00");
}
return StringUtil.Integer2String(month, "00");
}
/**
* 得到当前日
*
* @since 1.0
* @return 返回当前日(01~31)
*/
public static String getDay2() {
int day = getDay();
if (day < 10) {
return "0" StringUtil.Integer2String(day, "00");
}
return StringUtil.Integer2String(day, "00");
}
/**
* 得到指定格式的当前时间
*
* @param format
*格式化形式(年用YY/YYYY表示;月用M/MM表示;日用D/DD表示,之间任意任序组合),<br />
*如"YYYY-MM-DD"返回形如:1986-12-17<br />
*如"YY-MM"返回形如:86-12<br />
*如"YY年MM月"返回形如:86年12月……
* @since 1.0
* @return 返回指定格式的当前时间
*
*/
public static String getDate(String format) {
format = format.replace("YYYY", getYear() "");
format = format.replace("YY", getYear2());
format = format.replace("MM", getMonth2());
format = format.replace("M", getMonth() "");
format = format.replace("DD", getDay2());
format = format.replace("D", getDay() "");
return format;
}
/**
* 将字符串按指定格式解析成日期对象
*
* @since 1.1
* @param dateStr
*需要进行转换的日期字符串
* @param pattern
*日期字符串的格式
* @return "yyyy-MM-dd HH:mm:ss"形式的日期对象
*/
public static java.util.Date parseDate(String dateStr, String pattern) {
SimpleDateFormat DATEFORMAT = new SimpleDateFormat(defDtPtn);
DATEFORMAT.applyPattern(pattern);
java.util.Date ret = null;
try {
ret = DATEFORMAT.parse(dateStr);
} catch (Exception e) {
e.printStackTrace();
}
DATEFORMAT.applyPattern(defDtPtn);
return ret;
}
/**
* 计算详细年龄
*
* @since 1.1
* @param birthdayStr
*出生日期 格式"YYYY-MM-DD"
* @return 指定日期至今天的年龄
*/
public static String countAge(String birthdayStr) {
String age = "";
if (birthdayStr != null && birthdayStr.length() == 8) {
java.util.Date birthday = parseDate(birthdayStr, "YYYY-MM-DD");
if (birthday != null) {
Calendar calendar = Calendar.getInstance();
int year1 = getYear();
int month1 = StringUtil.String2Integer(getMonth2(), 0);
int day1 = StringUtil.String2Integer(getDay2(), 00);
calendar.setTime(birthday);
int year2 = calendar.get(Calendar.YEAR);
int month2 = calendar.get(Calendar.MONTH) 1;
int day2 = calendar.get(Calendar.DATE);
int year = year1 - year2;
int month = month1 - month2;
int day = day1 - day2;
if (month < 0) {
year = year - 1;
month = 12 month1 - month2;
}
if (day < 0) {
month = month - 1;
if (month < 0) {
year = year - 1;
month = 11;
}
int lastMonthDay = 0;
if (month1 == 0) {
lastMonthDay = getDayOfMonth(12, year1 - 1);
} else {
lastMonthDay = getDayOfMonth(month1, year1);
}
day = lastMonthDay day1 - day2;
}
if (year > 5) {
age = year "岁";
} else if (year > 0) {
if (month == 0) {
age = year "岁";
} else {
age = year "岁" month "月";
}
} else {
if (month > 5) {
age = month "月";
} else if (month > 0) {
age = month "月" day "天";
} else {
age = day "天";
}
}
}
}
return age;
}
/**
* 得到指定年月的天数
*
* @since 1.1
* @param month
*指定月份
* @param year
*指定的年份
* @return 天数
*/
public static int getDayOfMonth(int month, int year) {
int ret = 0;
boolean flag = false;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
flag = true;
}
switch (month) {
case 1:
ret = 31;
break;
case 2:
if (flag) {
ret = 29;
} else {
ret = 28;
}
break;
case 3:
ret = 31;
break;
case 4:
ret = 30;
break;
case 5:
ret = 31;
break;
case 6:
ret = 30;
break;
case 7:
ret = 31;
break;
case 8:
ret = 31;
break;
case 9:
ret = 30;
break;
case 10:
ret = 31;
break;
case 11:
ret = 30;
break;
case 12:
ret = 31;
break;
default:
break;
}
return ret;
}
/**
* 计算某天是星期几
*
* @since 1.1
* @param p_date
*日期字符串
* @return 星期
*/
public static int whatDayIsSpecifyDate(String p_date) {
// 2002-2-22 is friday5
long differenceDays = 0L;
long m = 0L;
differenceDays = signDaysBetweenTowDate(p_date, "2002-2-22");
m = (differenceDays % 7);
m = m 5;
m = m > 7 ? m - 7 : m;
return Integer.parseInt(m "");
}
/**
* 计算两日期间相差天数.
*
* @since 1.1
* @param d1
*日期字符串
* @param d2
*日期字符串
* @return long 天数
*/
public static long signDaysBetweenTowDate(String d1, String d2) {
java.sql.Date dd1 = null;
java.sql.Date dd2 = null;
long result = -1l;
try {
dd1 = java.sql.Date.valueOf(d1);
dd2 = java.sql.Date.valueOf(d2);
result = signDaysBetweenTowDate(dd1, dd2);
} catch (Exception ex) {
result = -1;
}
return result;
}
/**
* 计算两日期间相差天数.
*
* @since 1.1
* @param d1
*开始日期 日期型
* @param d2
*结束日期 日期型
* @return long 天数
*/
public static long signDaysBetweenTowDate(java.sql.Date d1, java.sql.Date d2) {
return (d1.getTime() - d2.getTime()) / (3600 * 24 * 1000);
}
}
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
这是一个比较全面的处理日期时间的工具类,利用该工具类可以得到你想要的日期时间。里面代码简洁,方法已经封装好,只需要调用即可。自己可以将其打成jar包来用
非常强大的java时间处理工具类!.docx
99 非常强大的java时间处理工具类!.docx
7 非常强大的java时间处理工具类!.docx
处理date添加或减少天数 处理date添加或减少月份 处理date添加或减少年份
NULL 博文链接:https://topmanopensource.iteye.com/blog/434445
* 根据日历的规则,为基准时间添加指定日历字段的单个时间单元 * * @param field * 日历字段, 使用Calendar类定义的日历字段常量 * @param up * 指定日历字段的值的滚动方向。true:向上滚动 / false:向...
* @param 当前月份所在的时间 * @return 月份的最后一天 */ public static Date getLastDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.MONTH...
java 时间处理工具类,包含时间截取、转换,固定格式时间转换
J2EE常用工具类汇总 ...J2EE常用工具类—时间处理工具类 J2EE常用工具类—文件打包解包处理类 J2EE常用工具类——文件操作类 J2EE常用工具类——邮件处理类(javaMail发送邮件) J2EE常用工具类——字符串工具类
时间格式化工具类
没事写了一个关于java时间处理的工具类,很简单
DateUtil:时间处理工具类 DBConnection:jdbc工具类 FileOperater:文件处理工具类 包括 读取文本文件,写出文本文件, 大文件切分,文件下载,文件或文件夹比较,文件或文件夹遍历筛选 ...... HttpClientCard...
日期处理工具类,项目中的工具类,测试无bug,请放心使用
该Java工具类调用时只需要类名.方法即可调用。 1.返回纯数字的年月日时分秒时间戳:20191008103656 4.在日期上增加数个整月 5.在日期上增加天数 6.在日期上增加n分钟 7.两个时间相差距离多少天多少小时...
基本涵盖了各种场景的日期处理需要,包括时间类型转换,获取N天前后,月初月末,某段时间按天拆分等功能,欢迎使用。
一个关于时间处理的工具类,封装了常见的处理时间的方法。一个关于时间处理的工具类,封装了常见的处理时间的方法。一个关于时间处理的工具类,封装了常见的处理时间的方法。
Swift的现实世界日期处理工具类