Date
- 时间类
Calendar
- 日期类
一个时间日期工具类
- 时间转字符串(有默认时间格式,带时间格式)
- 字符串转时间(有默认时间格式,带时间格式)
- 计算两个日期之间相差的天数
- 计算当前时间多少天以后的日期
- 判断是否是日期格式
package datedemo;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;
/**
* 日期工具
*/
public class DateUtil {
private final static String A="yyyy-MM-dd";//日期格式
private final static String B="yyyy-MM-dd HH:mm:ss";//日期格式
private final static String C="yyyy/MM/dd HH:mm:ss";//日期格式
private final static String exp="((^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(10|12|0?[13578])([-\\/\\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(11|0?[469])([-\\/\\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(0?2)([-\\/\\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([3579][26]00)([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][0][48])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][0][48])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][2468][048])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][2468][048])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][13579][26])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][13579][26])([-\\/\\._])(0?2)([-\\/\\._])(29)$))";
/**
* 时间类型转字符串 固定返回 日期为 yyyy-MM-dd
* @param date
* @return String
*/
public static String getDateToString(Date date) throws Exception{
String s;
SimpleDateFormat sft=new SimpleDateFormat(A);//格式时间对象
s=sft.format(date);
return s;
}
/**
* 时间类型转字符串 不固定日期格式
* @param date
* @param format
* @return String
*/
public static String getDteToString (Date date,String format) throws Exception{
String s;
SimpleDateFormat sft=new SimpleDateFormat(format);//格式时间对象
s=sft.format(date);
return s;
}
/**
* 字符串时间转时间类型 固定日期格式 yyyy-MM-dd
* @param text 字符串时间
* @return Date
* @throws Exception
*/
public static Date getStringToDate(String text) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat(A);//格式时间对象
Date date = sdf.parse(text);
return date;
}
/**
* 字符串时间转时间类型 不固定时间格式
* @param text 时间字符串
* @param format 日期格式
* @return Date
* @throws Exception
*/
public static Date gettringToDate(String text,String format) throws Exception{
SimpleDateFormat sdf=new SimpleDateFormat(format);//格式时间对象
Date date=sdf.parse(text);
return date;
}
/**
*计算两个日期之间相差的天数
* @param a 第一个日期时间
* @param b 第二个日期时间
* @return
* @throws Exception
*/
public static long getDaysBetweenTwoDates(Date a, Date b) throws Exception {
//判断这两个时间的大小
if(a.equals(b)) return 0;
if(!a.before(b)){//保证返回的值为正数
Date temp;
temp=a;
a=b;
b=temp;
}
Calendar c = Calendar.getInstance();//获取calendar对像
c.setTime(a);//设置时间 date 转 calendar 类型
long t1 = c.getTimeInMillis();//获取时间戳
c.setTime(b);
long t2 = c.getTimeInMillis();
//计算天数
long days = (t2 - t1) / (24 * 60 * 60 * 1000);
return days;
}
/**
* 计算当前时间多少天以后的日期
* @param currentDate 当前时间
* @param distance 距离多少天
* @return
*/
public static Date getNextDasByNumber(Date currentDate,int distance)throws Exception{
Calendar calendar=Calendar.getInstance();//获取日历对象
calendar.setTime(currentDate);//设置当前时间 date 转 calendar 类型
calendar.add(Calendar.DATE,distance);//计算离当前时间以后的日期
Date date=calendar.getTime();//calendar 转 date 类型
return date;
}
/**
* 判断是否是日期格式
* @param date 字符串
* @return
*/
public static boolean isDate(String date){
// 创建 Pattern 对象 java正则表达式对象
Pattern r = Pattern.compile(exp);
boolean flag = r.matcher(date).matches();//判断它格式是否正确
return flag;
}
public static void main(String []arg){
Calendar calendar=Calendar.getInstance();
try {
//验证计算两个日期之间相差的天数
long i= getDaysBetweenTwoDates(getStringToDate("2017-02-11"),getStringToDate("2017-03-11"));
System.out.println("计算(2017-02-11,2017-03-11)两个日期之间相差的天数:"+i);
Date d= getNextDasByNumber(getStringToDate("2017-02-11"),28);
System.out.println("计算(2017-02-11)时间128天以后的日期:"+getDateToString(d));
boolean f= isDate("2017-02-11");
System.out.println("判断2017-02-11日期格式是否正确:"+f);
boolean f1= isDate("20170211");//正则表达式不支持这种
System.out.println("判断20170211日期格式是否正确:"+f1);
boolean f2= isDate("2017/02/11");
System.out.println("判断2017/02/11日期格式是否正确:"+f2);
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}
}
}
作者:P丶少
来源:CSDN
原文:https://blog.csdn.net/qq_28430851/article/details/78144488
网友评论