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; /**
* 时间范围:季度
*/
public static final int QUARTER = 2; /**
* 时间范围:月
*/
public static final int MONTH = 3; /**
* 时间范围:旬
*/
public static final int TENDAYS = 4; /**
* 时间范围:周
*/
public static final int WEEK = 5; /**
* 时间范围:日
*/
public static final int DAY = 6; /* 基准时间 */
private Date fiducialDate = null; private Calendar cal = null; private DateUtils(Date fiducialDate) {
if (fiducialDate != null) {
this.fiducialDate = fiducialDate;
} else {
this.fiducialDate = new Date(System.currentTimeMillis());
} this.cal = Calendar.getInstance();
this.cal.setTime(this.fiducialDate);
this.cal.set(Calendar.HOUR_OF_DAY, 0);
this.cal.set(Calendar.MINUTE, 0);
this.cal.set(Calendar.SECOND, 0);
this.cal.set(Calendar.MILLISECOND, 0); this.fiducialDate = this.cal.getTime();
} /**
* 获取DateHelper实例
*
* @param fiducialDate
* 基准时间
* @return Date
*/
public static DateUtils getInstance(Date fiducialDate) {
return new DateUtils(fiducialDate);
} /**
* 获取DateHelper实例, 使用当前时间作为基准时间
*
* @return Date
*/
public static DateUtils getInstance() {
return new DateUtils(null);
} /**
* 获取年的第一天
*
* @param offset
* 偏移量
* @return Date
*/
public Date getFirstDayOfYear(int offset) {
cal.setTime(this.fiducialDate);
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + offset);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
} /**
* 获取年的最后一天
*
* @param offset
* 偏移量
* @return Date
*/
public Date getLastDayOfYear(int offset) {
cal.setTime(this.fiducialDate);
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + offset);
cal.set(Calendar.MONTH, Calendar.DECEMBER);
cal.set(Calendar.DAY_OF_MONTH, 31);
return cal.getTime();
} /**
* 获取季度的第一天
*
* @param offset
* 偏移量
* @return Date
*/
public Date getFirstDayOfQuarter(int offset) {
cal.setTime(this.fiducialDate);
cal.add(Calendar.MONTH, offset * 3);
int mon = cal.get(Calendar.MONTH);
if (mon >= Calendar.JANUARY && mon <= Calendar.MARCH) {
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 1);
}
if (mon >= Calendar.APRIL && mon <= Calendar.JUNE) {
cal.set(Calendar.MONTH, Calendar.APRIL);
cal.set(Calendar.DAY_OF_MONTH, 1);
}
if (mon >= Calendar.JULY && mon <= Calendar.SEPTEMBER) {
cal.set(Calendar.MONTH, Calendar.JULY);
cal.set(Calendar.DAY_OF_MONTH, 1);
}
if (mon >= Calendar.OCTOBER && mon <= Calendar.DECEMBER) {
cal.set(Calendar.MONTH, Calendar.OCTOBER);
cal.set(Calendar.DAY_OF_MONTH, 1);
}
return cal.getTime();
} public Date getYesterday() {
long time = this.fiducialDate.getTime() - 60 * 60 * 24 * 1000;
return new Date(time);
} public Date getTomorrow(){
long time = this.fiducialDate.getTime() + 60 * 60 * 24 * 1000;
return new Date(time);
} /**
* 获取季度的最后一天
*
* @param offset
* 偏移量
* @return Date
*/
public Date getLastDayOfQuarter(int offset) {
cal.setTime(this.fiducialDate);
cal.add(Calendar.MONTH, offset * 3); int mon = cal.get(Calendar.MONTH);
if (mon >= Calendar.JANUARY && mon <= Calendar.MARCH) {
cal.set(Calendar.MONTH, Calendar.MARCH);
cal.set(Calendar.DAY_OF_MONTH, 31);
}
if (mon >= Calendar.APRIL && mon <= Calendar.JUNE) {
cal.set(Calendar.MONTH, Calendar.JUNE);
cal.set(Calendar.DAY_OF_MONTH, 30);
}
if (mon >= Calendar.JULY && mon <= Calendar.SEPTEMBER) {
cal.set(Calendar.MONTH, Calendar.SEPTEMBER);
cal.set(Calendar.DAY_OF_MONTH, 30);
}
if (mon >= Calendar.OCTOBER && mon <= Calendar.DECEMBER) {
cal.set(Calendar.MONTH, Calendar.DECEMBER);
cal.set(Calendar.DAY_OF_MONTH, 31);
}
return cal.getTime();
} /**
* 获取月的最后一天
*
* @param offset
* 偏移量
* @return Date
*/
public Date getFirstDayOfMonth(int offset) {
cal.setTime(this.fiducialDate);
cal.add(Calendar.MONTH, offset);
cal.set(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
} /**
* 获取月的最后一天
*
* @param offset
* 偏移量
* @return Date
*/
public Date getLastDayOfMonth(int offset) {
cal.setTime(this.fiducialDate);
cal.add(Calendar.MONTH, offset + 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
} /**
* 获取旬的第一天
*
* @param offset
* 偏移量
* @return Date
*/
public Date getFirstDayOfTendays(int offset) {
cal.setTime(this.fiducialDate);
int day = cal.get(Calendar.DAY_OF_MONTH);
if (day >= 21) {
day = 21;
} else if (day >= 11) {
day = 11;
} else {
day = 1;
} if (offset > 0) {
day = day + 10 * offset;
int monOffset = day / 30;
day = day % 30;
cal.add(Calendar.MONTH, monOffset);
cal.set(Calendar.DAY_OF_MONTH, day);
} else {
int monOffset = 10 * offset / 30;
int dayOffset = 10 * offset % 30;
if ((day + dayOffset) > 0) {
day = day + dayOffset;
} else {
monOffset = monOffset - 1;
day = day - dayOffset - 10;
}
cal.add(Calendar.MONTH, monOffset);
cal.set(Calendar.DAY_OF_MONTH, day);
}
return cal.getTime();
} /**
* 获取旬的最后一天
*
* @param offset
* 偏移量
* @return Date
*/
public Date getLastDayOfTendays(int offset) {
Date date = getFirstDayOfTendays(offset + 1);
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
} /**
* 获取周的第一天(MONDAY)
*
* @param offset
* 偏移量
* @return Date
*/
public Date getFirstDayOfWeek(int offset) {
cal.setTime(this.fiducialDate);
cal.add(Calendar.DAY_OF_MONTH, offset * 7);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return cal.getTime();
} /**
* 获取周的最后一天(SUNDAY)
*
* @param offset
* 偏移量
* @return Date
*/
public Date getLastDayOfWeek(int offset) {
cal.setTime(this.fiducialDate);
cal.add(Calendar.DAY_OF_MONTH, offset * 7);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.add(Calendar.DAY_OF_MONTH, 6);
return cal.getTime();
} /**
* 获取指定时间范围的第一天
*
* @param dateRangeType
* 时间范围类型
* @param offset
* 偏移量
* @return Date
*/
public Date getFirstDate(int dateRangeType, int offset) {
return null;
} /**
* 获取指定时间范围的最后一天
*
* @param dateRangeType
* 时间范围类型
* @param offset
* 偏移量
* @return Date
*/
public Date getLastDate(int dateRangeType, int offset) {
return null;
} /**
* 根据日历的规则,为基准时间添加指定日历字段的时间量
*
* @param field
* 日历字段, 使用Calendar类定义的日历字段常量
* @param offset
* 偏移量
* @return Date
*/
public Date add(int field, int offset) {
cal.setTime(this.fiducialDate);
cal.add(field, offset);
return cal.getTime();
} /**
* 根据日历的规则,为基准时间添加指定日历字段的单个时间单元
*
* @param field
* 日历字段, 使用Calendar类定义的日历字段常量
* @param up
* 指定日历字段的值的滚动方向。true:向上滚动 / false:向下滚动
* @return Date
*/
public Date roll(int field, boolean up) {
cal.setTime(this.fiducialDate);
cal.roll(field, up);
return cal.getTime();
} /**
* 把字符串转换为日期
*
* @param dateStr
* 日期字符串
* @param format
* 日期格式
* @return Date
*/
public static Date strToDate(String dateStr, String format) {
Date date = null; if (dateStr != null && (!dateStr.equals(""))) {
DateFormat df = new SimpleDateFormat(format);
try {
date = df.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
}
return date;
} /**
* 把字符串转换为日期,日期的格式为yyyy-MM-dd HH:ss
*
* @param dateStr
* 日期字符串
* @return Date
*/
public static Date strToDate(String dateStr) {
Date date = null; if (dateStr != null && (!dateStr.equals(""))) {
if (dateStr.matches("\\d{4}-\\d{1,2}-\\d{1,2}")) {
dateStr = dateStr + " 00:00";
} else if (dateStr.matches("\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}")) {
dateStr = dateStr + ":00";
} else {
System.out.println(dateStr + " 格式不正确");
return null;
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:ss");
try {
date = df.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
}
return date;
} /**
* 把日期转换为字符串
*
* @param date
* 日期实例
* @param format
* 日期格式
* @return Date
*/
public static String dateToStr(Date date, String format) {
return (date == null) ? "" : new SimpleDateFormat(format).format(date);
} public static String dateToStr(Date date) {
return (date == null) ? ""
: new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
} /**
* 取得当前日期 年-月-日
*
* @return Date
*/
public static String getCurrentDate() {
DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
return f.format(Calendar.getInstance().getTime());
} public static void main(String[] args) {
DateUtils dateHelper = DateUtils.getInstance(); /* Year */
for (int i = -5; i <= 5; i++) {
System.out.println("FirstDayOfYear(" + i + ") = "
+ dateHelper.getFirstDayOfYear(i));
System.out.println("LastDayOfYear(" + i + ") = "
+ dateHelper.getLastDayOfYear(i));
} /* Quarter */
for (int i = -5; i <= 5; i++) {
System.out.println("FirstDayOfQuarter(" + i + ") = "
+ dateHelper.getFirstDayOfQuarter(i));
System.out.println("LastDayOfQuarter(" + i + ") = "
+ dateHelper.getLastDayOfQuarter(i));
} /* Month */
for (int i = -5; i <= 5; i++) {
System.out.println("FirstDayOfMonth(" + i + ") = "
+ dateHelper.getFirstDayOfMonth(i));
System.out.println("LastDayOfMonth(" + i + ") = "
+ dateHelper.getLastDayOfMonth(i));
} /* Week */
for (int i = -5; i <= 5; i++) {
System.out.println("FirstDayOfWeek(" + i + ") = "
+ dateHelper.getFirstDayOfWeek(i));
System.out.println("LastDayOfWeek(" + i + ") = "
+ dateHelper.getLastDayOfWeek(i));
} /* Tendays */
for (int i = -5; i <= 5; i++) {
System.out.println("FirstDayOfTendays(" + i + ") = "
+ dateHelper.getFirstDayOfTendays(i));
System.out.println("LastDayOfTendays(" + i + ") = "
+ dateHelper.getLastDayOfTendays(i));
}
} /**
* 取当前日期的字符串形式,"XXXX年XX月XX日"
*
* @return java.lang.String
*/
public static String getPrintDate() {
String printDate = "";
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
printDate += calendar.get(Calendar.YEAR) + "年";
printDate += (calendar.get(Calendar.MONTH) + 1) + "月";
printDate += calendar.get(Calendar.DATE) + "日";
return printDate;
} /**
* 将指定的日期字符串转化为日期对象
*
* @param dateStr
* 日期字符串
* @return java.util.Date
*/
public static Date getDate(String dateStr, String format) {
if (dateStr == null) {
return new Date();
}
if (format == null) {
format = "yyyy-MM-dd";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date date = sdf.parse(dateStr);
return date;
} catch (Exception e) {
return null;
}
} /**
* 从指定Timestamp中得到相应的日期的字符串形式 日期"XXXX-XX-XX"
*
* @param dateTime
* @return 、String
*/
public static String getDateFromDateTime(Timestamp dateTime) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(dateTime).toString();
} /**
* 得到当前时间 return java.sql.Timestamp
*
* @return Timestamp
*/
public static Timestamp getNowTimestamp() {
long curTime = System.currentTimeMillis();
return new Timestamp(curTime);
}
}
java时间处理工具类--DateUtils的更多相关文章
- JAVA 日期处理工具类 DateUtils
package com.genlot.common.utils; import java.sql.Timestamp;import java.text.ParseException;import ja ...
- JAVA时间Date工具类
package com.common.util; import java.text.DateFormat; import java.text.ParseException; import java.t ...
- 时间处理工具类DateUtils
public class DateUtils { public static final String SHORT_DATE ...
- java 日期工具类DateUtils
日期工具类DateUtils CreateTime--2017年5月27日08:48:00Author:Marydon DateUtils.java-对日期类的进一步封装 import java. ...
- 使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间。
1.使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间. package com.fline.aic.utils; import java.text.DateFormat ...
- [java工具类01]__构建格式化输出日期和时间的工具类
在之前的学习中,我写过一篇关于字符串格式化的,就主要设计到了时间以及日期的各种格式化显示的设置,其主要时通过String类的fomat()方法实现的. 我们可以通过使用不同的转换符来实现格式化显示不同 ...
- Java中Date类型如何向前向后滚动时间,( 附工具类)
Java中的Date类型向前向后滚动时间(附工具类) 废话不多说,先看工具类: import java.text.SimpleDateFormat; import java.util.Calendar ...
- Java日期时间实用工具类
Java日期时间实用工具类 1.Date (java.util.Date) Date(); 以当前时间构造一个Date对象 Date(long); 构造函数 ...
- java时间切片工具
项目中经常会遇到根据根据时间区间来查询数据的场景, 如时间跨度大可能相应的sql的执行效率会显著降低, 因此可以对时间区间进行切割成若干个小范围的时间片, 这样不仅可以提高sql的性能还可以做一下并发 ...
随机推荐
- C#字符串要点(复习专用)
一.字符串 通过string定义一个字符串,或者通过String类来创建对象. 通过new String() 创建有一下几种构造函数(从元数据),以此顺序创建string: // // 摘要: // ...
- 使用Google浏览器开发者工具学习HTTP请求记录
GET请求 1.Google浏览器开发者工具截图图示 2.General Request URL :为请求链接 Status Code :为HTTP响应状态码 3.ResponseHeaders :响 ...
- jzoj5683. 【GDSOI2018模拟4.22】Prime (Min_25筛+拉格朗日插值+主席树)
题面 \(n\leq 10^{12},k\leq 100\) 题解 一眼就是一个\(Min\_25\)筛+拉格朗日插值优化,然而打完之后交上去发现只有\(60\)分 神\(tm\)还要用主席树优化-- ...
- Java泛型<>内各种参数的异同
先说下本篇随笔主要涉及到的东西(参考Java编程思想一书): 1.说明 List<Fruit> 与 List<Apple> 之间为什么是非继承关系. 2.由 1 引出的问题说明 ...
- P1903 [国家集训队]数颜色 / 维护队列 带修改的莫队
\(\color{#0066ff}{ 题目描述 }\) 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支 ...
- firefox浏览本地网站慢的问题
用火狐调试本地站点网站,总感觉有点迟钝 经查,原来是火狐会检测网站来源,具体工作原理不详 解决办法 依次打开:C:\Windows\System32\drivers\etc 用记事本或editplus ...
- Entity Framework 更新带外键的实体为null
using (var ctx = new PortalContext()){ var city = ctx.Cities.Find(42); ctx.Entry(city) ...
- linux中firewall与iptables防火墙服务
火墙firewall-cmd --state 查看火墙的状态firewall-cmd --get-active-zones 目前所处的域firewall-cmd --get-default-zone ...
- Java——flush()方法
Java在使用流时,缓冲区是一种发送数据的高效方法,但当溢出缓冲区的部分需要用flush()方法强制将数据发送出去,不必等到缓冲区再次装满,尤其是在数据量特别小的情况下,如果不使用此方法,很容易出现流 ...
- OpenCV属性页配置问题~
详细的OpenCV属性页的安装流程,参考这个网站:http://m.bubuko.com/infodetail-793518.html 运行例子时候,如果遇到这个问题,可能配置环境时候出现问题了. 此 ...