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(, buffer.capacity());
buffer.append(getYear(calendar)); if (getMonth(calendar) < ) {
buffer.append(ZERO);
}
buffer.append(getMonth(calendar)); if (getDate(calendar) < ) {
buffer.append(ZERO);
}
buffer.append(getDate(calendar));
if (getHour(calendar) < ) {
buffer.append(ZERO);
}
buffer.append(getHour(calendar));
if (getMinute(calendar) < ) {
buffer.append(ZERO);
}
buffer.append(getMinute(calendar));
if (getSecond(calendar) < ) {
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 * ));
} public static void main(String args[]) {
System.out.println(getSpecficMonthStart(Calendar.getInstance().getTime(), ));
} /**
* 获取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, );
return getStartDate(cal.getTime());
} /**
* 获取date年后的amount年的最后一天的终止时间
*
* @param amount
* 可正、可负
* @return
*/
public static Date getSpecficYearEnd(Date date, int amount) {
Date temp = getStartDate(getSpecficYearStart(date, amount + ));
Calendar cal = Calendar.getInstance();
cal.setTime(temp);
cal.add(Calendar.DAY_OF_YEAR, -);
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, );
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 + ));
cal.add(Calendar.DAY_OF_YEAR, -);
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())/;
return second.intValue();
} private int getYear(Calendar calendar) {
return calendar.get(Calendar.YEAR);
} private int getMonth(Calendar calendar) {
return calendar.get(Calendar.MONDAY) + ;
} 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;
}
}

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

  1. java时间处理工具类--DateUtils

    package com.hexiang.utils; /** * @(#)DateUtil.java * * * @author kidd * @version 1.00 2007/8/8 */ im ...

  2. Java中Date类型如何向前向后滚动时间,( 附工具类)

    Java中的Date类型向前向后滚动时间(附工具类) 废话不多说,先看工具类: import java.text.SimpleDateFormat; import java.util.Calendar ...

  3. 使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间。

    1.使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间. package com.fline.aic.utils; import java.text.DateFormat ...

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

    在之前的学习中,我写过一篇关于字符串格式化的,就主要设计到了时间以及日期的各种格式化显示的设置,其主要时通过String类的fomat()方法实现的. 我们可以通过使用不同的转换符来实现格式化显示不同 ...

  5. Java日期时间实用工具类

    Java日期时间实用工具类 1.Date (java.util.Date)    Date();        以当前时间构造一个Date对象    Date(long);        构造函数   ...

  6. HttpTool.java(在java tool util工具类中已存在) 暂保留

    HttpTool.java 该类为java源生态的http 请求工具,不依赖第三方jar包 ,即插即用. package kingtool; import java.io.BufferedReader ...

  7. java格式处理工具类

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

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

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  9. JAVA 8 日期工具类

    JAVA 8 日期工具类 主题描述 JAVA中日期时间的历史 代码成果 主题描述 JAVA的日期时间一直比较混乱,本来以为joda会是巅峰,但是JAVA 8改变了我的思想.但是即便在JAVA 8面前, ...

随机推荐

  1. Python学习笔记六

    Python课堂笔记六 常用模块已经可以在单位实际项目中使用,可以实现运维自动化.无需手工备份文件,数据库,拷贝,压缩. 常用模块 time模块 time.time time.localtime ti ...

  2. hdu 5183

    hdu 5183(Hash处理区间问题) 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5183 题意:给出一个n个元素的数组,现在要求判断 a1-a2 ...

  3. CoopyIII开发文档之控制LED灯开关

    作者:那年:QQ:843681152 一. CooplyIII环境的搭建 工欲善其事必先利器,如何搭建CooplyIII的开发环境是一切coolpyIII开发的前提.CoolpyIII作者内cool超 ...

  4. idea报错:Invalid bound statement (not found)

    在配置MyBatis接口映射的Mapper.xml时,提示Invalid bound statement (not found)异常,就算是接口和xml名字相同,路径相同也无法找到,在网上找到了几种解 ...

  5. Innodb与Myisam引擎的区别与应用场景

    1. 区别: (1)事务处理: MyISAM是非事务安全型的,而InnoDB是事务安全型的(支持事务处理等高级处理): (2)锁机制不同: MyISAM是表级锁,而InnoDB是行级锁: (3)sel ...

  6. React(v16.8.4)生命周期详解

    当前版本v16.8.4 装载过程(组件第一次在DOM树中渲染的过程): constructor(常用) -> getInitialState(v16.0已废弃) -> getDefault ...

  7. loadrunner之java user脚本开发

    脚本开发环境: loadrunner11.0 jdk1.6.32_x86_32 脚本开发 1.选择JavaVuser协议 2.配置java环境(Vuser--RunTime Settings) 3.开 ...

  8. mysql 使用教程 入门

    转载 http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html MySQL有三大类数据类型, 分别为数字.日期\时间.字符串, 这三大 ...

  9. GG的文化课

    attack大神退役后,我连文化课都被吊打了 attack:我要回来虐你们了 attack:怎么感觉能裸分清北呢 attack:我稳了 attack:你们个菜鸡,连bed和bad怎么读都不知道

  10. 洛谷.5284.[十二省联考2019]字符串问题(后缀自动机 拓扑 DP)

    LOJ BZOJ 洛谷 对这题无话可说,确实比较...裸... 像dls说的拿拓扑和parent树一套就能出出来了... 另外表示BZOJ Rank1 tql... 暴力的话,由每个\(A_i\)向它 ...