Java时间工具类
来源:互联网 发布:linux下测试网速 编辑:程序博客网 时间:2024/05/16 16:16
- 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;
- }
-
- }