Java时间处理
分类:Java
来源:网络
时间:2010-10-28 23:34:31
Java时间处理API很丰富,但有时候调用起来不方便。本文代码封装了一些常用的时间处理,方便开发时调用。
- package cn.org.jshuwei.j2ee.util;
- 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);
- }
- }
- 默认分类(20)
- J2EE(25)
- Java(56)
- PHP(55)
- SEO(10)
- 网页设计(20)
- 网站建设(37)
- 数据库(7)
- JavaScript(17)
- JQuery(6)
- MySQL(20)
- SQL Server(6)
- Access(1)
- Oracle(6)
- office(6)
- Dreamweaver(4)
- Photoshop(12)
- Flash(9)
- Fireworks(13)
- CSS(14)
- HTML(4)
- .NET(7)
- ASP(2)
- DB2(1)
- Ajax(2)
- Linux(12)
- Struts(7)
- Hibernate(8)
- Spring(2)
- Jsp(22)
- Asp(8)
- C#(3)
- C++(1)
- 网络安全(5)
- 软件工程(7)
- XML(1)
- English(2)
- 计算机等级考试(2)
- 计算机病毒(4)
- 个人日志(76)
- 互联网(15)
- ActionScript(10)
- Android(3)
- 数据结构与算法(1)
- 游戏策略(3)
- 美文翻译(2)
- 编程开发(19)
- 计算机应用(4)
- 计算机(10)
- Unity3d(6)
- 其他(1)
- egret(1)