java中关于时间的一些常规操作

package test2;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/********************
 * 时间工具类
 * 1.将String类型的时间转化为Date类型
 * 2.将Date类型的时间转化为String类型
 * 3.获取当前系统时间格式化字符串
 * 4.获取当前时间戳
 * 5.判断传入的Date类型时间是否在当前系统时间之前
 * 6.判断传入的Date类型时间是否在当前系统时间之后
 * 7.判断传入的Date类型时间是否和当前系统时间相等
 * 8. 获取两个时间间隔的天数
 * 9.得到一个时间延后多少天之后的时间
 * 10.判断是否润年
 * 11.获取某个月的最后一天
 * 12.判断两个日期是否在同一周
 * 13.根据一个日期,返回是星期几的字符串
 * @author Shenfd
 *
 */
public class DateUtil {
	
	private DateUtil(){
		
	}
	
	static final String format1 = "yyyy-MM-dd HH:mm:ss";
	static final String format2 = "yyyy-MM-dd HH:mm:ss:SS";
	static final String format3 = "yyyy/MM/dd HH:mm:ss";
	static final String format4 = "yyyy/MM/dd HH:mm:ss:SS";
	static final String format5 = "yyyy年MM月dd日 HH时mm分ss秒";
	static final String format6 = "yyyy年MM月dd日 HH时mm分ss秒SS毫秒";
	static final String format7 = "yyyy-MM-dd";
	static final String format8 = "yyyy年MM月dd日";
	static final String format9 = "yyyyMMddHHmmssSS";
	
	/*******************************
	 * 将String时间转化为Date类型
	 * @param stringTime 
	 * 参数为String类型时间,格式[yyyy-MM-dd HH:mm:ss]
	 * @return Date类型时间
	 * @throws ParseException 
	 */
	public static Date stringParseToDate1(String stringTime) throws ParseException{
		DateFormat df = new SimpleDateFormat(format1);
		Date date = null;
		date = df.parse(stringTime);
		return date;
	}
	
	/*******************************
	 * 将String时间转化为Date类型
	 * @param stringTime 
	 * 参数为String类型时间,格式[yyyy-MM-dd HH:mm:ss:SS],参数精确到毫秒
	 * @return Date类型时间
	 * @throws ParseException 
	 */
	public static Date stringParseToDate2(String stringTime) throws ParseException{
		DateFormat df = new SimpleDateFormat(format2);
		Date date = null;
		date = df.parse(stringTime);
		return date;
	}
	
	/*******************************
	 * 将String时间转化为Date类型
	 * @param stringTime 
	 * 参数为String类型时间,日期有斜/,格式[yyyy/MM/dd HH:mm:ss]
	 * @return Date类型时间
	 * @throws ParseException 
	 */
	public static Date stringParseToDate3(String stringTime) throws ParseException{
		DateFormat df = new SimpleDateFormat(format3);
		Date date = null;
		date = df.parse(stringTime);
		return date;
	}
	
	/*******************************
	 * 将String时间转化为Date类型
	 * @param stringTime 
	 * 参数为String类型时间,日期有斜/,格式[yyyy/MM/dd HH:mm:ss:SS],参数要精确到毫秒
	 * @return Date类型时间
	 * @throws ParseException 
	 */
	public static Date stringParseToDate4(String stringTime) throws ParseException{
		DateFormat df = new SimpleDateFormat(format4);
		Date date = null;
		date = df.parse(stringTime);
		return date;
	}
 
	/*******************************
	 * 将String时间转化为Date类型
	 * @param stringTime 
	 * 参数为String类型时间,日期有斜/,格式[yyyy年MM月dd日 HH24时mm分ss秒]
	 * @return Date类型时间
	 * @throws ParseException 
	 */
	public static Date stringParseToDate5(String stringTime) throws ParseException{
		DateFormat df = new SimpleDateFormat(format5);
		Date date = null;
		date = df.parse(stringTime);
		return date;
	}
	
