Java日期处理工具类DateUtils详解
程序员文章站
2023-12-04 10:58:34
本文实例为大家分享了java日期处理工具类dateutils的具体代码,供大家参考,具体内容如下
import java.sql.timestamp;
imp...
本文实例为大家分享了java日期处理工具类dateutils的具体代码,供大家参考,具体内容如下
import java.sql.timestamp; import java.text.parseexception; import java.text.simpledateformat; import java.util.calendar; import java.util.date; /** * <日期时间处理工具类> */ public class dateutils { /** * date format pattern this is often used. */ public static final string pattern_ymd = "yyyy-mm-dd"; /** * date format pattern this is often used. */ public static final string pattern_ymdhms="yyyy-mm-dd hh:mm:ss"; /** * formats the given date according to the ymd pattern. * * @param date the date to format. * @return an ymd formatted date string. * * @see #pattern_ymd */ public static string formatdate(date date) { return formatdate(date, pattern_ymd); } /** * formats the given date according to the specified pattern. the pattern * must conform to that used by the {@link simpledateformat simple date * format} class. * * @param date the date to format. * @param pattern the pattern to use for formatting the date. * @return a formatted date string. * * @throws illegalargumentexception if the given date pattern is invalid. * * @see simpledateformat */ public static string formatdate(date date, string pattern) { if (date == null) throw new illegalargumentexception("date is null"); if (pattern == null) throw new illegalargumentexception("pattern is null"); simpledateformat formatter = new simpledateformat(pattern); return formatter.format(date); } /** * parses a date value. the format used for parsing the date value are retrieved from * the default pattern_ymd. * * @param datevalue the date value to parse * * @return the parsed date * * @throws illegalargumentexception if the given datevalue is invalid. */ public static date parsedate(string datevalue) { return parsedate(datevalue, null); } /** * parses the date value using the given date format. * * @param datevalue the date value to parse * @param dateformat the date format to use * * @return the parsed date. if parse is failed , return null * * @throws illegalargumentexception if the given datevalue is invalid. */ public static date parsedate(string datevalue, string dateformat) { if (datevalue == null) { throw new illegalargumentexception("datevalue is null"); } if (dateformat == null) { dateformat = pattern_ymd; } simpledateformat df = new simpledateformat(dateformat); date result = null; try { result = df.parse(datevalue); } catch (parseexception pe) { pe.printstacktrace();// 日期型字符串格式错误 } return result; } /** * adds a number of years to a date returning a new object. * the original date object is unchanged. * * @param date the date, not null * @param amount the amount to add, may be negative * @return the new date object with the amount added * @throws illegalargumentexception if the date is null */ public static date addyears(date date, int amount) { return add(date, calendar.year, amount); } /** * adds a number of years to a timestamp returning a new object. * the original timestamp object is unchanged. * * @param timestamp the timestamp, not null * @param amount the amount to add, may be negative * @return the new timestamp object with the amount added * @throws illegalargumentexception if the timestamp is null */ public static timestamp addyears(timestamp timestamp, int amount) { return add(timestamp, calendar.year, amount); } //----------------------------------------------------------------------- /** * adds a number of months to a date returning a new object. * the original date object is unchanged. * * @param date the date, not null * @param amount the amount to add, may be negative * @return the new date object with the amount added * @throws illegalargumentexception if the date is null */ public static date addmonths(date date, int amount) { return add(date, calendar.month, amount); } /** * adds a number of months to a timestamp returning a new object. * the original timestamp object is unchanged. * * @param timestamp the timestamp, not null * @param amount the amount to add, may be negative * @return the new timestamp object with the amount added * @throws illegalargumentexception if the timestamp is null */ public static timestamp addmonths(timestamp timestamp, int amount) { return add(timestamp, calendar.month, amount); } //----------------------------------------------------------------------- /** * adds a number of days to a date returning a new object. * the original date object is unchanged. * * @param date the date, not null * @param amount the amount to add, may be negative * @return the new date object with the amount added * @throws illegalargumentexception if the date is null */ public static date adddays(date date, int amount) { return add(date, calendar.date, amount); } /** * adds a number of days to a timestamp returning a new object. * the original timestamp object is unchanged. * * @param timestamp the timestamp, not null * @param amount the amount to add, may be negative * @return the new timestamp object with the amount added * @throws illegalargumentexception if the timestamp is null */ public static timestamp adddays(timestamp timestamp, int amount) { return add(timestamp, calendar.date, amount); } //----------------------------------------------------------------------- /** * adds a number of minutes to a timestamp returning a new object. * the original timestamp object is unchanged. * * @param timestamp the timestamp, not null * @param amount the amount to add, may be negative * @return the new timestamp object with the amount added * @throws illegalargumentexception if the timestamp is null */ public static timestamp addminutes(timestamp timestamp, int amount) { return add(timestamp, calendar.minute, amount); } /** * adds a number of days to current time returning a new object. * * @param amount the amount to add, may be negative * @return the new timestamp object with the amount added */ public static timestamp adddays(int amount) { calendar c = calendar.getinstance(); c.add(calendar.date, amount); return new timestamp(c.gettimeinmillis()); } //----------------------------------------------------------------------- /** * adds to a date returning a new object. * the original date object is unchanged. * * @param date the date, not null * @param calendarfield the calendar field to add to * @param amount the amount to add, may be negative * @return the new date object with the amount added * @throws illegalargumentexception if the date is null */ private static date add(date date, int calendarfield, int amount) { if (date == null) { throw new illegalargumentexception("the date must not be null"); } calendar c = calendar.getinstance(); c.settime(date); c.add(calendarfield, amount); return c.gettime(); } /** * adds to a timestamp returning a new object. * the original timestamp object is unchanged. * * @param timestamp the timestamp, not null * @param calendarfield the calendar field to add to * @param amount the amount to add, may be negative * @return the new timestamp object with the amount added * @throws illegalargumentexception if the timestamp is null */ private static timestamp add(timestamp timestamp, int calendarfield, int amount) { if (timestamp == null) { throw new illegalargumentexception("the timestamp must not be null"); } calendar c = calendar.getinstance(); c.settime(timestamp); c.add(calendarfield, amount); return new timestamp(c.gettimeinmillis()); } /** * <生成最小的当天日期值> * @return 最小的当天日期值 */ public static timestamp now() { calendar c = calendar.getinstance(); c.set(calendar.hour_of_day, 0); c.set(calendar.minute, 0); c.set(calendar.second, 0); c.set(calendar.millisecond, 0); return new timestamp(c.gettimeinmillis()); } /** this class should not be instantiated. */ private dateutils() { } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。