JAVA时间Date工具类

package com.common.util;

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

public class DateUtils {
	private StringBuffer buffer = new StringBuffer();
	private static String ZERO = "0";
	private static DateUtils date;
	public static SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
	public static SimpleDateFormat format1 = new SimpleDateFormat(
			"yyyyMMdd HH:mm:ss");
	public static SimpleDateFormat common_format = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	public String getNowString() {
		Calendar calendar = getCalendar();
		buffer.delete(0, buffer.capacity());
		buffer.append(getYear(calendar));

		if (getMonth(calendar) < 10) {
			buffer.append(ZERO);
		}
		buffer.append(getMonth(calendar));

		if (getDate(calendar) < 10) {
			buffer.append(ZERO);
		}
		buffer.append(getDate(calendar));
		if (getHour(calendar) < 10) {
			buffer.append(ZERO);
		}
		buffer.append(getHour(calendar));
		if (getMinute(calendar) < 10) {
			buffer.append(ZERO);
		}
		buffer.append(getMinute(calendar));
		if (getSecond(calendar) < 10) {
			buffer.append(ZERO);
		}
		buffer.append(getSecond(calendar));
		return buffer.toString();
	}

	private static int getDateField(Date date, int field) {
		Calendar c = getCalendar();
		c.setTime(date);
		return c.get(field);
	}

	public static int getYearsBetweenDate(Date begin, Date end) {
		int bYear = getDateField(begin, Calendar.YEAR);
		int eYear = getDateField(end, Calendar.YEAR);
		return eYear - bYear;
	}

	public static int getMonthsBetweenDate(Date begin, Date end) {
		int bMonth = getDateField(begin, Calendar.MONTH);
		int eMonth = getDateField(end, Calendar.MONTH);
		return eMonth - bMonth;
	}

	public static int getWeeksBetweenDate(Date begin, Date end) {
		int bWeek = getDateField(begin, Calendar.WEEK_OF_YEAR);
		int eWeek = getDateField(end, Calendar.WEEK_OF_YEAR);
		return eWeek - bWeek;
	}

	public static int getDaysBetweenDate(Date begin, Date end) {
		return (int) ((end.getTime()-begin.getTime())/(1000 * 60 * 60 * 24));
	}

	public static void main(String args[]) {
		System.out.println(getSpecficMonthStart(Calendar.getInstance().getTime(), 0));
	}