	/*******************************
	 * 将String时间转化为Date类型
	 * @param stringTime 
	 * 参数为String类型时间,日期有斜/,格式[yyyy年MM月dd日 HH24时mm分ss秒SS毫秒]
	 * @return Date类型时间
	 * @throws ParseException 
	 */
	public static Date stringParseToDate6(String stringTime) throws ParseException{
		DateFormat df = new SimpleDateFormat(format6);
		Date date = null;
		date = df.parse(stringTime);
		return date;
	}
	
	/*******************************
	 * 将String时间转化为Date类型
	 * @param stringTime 
	 * 参数为String类型时间,格式[yyyy-MM-dd]
	 * @return Date类型时间
	 * @throws ParseException 
	 */
	public static Date stringParseToDate7(String stringTime) throws ParseException{
		DateFormat df = new SimpleDateFormat(format7);
		Date date = null;
		date = df.parse(stringTime);
		return date;
	}
	
	/*******************************
	 * 将String时间转化为Date类型
	 * @param stringTime 
	 * 参数为String类型时间,格式[yyyy年MM月dd日]
	 * @return Date类型时间
	 * @throws ParseException 
	 */
	public static Date stringParseToDate8(String stringTime) throws ParseException{
		DateFormat df = new SimpleDateFormat(format7);
		Date date = null;
		date = df.parse(stringTime);
		return date;
	}
	
	/*******************
	 * 获取当前系统时间
	 * @return String类型时间,格式[yyyy-MM-dd HH:mm:ss]
	 */
	public static String getCurrentDateString1(){
		return new SimpleDateFormat(format1).format(new Date());
	}
	
	/*******************
	 * 获取当前系统时间
	 * @return String类型时间,精确到毫秒,格式[yyyy-MM-dd HH:mm:ss:SS]
	 */
	public static String getCurrentDateString2(){
		return new SimpleDateFormat(format2).format(new Date());
	}
	
	/*******************
	 * 获取当前系统时间,日期加斜/
	 * @return String类型时间,格式[yyyy/MM/dd HH:mm:ss]
	 */
	public static String getCurrentDateString3(){
		return new SimpleDateFormat(format3).format(new Date());
	}
	
	/*******************
	 * 获取当前系统时间,精确到毫秒,日期加斜/
	 * @return String类型时间,格式[yyyy/MM/dd HH:mm:ss:SS]
	 */
	public static String getCurrentDateString4(){
		return new SimpleDateFormat(format4).format(new Date());
	}
	
	/*******************
	 * 获取当前系统时间
	 * @return String类型时间,格式[yyyy年MM月dd日 HH时mm分ss秒]
	 */
	public static String getCurrentDateString5(){
		return new SimpleDateFormat(format5).format(new Date());
	}
	
	/*******************
	 * 获取当前系统时间
	 * @return String类型时间,精确到毫秒,格式[yyyy年MM月dd日 HH时mm分ss秒SS毫秒]
	 */
	public static String getCurrentDateString6(){
		return new SimpleDateFormat(format6).format(new Date());
	}
	
	/*******************
	 * 获取当前系统时间
	 * @return String类型时间,精确到毫秒,格式[yyyy-MM-dd]
	 */
	public static String getCurrentDateString7(){
		return new SimpleDateFormat(format7).format(new Date());
	}
	
	/*******************
	 * 获取当前系统时间
	 * @return String类型时间,精确到毫秒,格式[yyyy年MM月dd日]
	 */
	public static String getCurrentDateString8(){
		return new SimpleDateFormat(format8).format(new Date());
	}
	
	/****************************
	 * 将Date类型日期格式化为String类型
	 * @param date 参数为Date类型
	 * @return 返回String类型时间,格式[yyyy-MM-dd HH:mm:ss]
	 */
	public static String dateParseToString1(Date date){
		DateFormat df = new SimpleDateFormat(format1);
		return df.format(date);
	}
	
	/****************************
	 * 将Date类型日期格式化为String类型
	 * @param date 参数为Date类型
	 * @return 返回String类型时间,格式[yyyy-MM-dd HH:mm:ss:SS]
	 */
	public static String dateParseToString2(Date date){
		DateFormat df = new SimpleDateFormat(format2);
		return df.format(date);
	}
	
