- 浏览: 122325 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (165)
- 数据库 (45)
- 架构 (0)
- java web前端+框架 (9)
- java web后端+框架 (56)
- Eclipse插件 (1)
- 解题思路 (2)
- Eclipse (2)
- linux (6)
- jquery (4)
- 正则 (3)
- jsp (1)
- javascript (8)
- oracle (39)
- 生活 (1)
- weblogic (5)
- tomcat (3)
- Jrebel (1)
- powerdesigner (1)
- svn (1)
- log4j (1)
- IDE (1)
- POI (2)
- jvm (2)
- ssh (1)
- http (1)
- notepad++ (2)
- 润乾 (1)
- 设计模式 (0)
- 实用类Utils (2)
- 算法 (1)
- xml (1)
- 实用类 (2)
最新评论
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Months;
import org.joda.time.Weeks;
import org.joda.time.Years;
import com.achievo.core.exception.DataInvalidException;
public class DateTimeUtils {
public static double LOCAL_TIME_ZONES = 8/24d;
public final static String[] parsePatterns = new String[] {
"yyyy-M-d",
"yyyy-MM-dd",
"yyyy-M-dd",
"yyyy-MM-d",
"yyyy-M-d H:m",
"yyyy-M-d H:mm",
"yyyy-M-d HH:m",
"yyyy-M-d HH:mm",
"yyyy-M-d H:m:s",
"yyyy-MM-dd HH:mm:ss",
"yyyy-M-d H:m:s.S",
"yyyy-MM-dd H:mm",
"yyyy-MM-dd HH:m",
"yyyy-MM-dd HH:mm",
"yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd HH:mm:ss.S",
"yyyy-MM-dd HH:mm:ss.SS",
"yyyy-MM-dd HH:mm:ss.SSS" };
private final static String[] weekNames = new String[] { "", "一", "二", "三", "四", "五", "六", "日" };
public final static String datePattern = "yyyy-MM-dd";
public final static String timePattern = "yyyy-MM-dd HH:mm:ss";
// public final static DateFormat dateFormat = new SimpleDateFormat(datePattern);
// public final static DateFormat timeFormat = new SimpleDateFormat(timePattern);
/**
* 获取当前系统日历
*
* @return
* @throws IllegalArgumentException
* @see [类、类#方法、类#成员]
*/
public static Calendar getCalendar() {
return Calendar.getInstance();
}
/**
* 获取当前系统时间,应统一调用,可根据用户的具体需求进行修改
*
* @return
* @throws IllegalArgumentException
* @see [类、类#方法、类#成员]
*/
public static Date getDate() {
return getCalendar().getTime();
}
/**
* 获取当前系统时间,返回java.sql.Date
*
* @return
* @throws IllegalArgumentException
* @see [类、类#方法、类#成员]
*/
public static java.sql.Date getSQLDate() {
return toSqlDate(getCalendar().getTime());
}
/**
* 获取当前系统日期
*
* @return
* @throws IllegalArgumentException
* @see [类、类#方法、类#成员]
*/
public static String getToDateString() {
return toDateString(getDate());
}
/**
* 将Date转Calendar
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static Calendar toCalendar(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
/**
* 按默认样式转换字符串到日期类型
*
* @param text
* @return
* @throws IllegalArgumentException
* @see [类、类#方法、类#成员]
*/
public static Date parseDate(final String text) throws IllegalArgumentException {
return parseDate(text, parsePatterns);
}
/**
* 按自定义样式转换字符串到日期类型
*
* @param text
* @param patterns
* @return
* @throws IllegalArgumentException
* @see [类、类#方法、类#成员]
*/
public static Date parseDate(final String text, final String... patterns) throws DataInvalidException {
try {
boolean parseAble = false;
for (int i = 0; (i < patterns.length) && !parseAble; i++) {
parseAble = (text.length() >= patterns[i].length());
}
if (parseAble) {
return DateUtils.parseDate(text, parsePatterns);
} else {
throw new DataInvalidException("0101010004", text+",日期");
}
} catch (ParseException ex) {
throw new DataInvalidException("0101010005", text+",日期");
}
}
/**
* 转换为日期字符串
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static String toDateString(final Date date) {
return parseString(date, "yyyy-MM-dd");
}
/**
* 转换为时间字符串
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static String toTimeString(final Date date) {
return parseString(date, "HH:mm:ss");
}
/**
* 转换为日期时间字符串
*
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static String toDateTimeString(final Date date) {
return parseString(date, "yyyy-MM-dd HH:mm:ss");
}
/**
* 转换为指定格式字符串
*
* @param date
* @param pattern
* @return
* @see [类、类#方法、类#成员]
*/
public static String parseString(final Date date, final String pattern) {
if(date == null){
throw new DataInvalidException("0101010001", "日期date");
}
if(pattern == null){
throw new DataInvalidException("0101010001", "日期格式pattern");
}
DateFormat dateFormat = new SimpleDateFormat(pattern, Locale.CHINESE);
return dateFormat.format(date);
}
/**
* 得到星期几(中文)
*
* @param date
* @return
*/
public static String cnWeekDay(Date date) {
return weekNames[getWeekDay(date)];
}
/**
* 得到星期几
*
* @param date
* @return
*/
public static int getWeekDay(Date date) {
if(date == null){
throw new DataInvalidException("0101010001", "日期date");
}
Calendar c = toCalendar(date);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
dayOfWeek = dayOfWeek==0?7:dayOfWeek;
return dayOfWeek;
}
/**
* 计算begin和end之间的天数
*
* @param begin
* @param end
* @return
* @see [类、类#方法、类#成员]
*/
public static int diffDays(final Date begin, final Date end) {
DateTime jStart = new DateTime(begin);
DateTime jEnd = new DateTime(end);
return Days.daysBetween(jStart, jEnd).getDays();
}
/**
* 计算begin和end之间的周数
*
* @param begin
* @param end
* @return
* @see [类、类#方法、类#成员]
*/
public static int diffWeeks(final Date begin, final Date end) {
DateTime jStart = new DateTime(begin);
DateTime jEnd = new DateTime(end);
return Weeks.weeksBetween(jStart, jEnd).getWeeks();
}
/**
* 计算begin和end之间的月数
*
* @param begin
* @param end
* @return
* @see [类、类#方法、类#成员]
*/
public static int diffMonths(final Date begin, final Date end) {
DateTime jStart = new DateTime(begin);
DateTime jEnd = new DateTime(end);
return Months.monthsBetween(jStart, jEnd).getMonths();
}
/**
* 计算begin和end之间的年数
*
* @param begin
* @param end
* @return
* @see [类、类#方法、类#成员]
*/
public static int diffYears(final Date begin, final Date end) {
DateTime jStart = new DateTime(begin);
DateTime jEnd = new DateTime(end);
return Years.yearsBetween(jStart, jEnd).getYears();
}
/**
* 对date,加上day天
*
* @param date
* @param day
* @return String
* @see [类、类#方法、类#成员]
*/
public static String addDay(final Date date, final int day) {
DateFormat dateFormat = new SimpleDateFormat(datePattern, Locale.CHINESE);
return dateFormat.format(operateDay(date, day));
}
/**
* 对date,加上day天
*
* @param date
* @param day
* @return Date
* @see [类、类#方法、类#成员]
*/
public static Date operateDay(final Date date, final int day) {
// return DateUtils.addDays(date, amount);
Calendar c = toCalendar(date);
c.add(Calendar.DATE, day);
return c.getTime();
}
/**
* 对date,加上month月
*
* @param date
* @param month
* @return String
* @see [类、类#方法、类#成员]
*/
public static String addMonth(final Date date, final int month) {
DateFormat dateFormat = new SimpleDateFormat(datePattern, Locale.CHINESE);
return dateFormat.format(operateDay(date, month));
}
/**
* 对date,加上month月
*
* @param date
* @param month
* @return Date
* @see [类、类#方法、类#成员]
*/
public static Date operateMonth(final Date date, final int month) {
Calendar c = toCalendar(date);
c.add(Calendar.MONTH, month);
return c.getTime();
}
/**
* 对date,加上year年
*
* @param date
* @param year
* @return String
* @see [类、类#方法、类#成员]
*/
public static String addYear(final Date date, final int year) {
DateFormat dateFormat = new SimpleDateFormat(datePattern, Locale.CHINESE);
return dateFormat.format(operateDay(date, year));
}
/**
* 对date,加上year年
*
* @param date
* @param year
* @return Date
* @see [类、类#方法、类#成员]
*/
public static Date opearteYear(final Date date, final int year) {
Calendar c = toCalendar(date);
c.add(Calendar.YEAR, year);
return c.getTime();
}
/**
* 比较日期 ,得到较大的日期
*/
public Date greatDate(Date date1, Date date2) {
return date1.getTime() > date2.getTime() ? date1 : date2;
}
/**
* 比较日期 ,得到较小的日期
*/
public Date lessDate(Date date1, Date date2) {
return date1.getTime() < date2.getTime() ? date1 : date2;
}
/**
* 当前年
* @return
* @see [类、类#方法、类#成员]
*/
public static int getToYear(){
return getCalendar().get(Calendar.YEAR);
}
/**
* 当前月
* @return
* @see [类、类#方法、类#成员]
*/
public static int getToMonth(){
return (getCalendar().get(Calendar.MONTH)+1);
}
/**
* 当前日(月中日期)
* @return
* @see [类、类#方法、类#成员]
*/
public static int getToDay(){
return getCalendar().get(Calendar.DAY_OF_MONTH);
}
/**
* date的年
* @return
* @see [类、类#方法、类#成员]
*/
public static int getYear(Date date){
return toCalendar(date).get(Calendar.YEAR);
}
/**
* date的月
* @return
* @see [类、类#方法、类#成员]
*/
public static int getMonth(Date date){
return (toCalendar(date).get(Calendar.MONTH)+1);
}
/**
* date的日(月中日期)
* @return
* @see [类、类#方法、类#成员]
*/
public static int getDay(Date date){
return toCalendar(date).get(Calendar.DAY_OF_MONTH);
}
/**
* 根据year,month,day获得Date
* @param year
* @param month
* @param day
* @return
* @see [类、类#方法、类#成员]
*/
public static Date getDate(int year, int month, int day){
Calendar c = new GregorianCalendar(year, (month-1), day);
return c.getTime();
}
/**
* 根据Date获得当天的起始时间,即0时
* @param year
* @param month
* @param day
* @return
* @see [类、类#方法、类#成员]
*/
public static Calendar toBeginOfCalendar(Date date){
Calendar c = toCalendar(date);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new GregorianCalendar(year, month, day);
}
/**
* 根据Date获得当天的起始时间,即0时
* @param year
* @param month
* @param day
* @return
* @see [类、类#方法、类#成员]
*/
public static Date toBeginOfDate(Date date){
return (toBeginOfCalendar(date)).getTime();
}
/**
* 得到本月最后一天
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static Date getLastDayOfMonth(Date date){
Calendar c = new GregorianCalendar(getYear(date), getMonth(date), 1);
c.add(Calendar.DATE, -1);
return c.getTime();
}
/**
* 将java.util.Date转换为java.sql.Date
* @param date
* @return
* @see [类、类#方法、类#成员]
*/
public static java.sql.Date toSqlDate(Date date){
return new java.sql.Date(date.getTime());
}
/**
* 比较targetDate是不是在beginDate和endDate之间,按天算,也就是yyyy-MM-dd;
* 三个条件必须同时满足:
* 1、beginDate必须小于等于endDate
* 2、targetDate大于等于beginDate
* 3、targetDate小于等于endDate
* @param targetDate
* @param beginDate
* @param endDate
* @return
*/
public static boolean isDayBetween(Date targetDate, Date beginDate, Date endDate){
Calendar targetCalendar = toBeginOfCalendar(targetDate);
Calendar beginCalendar = toBeginOfCalendar(beginDate);
Calendar endCalendar = toBeginOfCalendar(endDate);
//beginDate必须小于等于endDate
boolean isRightRange = beginCalendar.before(endCalendar) || beginCalendar.equals(endCalendar);
//targetDate大于等于beginDate
boolean bigOrEquals = targetCalendar.after(beginCalendar) || targetCalendar.equals(beginCalendar);
//targetDate小于等于endDate
boolean smallOrEquals = targetCalendar.before(endCalendar) || targetCalendar.equals(endCalendar);
//三个条件必须同时满足
return isRightRange&&bigOrEquals&&smallOrEquals;
}
/**
* @param args
*/
public static void main(String[] args) {
/*测试日期间差距*/
Date testDate = parseDate("2001-05-03 23:52:23.32");
System.out.println(testDate);
System.out.println("周" + cnWeekDay(testDate));
System.out.println("test Year:\t" + getYear(testDate));
System.out.println("test Month:\t" + getMonth(testDate));
System.out.println("test Day:\t" + getDay(testDate));
Date toDate = new Date();
System.out.println(toDate);
System.out.println("周" + cnWeekDay(toDate));
System.out.println("To Year:\t" + getToYear());
System.out.println("To Month:\t" + getToMonth());
System.out.println("To Day:\t\t" + getToDay());
System.out.println("getDate():\t" + getDate(2011, 1, 9));
System.out.println("getLastDayOfMonth():\t" + getLastDayOfMonth(getDate(2012, 2, 9)));
System.out.println("diffYears()" + diffYears(getDate(2001, 2, 9),getDate(2011, 1, 21)));
/*测试日期间差距*/
Date d1 = new Date();
System.out.println(getWeekDay(d1));
Date d2 = parseDate("2011-12-29");
System.out.println(toBeginOfDate(d1));
System.out.println(d2);
System.out.println(diffDays(d1, d2));
System.out.println(diffDays(toBeginOfDate(d1), toBeginOfDate(d2)));
/*按天比较*/
Date targetDate = parseDate("2012-02-28 08:52:23.32"),
beginDate = parseDate("2012-02-27 00:00:00.00"),
endDate = parseDate("2012-02-28 00:00:00.00");
System.out.println(isDayBetween(targetDate, beginDate, endDate));
/*
Calendar targetCalendar = toBeginOfCalendar(targetDate);
Calendar beginCalendar = toBeginOfCalendar(beginDate);
Calendar endCalendar = toBeginOfCalendar(endDate);
System.out.println(targetCalendar.after(beginCalendar));
System.out.println(targetCalendar.equals(beginCalendar));
System.out.println(targetCalendar.before(endCalendar));
System.out.println(targetCalendar.equals(endCalendar));
// */
}
}
发表评论
-
eclipse java代码格式化 javaformatter20150123.zip
2015-01-23 11:30 665eclipse java代码格式化 javaformatter ... -
List to Map
2015-01-21 17:22 437/** * <p>将List变为Map</p ... -
读取access工具类-ReadAccessUtil2
2015-01-06 13:52 505import com.healthmarketscience. ... -
ParameterizedType获取java泛型参数类型
2014-11-14 15:15 662ParameterizedType获取java泛型参数类型 ... -
分页帮助类-oracle-java
2014-10-09 17:46 420/** * 分页帮助类 * * @author ver ... -
java自动实例化List AutoArrayList
2014-07-21 15:51 704import java.util.ArrayList; p ... -
单一登录过滤器
2014-06-20 17:13 362/** * 单一登录过滤器 * * @author v ... -
正则 java 查找 打印 所有匹配项
2014-06-18 11:26 598package com.test.regex; import ... -
第3方包读取Access
2014-05-26 16:07 624package com.test; import java. ... -
多线程加签验签例子.zip
2014-05-26 16:08 479多线程加签验签例子.zip -
listToMap
2014-05-26 16:09 396/** * List实用类 * * @author v ... -
JdbcTemplateRowMapper
2015-05-19 15:44 535import java.lang.reflect.Constr ... -
KeyValuePair2
2015-05-19 15:44 467/** * 键值对 * * @author verno ... -
OrderUtil -排序工具
2014-05-22 09:35 612/** * 排序工具 * * @author vern ... -
ZipUtils
2014-05-22 09:35 420import java.io.BufferedOutputSt ... -
TimerUtils
2014-05-22 09:34 433import org.slf4j.Logger; import ... -
Excel View 4 Spring
2014-05-22 09:29 301Excel View 4 Spring @RequestMa ... -
DOMUtils xml操作实用类
2014-04-07 21:41 776/* * Copyright 2001-2004 The A ... -
生成验证码 数学+字母
2014-04-01 10:08 397public static String getCharAnd ... -
ConnUtils2程序耗时输出 oracle.sql.Clob类型转换成String类型
2014-03-29 10:00 650package com.achievo.ems.web.ser ...
相关推荐
时间处理帮助类
java8日期,所编写的一个工具类
return DateTimeUtils.isLeapYear(year) ? 1 : 0; } else { if (index_data[month - 1] == 31) { return 3; } else { return 2; } } } private void initAdapters() { yearAdapter = ...
最简单的springboot整合swagger案例 ,下载直接可以运行,运行访问地址是http://127.0.0.1:7722/g11n/swagger-ui.html#!/test/test
也可以用在 Listviews (Adapters) 只需在要设置日期的位置添加这些代码行: DateTimeUtils.formatDate(sampleDate, FormatType.TimeFormats.COMPLETE_TIME_FORMAT_WITH_DAY, DateTimeUtils.DateTimeZones.US_...
JavaLibJAVA开发,常用工具集概述JavaLib,是一个Java开发基础工具类库,对项目开发中常用的工具进行封装,如:加密、http请求、API接口等。目的是帮助开发者更快速、更快捷的开发。...功能列表: DateTimeUtils - 日期
产品内容产品代码产品描述产品价格销售内容产品代码销售日期时间数量总价程序文件DateTimeUtils.pas - 包含处理时间和获取随机时间信息的函数。 他们是: 函数 IsLeapYear(year : integer) : boolean; - 此函数验证...
ISO8601 OCaml的ISO 8601和RFC 3339日期解析。 工作正在进行中。 直到1.0.0版本,API才应考虑稳定。 许可模块 该模块的日期,时间和日期时间解析功能比ISO 8601或RFC 3339规范更宽松。 日期和时间可以使用基本...
可用班级SharedPrefsUtils ExifUtil FileUtils ImageUtils 实用程序LogUtils NotificationIdFactory 路径实用程序PermissionUtils DateTimeFormat DateTimeStyle DateTimeUnits DateTimeUtils 全球常量实用程序下载...
DateTimeUtils [代码] DateTimeUtils.GetIntervalIntersection() 计算两个日期时间间隔之间的交集(交叠)。 例子: DateTime now = DateTime . Now ; DateTimeInterval testInterval = new DateTimeInterval...