	/**
	 * 获取date年后的amount年的第一天的开始时间
	 *
	 * @param amount
	 *            可正、可负
	 * @return
	 */
	public static Date getSpecficYearStart(Date date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, amount);
		cal.set(Calendar.DAY_OF_YEAR, 1);
		return getStartDate(cal.getTime());
	}

	/**
	 * 获取date年后的amount年的最后一天的终止时间
	 *
	 * @param amount
	 *            可正、可负
	 * @return
	 */
	public static Date getSpecficYearEnd(Date date, int amount) {
		Date temp = getStartDate(getSpecficYearStart(date, amount + 1));
		Calendar cal = Calendar.getInstance();
		cal.setTime(temp);
		cal.add(Calendar.DAY_OF_YEAR, -1);
		return getFinallyDate(cal.getTime());
	}

	/**
	 * 获取date月后的amount月的第一天的开始时间
	 *
	 * @param amount
	 *            可正、可负
	 * @return
	 */
	public static Date getSpecficMonthStart(Date date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, amount);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		return getStartDate(cal.getTime());
	}

	/**
	 * 获取当前自然月后的amount月的最后一天的终止时间
	 *
	 * @param amount
	 *            可正、可负
	 * @return
	 */
	public static Date getSpecficMonthEnd(Date date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getSpecficMonthStart(date, amount + 1));
		cal.add(Calendar.DAY_OF_YEAR, -1);
		return getFinallyDate(cal.getTime());
	}

	/**
	 * 获取date周后的第amount周的开始时间(这里星期一为一周的开始)
	 *
	 * @param amount
	 *            可正、可负
	 * @return
	 */
	public static Date getSpecficWeekStart(Date date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.setFirstDayOfWeek(Calendar.MONDAY); /* 设置一周的第一天为星期一 */
		cal.add(Calendar.WEEK_OF_MONTH, amount);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return getStartDate(cal.getTime());
	}

	/**
	 * 获取date周后的第amount周的最后时间(这里星期日为一周的最后一天)
	 *
	 * @param amount
	 *            可正、可负
	 * @return
	 */
	public static Date getSpecficWeekEnd(Date date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.MONDAY); /* 设置一周的第一天为星期一 */
		cal.add(Calendar.WEEK_OF_MONTH, amount);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return getFinallyDate(cal.getTime());
	}

	public static Date getSpecficDateStart(Date date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, amount);
		return getStartDate(cal.getTime());
	}

	public static Date getSpecficDateEnd(Date date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, amount);
		return getFinallyDate(cal.getTime());
	}

	/**
	 * 得到指定日期的一天的的最后时刻23:59:59
	 *
	 * @param date
	 * @return
	 */
	public static Date getFinallyDate(Date date) {
		String temp = format.format(date);
		temp += " 23:59:59";

		try {
			return format1.parse(temp);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 得到指定日期的一天的的最后时刻23:59:59
	 *
	 * @param date
	 * @return
	 */
	public static String getFinallyDateStr(Date date) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String temp = format.format(date);
		temp += " 23:59:59";

		try {
			return temp;
		} catch (Exception e) {
			return temp;
		}
	}

	/**
	 * 得到指定日期的一天的开始时刻00:00:00
	 *
	 * @param date
	 * @return
	 */
	public static Date getStartDate(Date date) {
		String temp = format.format(date);
		temp += " 00:00:00";

		try {
			return format1.parse(temp);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 得到指定日期的一天的开始时刻00:00:00
	 *
	 * @param date
	 * @return
	 */
	public static String getStartDateStr(Date date) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String temp = format.format(date);
		temp += " 00:00:00";

		try {
			return temp;
		} catch (Exception e) {
			return temp;
		}
	}

	/**
	 *
	 * @param date
	 *            指定比较日期
	 * @param compareDate
	 * @return
	 */
	public static boolean isInDate(Date date, Date compareDate) {
		if (compareDate.after(getStartDate(date))
				&& compareDate.before(getFinallyDate(date))) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 获取两个时间的差值秒
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static Integer getSecondBetweenDate(Date d1,Date d2){
		Long second=(d2.getTime()-d1.getTime())/1000;
		return second.intValue();
	}

	private int getYear(Calendar calendar) {
		return calendar.get(Calendar.YEAR);
	}

	private int getMonth(Calendar calendar) {
		return calendar.get(Calendar.MONDAY) + 1;
	}

	private int getDate(Calendar calendar) {
		return calendar.get(Calendar.DATE);
	}

	private int getHour(Calendar calendar) {
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	private int getMinute(Calendar calendar) {
		return calendar.get(Calendar.MINUTE);
	}

	private int getSecond(Calendar calendar) {
		return calendar.get(Calendar.SECOND);
	}

	private static Calendar getCalendar() {
		return Calendar.getInstance();
	}

	public static DateUtils getDateInstance() {
		if (date == null) {
			date = new DateUtils();
		}
		return date;
	}
}

  

package com.jetcms.common.util;

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

/** * @author hzp */public class DateUtils {   private StringBuffer buffer = new StringBuffer();   private static String ZERO = "0";   private static DateUtils date;   public static SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");   public static SimpleDateFormat format1 = new SimpleDateFormat(         "yyyyMMdd HH:mm:ss");   public static SimpleDateFormat common_format = new SimpleDateFormat(         "yyyy-MM-dd HH:mm:ss");

   public String getNowString() {      Calendar calendar = getCalendar();buffer.delete(0, buffer.capacity());buffer.append(getYear(calendar));

      if (getMonth(calendar) < 10) {         buffer.append(ZERO);}      buffer.append(getMonth(calendar));

      if (getDate(calendar) < 10) {         buffer.append(ZERO);}      buffer.append(getDate(calendar));      if (getHour(calendar) < 10) {         buffer.append(ZERO);}      buffer.append(getHour(calendar));      if (getMinute(calendar) < 10) {         buffer.append(ZERO);}      buffer.append(getMinute(calendar));      if (getSecond(calendar) < 10) {         buffer.append(ZERO);}      buffer.append(getSecond(calendar));      return buffer.toString();}

   private static int getDateField(Date date, int field) {      Calendar c = getCalendar();c.setTime(date);      return c.get(field);}

   public static int getYearsBetweenDate(Date begin, Date end) {      int bYear = getDateField(begin, Calendar.YEAR);      int eYear = getDateField(end, Calendar.YEAR);      return eYear - bYear;}

   public static int getMonthsBetweenDate(Date begin, Date end) {      int bMonth = getDateField(begin, Calendar.MONTH);      int eMonth = getDateField(end, Calendar.MONTH);      return eMonth - bMonth;}

   public static int getWeeksBetweenDate(Date begin, Date end) {      int bWeek = getDateField(begin, Calendar.WEEK_OF_YEAR);      int eWeek = getDateField(end, Calendar.WEEK_OF_YEAR);      return eWeek - bWeek;}

   public static int getDaysBetweenDate(Date begin, Date end) {      return (int) ((end.getTime()-begin.getTime())/(1000 * 60 * 60 * 24));}

   public static void main(String args[]) {      System.out.println(getSpecficMonthStart(Calendar.getInstance().getTime(), 0));}

   /**    * 获取date年后的amount年的第一天的开始时间*     * @param amount*            可正、可负* @return*/public static Date getSpecficYearStart(Date date, int amount) {      Calendar cal = Calendar.getInstance();cal.setTime(date);cal.add(Calendar.YEAR, amount);cal.set(Calendar.DAY_OF_YEAR, 1);      return getStartDate(cal.getTime());}

   /**    * 获取date年后的amount年的最后一天的终止时间*     * @param amount*            可正、可负* @return*/public static Date getSpecficYearEnd(Date date, int amount) {      Date temp = getStartDate(getSpecficYearStart(date, amount + 1));Calendar cal = Calendar.getInstance();cal.setTime(temp);cal.add(Calendar.DAY_OF_YEAR, -1);      return getFinallyDate(cal.getTime());}

   /**    * 获取date月后的amount月的第一天的开始时间*     * @param amount*            可正、可负* @return*/public static Date getSpecficMonthStart(Date date, int amount) {      Calendar cal = Calendar.getInstance();cal.setTime(date);cal.add(Calendar.MONTH, amount);cal.set(Calendar.DAY_OF_MONTH, 1);      return getStartDate(cal.getTime());}

   /**    * 获取当前自然月后的amount月的最后一天的终止时间*     * @param amount*            可正、可负* @return*/public static Date getSpecficMonthEnd(Date date, int amount) {      Calendar cal = Calendar.getInstance();cal.setTime(getSpecficMonthStart(date, amount + 1));cal.add(Calendar.DAY_OF_YEAR, -1);      return getFinallyDate(cal.getTime());}

   /**    * 获取date周后的第amount周的开始时间(这里星期一为一周的开始)*     * @param amount*            可正、可负* @return*/public static Date getSpecficWeekStart(Date date, int amount) {      Calendar cal = Calendar.getInstance();cal.setTime(date);cal.setFirstDayOfWeek(Calendar.MONDAY); /* 设置一周的第一天为星期一 */cal.add(Calendar.WEEK_OF_MONTH, amount);cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);      return getStartDate(cal.getTime());}

   /**    * 获取date周后的第amount周的最后时间(这里星期日为一周的最后一天)*     * @param amount*            可正、可负* @return*/public static Date getSpecficWeekEnd(Date date, int amount) {      Calendar cal = Calendar.getInstance();cal.setFirstDayOfWeek(Calendar.MONDAY); /* 设置一周的第一天为星期一 */cal.add(Calendar.WEEK_OF_MONTH, amount);cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);      return getFinallyDate(cal.getTime());}

   public static Date getSpecficDateStart(Date date, int amount) {      Calendar cal = Calendar.getInstance();cal.setTime(date);cal.add(Calendar.DAY_OF_YEAR, amount);      return getStartDate(cal.getTime());}

   public static Date getSpecficDateEnd(Date date, int amount) {      Calendar cal = Calendar.getInstance();cal.setTime(date);cal.add(Calendar.DAY_OF_YEAR, amount);      return getFinallyDate(cal.getTime());}

   /**    * 得到指定日期的一天的的最后时刻23:59:59    *     * @param date* @return*/public static Date getFinallyDate(Date date) {      String temp = format.format(date);temp += " 23:59:59";

      try {         return format1.parse(temp);} catch (ParseException e) {         return null;}   }

   /**    * 得到指定日期的一天的的最后时刻23:59:59    *    * @param date* @return*/public static String getFinallyDateStr(Date date) {      DateFormat format = new SimpleDateFormat("yyyy-MM-dd");String temp = format.format(date);temp += " 23:59:59";

      try {         return temp;} catch (Exception e) {         return temp;}   }

   /**    * 得到指定日期的一天的开始时刻00:00:00    *     * @param date* @return*/public static Date getStartDate(Date date) {      String temp = format.format(date);temp += " 00:00:00";

      try {         return format1.parse(temp);} catch (Exception e) {         return null;}   }

   /**    * 得到指定日期的一天的开始时刻00:00:00    *    * @param date* @return*/public static String getStartDateStr(Date date) {      DateFormat format = new SimpleDateFormat("yyyy-MM-dd");String temp = format.format(date);temp += " 00:00:00";

      try {         return temp;} catch (Exception e) {         return temp;}   }

   /**    *     * @param date*            指定比较日期* @param compareDate* @return*/public static boolean isInDate(Date date, Date compareDate) {      if (compareDate.after(getStartDate(date))            && compareDate.before(getFinallyDate(date))) {         return true;} else {         return false;}

   }

   /**    * 获取两个时间的差值秒* @param d1* @param d2* @return*/public static Integer getSecondBetweenDate(Date d1,Date d2){      Long second=(d2.getTime()-d1.getTime())/1000;      return second.intValue();}

   private int getYear(Calendar calendar) {      return calendar.get(Calendar.YEAR);}

   private int getMonth(Calendar calendar) {      return calendar.get(Calendar.MONDAY) + 1;}

   private int getDate(Calendar calendar) {      return calendar.get(Calendar.DATE);}

   private int getHour(Calendar calendar) {      return calendar.get(Calendar.HOUR_OF_DAY);}

   private int getMinute(Calendar calendar) {      return calendar.get(Calendar.MINUTE);}

   private int getSecond(Calendar calendar) {      return calendar.get(Calendar.SECOND);}

   private static Calendar getCalendar() {      return Calendar.getInstance();}

   public static DateUtils getDateInstance() {      if (date == null) {         date = new DateUtils();}      return date;}}

原文地址:https://www.cnblogs.com/pxblog/p/10593440.html

时间: 2024-10-11 01:52:56

JAVA时间Date工具类的相关文章

java时间比较工具类分享

开发中经常需要比较时间,写了一个简易的工具类,分享一下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

java时间处理工具类--DateUtils

package com.hexiang.utils; /** * @(#)DateUtil.java * * * @author kidd * @version 1.00 2007/8/8 */ import java.util.*; import java.text.*; import java.sql.Timestamp; public class DateUtils { /** * 时间范围:年 */ public static final int YEAR = 1; /** * 时间范围

java 时间处理工具类

+++++++++ ++++++++++ package DataUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DataUtil { /** * 预设不同的时间格式 */ //精确到年月日(英文) eg:2019-12-31 public static String F

[java工具类01]__构建格式化输出日期和时间的工具类

在之前的学习中,我写过一篇关于字符串格式化的,就主要设计到了时间以及日期的各种格式化显示的设置,其主要时通过String类的fomat()方法实现的. 我们可以通过使用不同的转换符来实现格式化显示不同的时间以及日期信息,但我们了解到,时间以及日期的转换符实在是太多了,导致我们无法十分方便的在需要的时候格式化出想要的日期时间输出格式. 然而在学习过程中,我们了解到类是可以相互调用的,以及静态方法是可以跨类使用的,,所以,通过本文,将构建一个显示时间日期的工具类,定义几个常用的日期时间格式,之后我们

Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ?Copyright 蕃薯耀 2017年9月13日 http://www.cnblogs.com/fanshuyao/ 直接上代码: import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.ref

java格式处理工具类

import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.math.BigInteger; import java.text.Par

28个Java常用的工具类

源码下载:http://pan.baidu.com/s/1pJLSczD Base64.javaBase64DecodingException.javaCConst.javaCharTools.javaConfigHelper.javaCounter.javaCTool.javaDateHandler.javaDateUtil.javaDealString.javaDebugOut.javaDom4jHelper.javaEscape.javaExecHelper.javaFileHelper.

joda-time的使用,创建时间处理工具类DateTimeUtil

@大神爱吃茶 joda-time的使用,创建时间处理工具类DateTimeUtil Joda-Time是专门处理时间的库.可以将我们传进去的date对象标准化为我们想要指定保存的时间格式形式: 1 import org.apache.commons.lang3.StringUtils; 2 import org.joda.time.DateTime; 3 import org.joda.time.format.DateTimeFormat; 4 import org.joda.time.form

[精品] 收集的27个java开发常用工具类.基本满足开发需求

原文:[精品] 收集的27个java开发常用工具类.基本满足开发需求 源代码下载地址:http://www.zuidaima.com/share/1596028005993472.htm 最近从网上收集的java开发常用的工具类,分享给大家.基本满足开发需求.推荐给热爱最代码以及java的牛牛们.   每个类都有注释的,欢迎大家可以下载使用. 字符编码:CharTools, base64:Base64 *.java Md5加密:  MD5*.java 上传:*Uploader* 生成缩略图类:T