Java系统中时间封装处理

时间:2021-12-10 23:38:46

封装时间操作

package com.cloud.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
/* 私有构造方法,工具类不需要实例化 */
private DateUtil(){}
/* 判断是否是时间类型 */
public static boolean idDate(String dateStr){
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.parse(dateStr);
return true;
} catch (ParseException e) {
return false;
}
}
/* 格式化日期 */
public static Date parseDate(String dateStr, String format) {
Date date = null;
try {
java.text.DateFormat df = new java.text.SimpleDateFormat(format);
String dt = (dateStr).replaceAll("-", "/");
if ((!dt.equals("")) && (dt.length() < format.length())) {
dt += format.substring(dt.length()).replaceAll("[YyMmDdHhSs]","0");
}
date = (Date) df.parse(dt);
} catch (Exception e) {
}
return date;
}
/* 格式化日期:yyyy/MM/dd 形式 */
public static Date parseDate(String dateStr){
return parseDate(dateStr.replace("-", "/"),"yyyy/MM/dd");
}
/* 格式化日期:java.sql.Date 类型转换为 java.util.Date类型 */
public static Date parseDate(java.sql.Date date){
return date;
}
/* 格式化日期: java.util.Date类型转换java.sql.Date类型 */
public static java.sql.Date parseSqlDate(Date date){
if(date!=null)
return new java.sql.Date(date.getTime());
else
return null;
}
/* 根据String日期,并按照格式,返回SQL日期 */
public static java.sql.Date parseSqlDate(String dateStr,String format){
Date date = parseDate(dateStr, format);
return parseSqlDate(date);
}
/* 根据String日期,返回SQL日期 */
public static java.sql.Date parseSqlDate(String dateStr){
return parseSqlDate(dateStr, "yyyy/MM/dd");
}
/* 根据String日期,并根据一定的格式转换成java.sql.Timestamp的日期 */
public static java.sql.Timestamp parseTimestamp(String dateStr,String format){
Date date = parseDate(dateStr, format);
if(date!=null){
long time = date.getTime();
return new java.sql.Timestamp(time);
} else
return null;
}
/* 根据dateStr 字符串日期 返回yyyy/MM/dd HH:mm:ss 格式的java.sql.Timestamp日期 */
public static java.sql.Timestamp parseTimestamp(String dateStr){
return parseTimestamp(dateStr.replace("-", "/"), "yyyy/MM/dd HH:mm:ss");
}
/* 格式化date类型为String类型日期格式 */
public static String format(Date date,String format){
String result = "";
try{
if(date!=null){
DateFormat df = new SimpleDateFormat(format);
result = df.format(date);
}
} catch(Exception e){}
return result;
}
/* 格式化date类型 */
public static String format(Date date){
return format(date,"yyyy/MM/dd");
}
/* 获取日期的年份 */
public static int getYear(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.YEAR);
}
/* 获取日期的月份 */
public static int getMonth(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.MONTH)+1;
}
/* 获取日期的天份 */
public static int getDay(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.DAY_OF_MONTH);
}
/* 获取小时 */
public static int getHour(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.HOUR_OF_DAY);
}
/* 获取分钟 */
public static int getMinute(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.MINUTE);
}
/* 获取秒钟 */
public static int getSecond(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.SECOND);
}
/* 获取毫秒 */
public static long getMillis(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
/* 返回字符类型时间 */
public static String getTime(Date date){
return format(date, "HH:mm:ss");
}
/* 返回字符类型的日期和时间 */
public static String getDateTime(Date date){
return format(date, "yyyy/MM/dd HH:mm:ss");
}
/* 返回日期相加后的日期 */
public static Date addDate(Date date,int day){
Calendar c = Calendar.getInstance();
c.setTimeInMillis(getMillis(date)+((long) day) * 24 * 3600 * 1000);
return c.getTime();
}
/* 两个日期相减 */
public static int diffDate(Date date,Date date1){
return (int)((getMinute(date)-getMillis(date1))/(24 * 3600 * 1000));
}
}

简单测试

package com.cloud.test;

import java.util.Date;
import com.cloud.util.DateUtil;
public class TestDateUtil {
public static void main(String[] args) {

boolean flag = DateUtil.idDate("2014-05-17");
System.out.println(flag);
Date date = DateUtil.parseDate("2016/11/12");
System.out.println(date.toString());
String time = DateUtil.getTime(new Date());
System.out.println(time);
}
}