小编给大家分享一下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 { public static final String PATTERN_YMD = "yyyy-MM-dd"; public static final String PATTERN_YMDHMS="yyyy-MM-dd HH:mm:ss"; public static String formatDate(Date date) { return formatDate(date, PATTERN_YMD); } 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); } public static Date parseDate(String dateValue) { return parseDate(dateValue, null); } 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; } public static Date addYears(Date date, int amount) { return add(date, Calendar.YEAR, amount); } public static Timestamp addYears(Timestamp timestamp, int amount) { return add(timestamp, Calendar.YEAR, amount); } //----------------------------------------------------------------------- public static Date addMonths(Date date, int amount) { return add(date, Calendar.MONTH, amount); } public static Timestamp addMonths(Timestamp timestamp, int amount) { return add(timestamp, Calendar.MONTH, amount); } //----------------------------------------------------------------------- public static Date addDays(Date date, int amount) { return add(date, Calendar.DATE, amount); } public static Timestamp addDays(Timestamp timestamp, int amount) { return add(timestamp, Calendar.DATE, amount); } //----------------------------------------------------------------------- public static Timestamp addMinutes(Timestamp timestamp, int amount) { return add(timestamp, Calendar.MINUTE, amount); } public static Timestamp addDays(int amount) { Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, amount); return new Timestamp(c.getTimeInMillis()); } //----------------------------------------------------------------------- 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(); } 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()); } 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()); } private DateUtils() { } }
以上是“Java如何实现日期处理工具类DateUtils”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!