	/****************************
	 * 将Date类型日期格式化为String类型
	 * @param date 参数为Date类型
	 * @return 返回String类型时间,格式[yyyy/MM/dd HH:mm:ss]
	 */
	public static String dateParseToString3(Date date){
		DateFormat df = new SimpleDateFormat(format3);
		return df.format(date);
	}
	
	/****************************
	 * 将Date类型日期格式化为String类型
	 * @param date 参数为Date类型
	 * @return 返回String类型时间,格式[yyyy/MM/dd HH:mm:ss:SS]
	 */
	public static String dateParseToString4(Date date){
		DateFormat df = new SimpleDateFormat(format4);
		return df.format(date);
	}
	
	/****************************
	 * 将Date类型日期格式化为String类型
	 * @param date 参数为Date类型
	 * @return 返回String类型时间,格式[yyyy年MM月dd日 HH时mm分ss秒]
	 */
	public static String dateParseToString5(Date date){
		DateFormat df = new SimpleDateFormat(format5);
		return df.format(date);
	}
	
	/****************************
	 * 将Date类型日期格式化为String类型
	 * @param date 参数为Date类型
	 * @return 返回String类型时间,格式[yyyy年MM月dd日 HH时mm分ss秒SS毫秒]
	 */
	public static String dateParseToString6(Date date){
		DateFormat df = new SimpleDateFormat(format6);
		return df.format(date);
	}
	
	/****************************
	 * 将Date类型日期格式化为String类型
	 * @param date 参数为Date类型
	 * @return 返回String类型时间,格式[yyyy-MM-dd]
	 */
	public static String dateParseToString7(Date date){
		DateFormat df = new SimpleDateFormat(format7);
		return df.format(date);
	}
	
	/****************************
	 * 将Date类型日期格式化为String类型
	 * @param date 参数为Date类型
	 * @return 返回String类型时间,格式[yyyy年MM月dd日]
	 */
	public static String dateParseToString8(Date date){
		DateFormat df = new SimpleDateFormat(format8);
		return df.format(date);
	}
	
	/***********************
	 * 获取当前时间戳 格式yyyyMMddHHmmssSS
	 * @return String类型时间戳
	 */
	public static String getTimeStampString(){
		return new SimpleDateFormat(format9).format(new Date());
	}
	
	/*************************
	 * 判断传入的Date类型时间是否在当前系统时间之前
	 * @param date 和当前系统时间比较的时间
	 * @return bollean
	 */
	public static boolean isBoforeCurrentDate(Date date){
		return (date.getTime()<(new Date().getTime())) ? true:false;
	}
	
	/*************************
	 * 判断传入的Date类型时间是否在当前系统时间之后
	 * @param date 和当前系统时间比较的时间
	 * @return boolean
	 */
	public static boolean isAfterCurrentDate(Date date){
		return (date.getTime()>(new Date().getTime())) ? true:false;
	}
	
	/*************************
	 * 判断传入的Date类型时间是否和当前系统时间相等
	 * @param date 和当前系统时间比较的时间
	 * @return boolean
	 */
	public static boolean isEqualsCurrentDate(Date date){
		return (date.getTime()==(new Date().getTime())) ? true:false;
	}
	
	/*************************
	 * 获取两个时间间隔的天数,如果返回值大于0,则表示startDate>endDate
	 * @param startDate Date类型
	 * @param endDate Date类型
	 * @return 间隔天数
	 */
	public static int getSeparateDay(Date startDate,Date endDate){
		return (int) ((startDate.getTime()-endDate.getTime())/(24 * 60 * 60 * 1000));
	}
	
	/*************************
	 * 获取两个时间间隔的天数,如果返回值大于0,则表示startDate>endDate
	 * @param startDate String类型
	 * @param endDate String类型
	 * @return 间隔天数
	 * @throws ParseException 
	 */
	public static int getSeparateDay(String startDate, String endDate) throws ParseException{
		DateFormat df = new SimpleDateFormat(format7);
		return (int) ((df.parse(startDate).getTime()-df.parse(endDate).getTime())/(24 * 60 * 60 * 1000));
	}
	
