欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java日期工具类实例分享

程序员文章站 2024-02-14 16:39:46
复制代码 代码如下:/** * 日期工具类 * 默认使用 "yyyy-mm-dd hh:mm:ss" 格式化日期 */public final...
复制代码 代码如下:

/**
 * 日期工具类
 * 默认使用 "yyyy-mm-dd hh:mm:ss" 格式化日期
 */
public final class dateutils {
/**
* 英文简写(默认)如:2010-12-01
*/
public static string format_short = "yyyy-mm-dd";
/**
* 英文全称  如:2010-12-01 23:15:06
*/
public static string format_long = "yyyy-mm-dd hh:mm:ss";
/**
* 精确到毫秒的完整时间    如:yyyy-mm-dd hh:mm:ss.s
*/
public static string format_full = "yyyy-mm-dd hh:mm:ss.s";
/**
* 中文简写  如:2010年12月01日
*/
public static string format_short_cn = "yyyy年mm月dd";
/**
* 中文全称  如:2010年12月01日  23时15分06秒
*/
public static string format_long_cn = "yyyy年mm月dd日  hh时mm分ss秒";
/**
* 精确到毫秒的完整中文时间
*/
public static string format_full_cn = "yyyy年mm月dd日  hh时mm分ss秒sss毫秒";
/**
* 获得默认的 date pattern
*/
public static string getdatepattern() {
return format_long;
}
/**
* 根据预设格式返回当前日期
* @return
*/
public static string getnow() {
return format(new date());
}
/**
* 根据用户格式返回当前日期
* @param format
* @return
*/
public static string getnow(string format) {
return format(new date(), format);
}
/**
* 使用预设格式格式化日期
* @param date
* @return
*/
public static string format(date date) {
return format(date, getdatepattern());
}
/**
* 使用用户格式格式化日期
* @param date 日期
* @param pattern 日期格式
* @return
*/
public static string format(date date, string pattern) {
string returnvalue = "";
if (date != null) {
simpledateformat df = new simpledateformat(pattern);
returnvalue = df.format(date);
}
return (returnvalue);
}
/**
* 使用预设格式提取字符串日期
* @param strdate 日期字符串
* @return
*/
public static date parse(string strdate) {
return parse(strdate, getdatepattern());
}
/**
* 使用用户格式提取字符串日期
* @param strdate 日期字符串
* @param pattern 日期格式
* @return
*/
public static date parse(string strdate, string pattern) {
simpledateformat df = new simpledateformat(pattern);
try {
return df.parse(strdate);
} catch (parseexception e) {
e.printstacktrace();
return null;
}
}
/**
* 在日期上增加数个整月
* @param date 日期
* @param n 要增加的月数
* @return
*/
public static date addmonth(date date, int n) {
calendar cal = calendar.getinstance();
cal.settime(date);
cal.add(calendar.month, n);
return cal.gettime();
}
/**
* 在日期上增加天数
* @param date 日期
* @param n 要增加的天数
* @return
*/
public static date addday(date date, int n) {
calendar cal = calendar.getinstance();
cal.settime(date);
cal.add(calendar.date, n);
return cal.gettime();
}
/**
* 获取时间戳
*/
public static string gettimestring() {
simpledateformat df = new simpledateformat(format_full);
calendar calendar = calendar.getinstance();
return df.format(calendar.gettime());
}
/**
* 获取日期年份
* @param date 日期
* @return
*/
public static string getyear(date date) {
return format(date).substring(0, 4);
}
/**
* 按默认格式的字符串距离今天的天数
* @param date 日期字符串
* @return
*/
public static int countdays (string date) {
long t = calendar.getinstance().gettime().gettime();
calendar c = calendar.getinstance();
c.settime(parse(date));
long t1 = c.gettime().gettime();
return (int)(t/1000 - t1/1000)/3600/24;
}
/**
* 按用户格式字符串距离今天的天数
* @param date 日期字符串
* @param format 日期格式
* @return
*/
public static int countdays (string date, string format) {
long t = calendar.getinstance().gettime().gettime();
calendar c = calendar.getinstance();
c.settime(parse(date, format));
long t1 = c.gettime().gettime();
return (int)(t/1000 - t1/1000)/3600/24;
}
}