一个Java时间工具类
- 摘要:主要包含了对时间操作的一些工具方法,仅供参考吧!源码如下:Java代码packagecom..util;importjava.sql.Timestamp;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;publicclassDataUtil{/***功能:将日期对象按照某种格式进行转换,返回转换后的字符串**@paramdate日期对象*@parampattern转换格式例:yyyy
- 标签:工具 Java 一个
主要包含了对时间操作的一些工具方法,仅供参考吧!
源码如下:
Java代码??
- package?com..util;??
- ??
- import?java.sql.Timestamp;??
- import?java.text.SimpleDateFormat;??
- import?java.util.Calendar;??
- import?java.util.Date;??
- ??
- public?class?DataUtil?{??
- ??
- ?????
- ?
- ?
- ?
- ?
- ??
- ????public?static?String?DateToString(Date?date,?String?pattern)?{??
- ????????String?strDateTime?=?null;??
- ????????SimpleDateFormat?formater?=?new?SimpleDateFormat(pattern);??
- ????????strDateTime?=?date?==?null???null?:?formater.format(date);??
- ????????return?strDateTime;??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ??
- ????public?static?String?DateToString(Date?date)?{??
- ????????String?_pattern?=?"yyyy-MM-dd";??
- ????????return?date?==?null???null?:?DateToString(date,?_pattern);??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ??
- ????public?static?String?DateTimeToString(Date?date)?{??
- ????????String?_pattern?=?"yyyy-MM-dd?HH:mm:ss";??
- ????????return?date?==?null???null?:?DateToString(date,?_pattern);??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ?
- ??
- ????public?static?Date?StringToDate(String?str,?String?pattern)?{??
- ????????Date?dateTime?=?null;??
- ????????try?{??
- ????????????if?(str?!=?null?&&?!str.equals(""))?{??
- ????????????????SimpleDateFormat?formater?=?new?SimpleDateFormat(pattern);??
- ????????????????dateTime?=?formater.parse(str);??
- ????????????}??
- ????????}?catch?(Exception?ex)?{??
- ????????}??
- ????????return?dateTime;??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ??
- ????public?static?Date?StringToDate(String?str)?{??
- ????????String?_pattern?=?"yyyy-MM-dd";??
- ????????return?StringToDate(str,?_pattern);??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ??
- ????public?static?Date?StringToDateTime(String?str)?{??
- ????????String?_pattern?=?"yyyy-MM-dd?HH:mm:ss";??
- ????????return?StringToDate(str,?_pattern);??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ?
- ?
- ??
- ????public?static?Timestamp?StringToDateHMS(String?str)?throws?Exception?{??
- ????????Timestamp?time?=?null;??
- ????????time?=?Timestamp.valueOf(str);??
- ????????return?time;??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ?
- ?
- ??
- ????public?static?Date?YmdToDate(int?year,?int?month,?int?day)?{??
- ????????Calendar?calendar?=?Calendar.getInstance();??
- ????????calendar.set(year,?month,?day);??
- ????????return?calendar.getTime();??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ??
- ????public?static?String?communityDateToString(Date?date)?{??
- ????????SimpleDateFormat?formater?=?new?SimpleDateFormat("MM/dd?HH:mm:ss");??
- ????????String?strDateTime?=?date?==?null???null?:?formater.format(date);??
- ????????return?strDateTime;??
- ????}??
- ??
- ????public?static?Date?getMaxDateOfDay(Date?date)?{??
- ????????if?(date?==?null)?{??
- ????????????return?null;??
- ????????}?else?{??
- ????????????Calendar?calendar?=?Calendar.getInstance();??
- ????????????calendar.setTime(date);??
- ????????????calendar.set(11,?calendar.getActualMaximum(11));??
- ????????????calendar.set(12,?calendar.getActualMaximum(12));??
- ????????????calendar.set(13,?calendar.getActualMaximum(13));??
- ????????????calendar.set(14,?calendar.getActualMaximum(14));??
- ????????????return?calendar.getTime();??
- ????????}??
- ????}??
- ??
- ????public?static?Date?getMinDateOfDay(Date?date)?{??
- ????????if?(date?==?null)?{??
- ????????????return?null;??
- ????????}?else?{??
- ????????????Calendar?calendar?=?Calendar.getInstance();??
- ????????????calendar.setTime(date);??
- ????????????calendar.set(11,?calendar.getActualMinimum(11));??
- ????????????calendar.set(12,?calendar.getActualMinimum(12));??
- ????????????calendar.set(13,?calendar.getActualMinimum(13));??
- ????????????calendar.set(14,?calendar.getActualMinimum(14));??
- ????????????return?calendar.getTime();??
- ????????}??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ?
- ?
- ??
- ????public?static?Date?getAfterDay(Date?date,?int?afterDays)?{??
- ????????Calendar?cal?=?Calendar.getInstance();??
- ????????cal.setTime(date);??
- ????????cal.add(Calendar.DATE,?1);??
- ????????return?cal.getTime();??
- ????}??
- ??
- ??????
- ?????
- ?
- ?
- ?
- ?
- ?
- ??
- ????public?static?int?DateDiff(Date?date1,?Date?date2)?{??
- ????????int?i?=?(int)?((date1.getTime()?-?date2.getTime())?/?3600?/?24?/?1000);??
- ????????return?i;??
- ????}??
- ??
- ??????
- ?????
- ?
- ?
- ?
- ?
- ?
- ??
- ????public?static?int?MinDiff(Date?date1,?Date?date2)?{??
- ????????int?i?=?(int)?((date1.getTime()?-?date2.getTime())?/?1000?/?60);??
- ????????return?i;??
- ????}??
- ??
- ??????
- ?????
- ?
- ?
- ?
- ?
- ?
- ??
- ????public?static?int?TimeDiff(Date?date1,?Date?date2)?{??
- ????????int?i?=?(int)?((date1.getTime()?-?date2.getTime()));??
- ????????return?i;??
- ????}??
- ??
- }??
?
-
DataUtil.rar?(1.2 KB)
- 下载次数: 12