	/****************************
	 * 得到一个时间延后多少天之后的时间
	 * @param nowDay String类型的时间
	 * @param delay 前移或后延的天数
	 * @return 返回String类型时间
	 * @throws ParseException
	 */
	public static String getMoveDay(String nowDay, int delay) throws ParseException{
		DateFormat df = new SimpleDateFormat(format7);
		Date d = stringParseToDate7(nowDay);
		long myTime = (d.getTime() / 1000) + delay * 24 * 60 * 60;
		d.setTime(myTime * 1000);
		return df.format(d);
	}
	
	/****************************
	 * 得到当前时间延后多少天之后的时间
	 * @param nowDay String类型的时间
	 * @param delay 前移或后延的天数
	 * @return 返回Date类型时间
	 * @throws ParseException
	 */
	public static Date getMoveDay(int delay) throws ParseException{
		DateFormat df = new SimpleDateFormat(format7);
		Date d = new Date();
		long myTime = (d.getTime() / 1000) + delay * 24 * 60 * 60;
		d.setTime(myTime * 1000);
		return df.parse(df.format(d));
	}
	
	
	/*********************
	 * 判断是否润年
	 * @param date Date类型时间
	 * @return boolean
	 */
	public static boolean isLeapYear(Date date) {
		GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
		gc.setTime(date);
		int year = gc.get(Calendar.YEAR);
		if ((year % 400) == 0)
			return true;
		else if ((year % 4) == 0) {
			if ((year % 100) == 0)
				return false;
			else
				return true;
		} else
			return false;
	}
	
	/*********************
	 * 判断是否润年
	 * @param date String类型时间
	 * @return boolean
	 * @throws ParseException 
	 */
	public static boolean isLeapYear(String date) throws ParseException {
		DateFormat df = new SimpleDateFormat(format7);
		Date d = df.parse(date);
		GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
		gc.setTime(d);
		int year = gc.get(Calendar.YEAR);
		if ((year % 400) == 0)
			return true;
		else if ((year % 4) == 0) {
			if ((year % 100) == 0)
				return false;
			else
				return true;
		} else
			return false;
	}
	
	/***********************
	 * 获取某个月的最后一天
	 * @param date String类型的时间
	 * @return String日期,返回格式:[yyyy-MM-dd]
	 * @throws ParseException
	 */
	public static String getEndDateOfMonth(String date) throws ParseException {// yyyy-MM-dd
		String str = date.substring(0, 8);
		String month = date.substring(5, 7);
		int mon = Integer.parseInt(month);
		if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
			str += "31";
		} else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
			str += "30";
		} else {
			if (isLeapYear(date)) {
				str += "29";
			} else {
				str += "28";
			}
		}
		return str;
	}
	
	/*******************
	 * 判断两个日期是否在同一周
	 * @param date1 Date类型的时间
	 * @param date2 Date类型的时间
	 * @return boolean
	 */
	public static boolean isSameWeekDates(Date date1, Date date2) {
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setTime(date1);
		cal2.setTime(date2);
		int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
		if (0 == subYear) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
			// 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		}
		return false;
	}
	
	
	/**************
	 * 根据一个日期,返回是星期几的字符串
	 * @param date Date类型的日期
	 * @return
	 */
	public static String getWeekStr(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		String str=new SimpleDateFormat("EEEE").format(c.getTime());
		if("1".equals(str)){
			str = "星期日";
		}else if("2".equals(str)){
			str = "星期一";
		}else if("3".equals(str)){
			str = "星期二";
		}else if("4".equals(str)){
			str = "星期三";
		}else if("5".equals(str)){
			str = "星期四";
		}else if("6".equals(str)){
			str = "星期五";
		}else if("7".equals(str)){
			str = "星期六";
		}
		return str;
	}
}


最后,欢迎访问风格清新简洁的轻博客网站[指尖一刻]



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值