- 浏览: 47648 次
- 性别:
- 来自: 西安
最新评论
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
* @version 1.0.0
* @create 2008-07-24 10:10
* @see 处理时间(包括时间格式的类)
*/
public class DateUtils {
/**
* 取时间年的格式代码
*/
public static String YEAR = "yyyy";
/**
* 取时间月的格式代码
*/
public static String MONTH = "MM";
/**
* 取时间日的格式代码
*/
public static String DAY = "dd";
/**
* 取时间时的格式代码
*/
public static String HOUR = "hh";
/**
* 取时间24小时制的格式代码
*/
public static String HOUR_24 = "HH";
/**
* 取时间分的格式代码
*/
public static String MIMUTE = "mm";
/**
* 取时间秒的格式代码
*/
public static String SECOND = "ss";
/**
* 取时间毫秒的格式代码
*/
public static String MILLISECOND = "SS";
/**
* 格式为yyyy-MM-dd的时间
*/
public static String YMD_FORMAT = YEAR + "-" + MONTH + "-" + DAY;
/**
* 格式为yyyy-MM-dd HH:mm:ss的时间
*/
public static String YMDHMS_FORMAT = YEAR + "-" + MONTH + "-" + DAY + " "
+ HOUR_24 + ":" + MIMUTE + ":" + SECOND;
/**
* 格式为yyyy-MM-dd HH:mm:ss:SS的时间
*/
public static String UTILTIME_FORMAT = YEAR + "-" + MONTH + "-" + DAY + " "
+ HOUR_24 + ":" + MIMUTE + ":" + SECOND + ":" + MILLISECOND;
/**
* 格式为yyyyMMddHHmmssSS的时间
*/
public static String CRITERIONTIME_FORMAT = YEAR + MONTH + DAY + HOUR_24
+ MIMUTE + SECOND + MILLISECOND;
public static String getYearMonthDay() {
return getDateByFormat(YEAR + "-" + MONTH + "-" + DAY);
}
/**
* @see 返回格式为yyyy-MM-dd HH:mm:ss:SS的时间字符串
* @return String date
*/
public static String getNOWTime_0() {
return getDateByFormat(UTILTIME_FORMAT);
}
/**
* @see 返回格式为yyyyMMddHHmmssSS的时间字符串
* @return String date
*/
public static String getNOWTime_1() {
return getDateByFormat(CRITERIONTIME_FORMAT);
}
/**
* @see 获得指定时间格式
* @param String
* format 时间格式
* @return String dateStr 返回获得相应格式时间的字符串
*/
public static String getDateByFormat(String format) {
String dateStr = "";
try {
if (format != null) {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat simFormat = new SimpleDateFormat(format,
Locale.CHINA);
dateStr = simFormat.format(date);
}
} catch (Exception e) {
e.printStackTrace();
}
return dateStr;
}
/**
* @see 获得指定时间格式
* @param Date
* date 时间
* @param String
* format 时间格式
* @return String dateStr 返回获得相应格式时间的字符串
*/
public static String getDateByFormatYMD(Date date) {
String dateStr = "";
try {
SimpleDateFormat simFormat = new SimpleDateFormat(YMD_FORMAT,
Locale.CHINA);
dateStr = simFormat.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return dateStr;
}
/**
* @see 获得指定时间格式
* @param Date
* date 时间
* @param String
* format 时间格式
* @return String dateStr 返回获得相应格式时间的字符串
*/
public static String getDateByFormat(Date date, String format) {
String dateStr = "";
try {
if (format != null) {
SimpleDateFormat simFormat = new SimpleDateFormat(format,
Locale.CHINA);
dateStr = simFormat.format(date);
}
} catch (Exception e) {
e.printStackTrace();
}
return dateStr;
}
/**
* @see 获得当前时间
* @return Date date
*/
public static Date getNOWTime() {
return new Date(System.currentTimeMillis());
}
/**
* @see 把字符串类型的时间转换为yyyy-MM-dd的时间格式
*/
public static Date getDateByStrToYMD(String str) {
Date date = null;
if (str != null && str.trim().length() > 0) {
DateFormat dFormat = new SimpleDateFormat(YMD_FORMAT);
try {
date = dFormat.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
}
return date;
}
/**
* @see 把字符串类型的时间转换为自定义格式的时间
*/
public static Date getDateByStrToFormat(String format, String str) {
DateFormat dFormat = new SimpleDateFormat(format);
Date date = null;
try {
if (str != null) {
date = dFormat.parse(str);
}
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 功能:判断输入年份是否为闰年<br>
*
* @param year
* @return 是:true 否:false
* @author pure
*/
public static boolean leapYear(int year) {
boolean leap;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
leap = true;
} else {
leap = false;
}
} else {
leap = true;
}
} else
leap = false;
return leap;
}
/**
* 功能:得到指定月份的月底 格式为:xxxx-yy-zz (eg: 2007-12-31)<br>
*
* @param String
* @return String
*/
public static String getEndOfMonth(String str) {
int tyear = Integer.parseInt(getDateByFormat(getDateByStrToYMD(str),
YEAR));
int tmonth = Integer.parseInt(getDateByFormat(getDateByStrToYMD(str),
MONTH));
String strtmonth = null;
String strZ = null;
if (tmonth == 1 || tmonth == 3 || tmonth == 5 || tmonth == 7
|| tmonth == 8 || tmonth == 10 || tmonth == 12) {
strZ = "31";
}
if (tmonth == 4 || tmonth == 6 || tmonth == 9 || tmonth == 11) {
strZ = "30";
}
if (tmonth == 2) {
if (leapYear(tyear)) {
strZ = "29";
} else {
strZ = "28";
}
}
strtmonth = tmonth >= 10 ? String.valueOf(tmonth) : ("0" + tmonth);
return tyear + "-" + strtmonth + "-" + strZ;
}
public static String getEndOfMonth(int tyear, int tmonth) {
String strtmonth = null;
String strZ = null;
if (tmonth == 1 || tmonth == 3 || tmonth == 5 || tmonth == 7
|| tmonth == 8 || tmonth == 10 || tmonth == 12) {
strZ = "31";
}
if (tmonth == 4 || tmonth == 6 || tmonth == 9 || tmonth == 11) {
strZ = "30";
}
if (tmonth == 2) {
if (leapYear(tyear)) {
strZ = "29";
} else {
strZ = "28";
}
}
strtmonth = tmonth >= 10 ? String.valueOf(tmonth) : ("0" + tmonth);
return tyear + "-" + strtmonth + "-" + strZ;
}
/**
* 功能:得到指定月份的月初 格式为:xxxx-yy-zz (eg: 2007-12-01)<br>
*
* @param String
* @return String
*/
public static String getStartOfMonth(int tyear, int tmonth) {
String strtmonth = tmonth >= 10 ? String.valueOf(tmonth)
: ("0" + tmonth);
return tyear + "-" + strtmonth + "-" + "01";
}
public static String getStartOfMonth(String str) {
int tyear = Integer.parseInt(getDateByFormat(getDateByStrToYMD(str),
YEAR));
int tmonth = Integer.parseInt(getDateByFormat(getDateByStrToYMD(str),
MONTH));
String strtmonth = tmonth >= 10 ? String.valueOf(tmonth)
: ("0" + tmonth);
return tyear + "-" + strtmonth + "-" + "01";
}
/**
* 功能:得到指定月份的月初 格式为:xxxx-yy-zz (eg: 2007-12-01)<br>
*
* @param String
* @return String
*/
public static int getMonthCountBySQU(String start, String end) {
int syear = Integer.parseInt(getDateByFormat(getDateByStrToYMD(start),
YEAR));
int smonth = Integer.parseInt(getDateByFormat(getDateByStrToYMD(start),
MONTH));
int eyear = Integer.parseInt(getDateByFormat(getDateByStrToYMD(start),
YEAR));
int emonth = Integer.parseInt(getDateByFormat(getDateByStrToYMD(start),
MONTH));
return (eyear - syear) * 12 + (emonth - smonth) + 1;
}
/**
* 获得时间序列 EG:2008-01-01~2008-01-31,2008-02-01~2008-02-29
*/
public static List getMonthSqu(String fromDate, String toDate) {
List list = new ArrayList();
int count = getMonthCountBySQU(fromDate, toDate);
int syear = Integer.parseInt(getDateByFormat(
getDateByStrToYMD(fromDate), YEAR));
int smonth = Integer.parseInt(getDateByFormat(
getDateByStrToYMD(fromDate), MONTH));
int eyear = Integer.parseInt(getDateByFormat(getDateByStrToYMD(toDate),
YEAR));
String startDate = fromDate;
String endDate = "";
for (int i = 1; i <= count; i++) {
if (syear <= eyear) {
startDate = getStartOfMonth(syear, smonth);
endDate = getEndOfMonth(syear, smonth);
list.add(startDate + "~" + endDate);
System.out.println(startDate + "~" + endDate);
if (smonth == 13) {
smonth = 1;
syear++;
}
smonth++;
}
}
return list;
}
/**
* 通过传入的时间来获得所属周内的时间
*
* @param start
* @param num
* @return
*/
public static String getDateOFWeekByDate(String start, int num) {
Date dd = getDateByStrToYMD(start);
Calendar c = Calendar.getInstance();
c.setTime(dd);
if (num == 1) // 返回星期一所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
else if (num == 2) // 返回星期二所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
else if (num == 3) // 返回星期三所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
else if (num == 4) // 返回星期四所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
else if (num == 5) // 返回星期五所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
else if (num == 6) // 返回星期六所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
else if (num == 0) // 返回星期日所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return getDateByFormatYMD(c.getTime());
}
}
发表评论
-
java 单元测试 初探
2010-12-06 21:33 785单元测试工具junit.jar 用junit进行测试,给出错误 ... -
java 开发几个设计原则;
2010-11-12 16:54 923java 开发几个设计原则 ... -
对象的序列化
2010-11-05 15:07 674下面将实现一个序列化对象的例子,也就是将对象保存到文件中,在从 ... -
未完任务
2010-10-17 18:05 0l Template Method模式 l Factory ... -
敏捷开发读书笔记一
2010-10-12 07:28 873要做到敏捷开发需要做到以下几点 A不要有重复的代码; B去 ...
相关推荐
public static DateUtils getInstance(Date fiducialDate) { return new DateUtils(fiducialDate); } /** * 获取DateHelper实例, 使用当前时间作为基准时间 * * @return Date */ public static ...
* 文件名:DateUtils.java 日期处理相关工具类 * 版本信息:V1.0 * 日期:2013-03-11 * Copyright BDVCD Corporation 2013 * 版权所有 http://www.bdvcd.com */ public class DateUtils { /**定义常量**/ ...
总的来说,`DateUtils` 类是Java开发中的一个实用工具,它提供了丰富的日期时间处理功能,而`Lunar` 类则扩展了对农历日期的支持。合理使用这两个类,可以有效地管理和操作日期和时间,提高代码的可读性和可维护性。...
在Java编程中,DateUtils工具类是一个非常实用的辅助类,它封装了各种日期和时间处理的方法,大大简化了开发者在处理日期时的工作。这里我们深入探讨一下自定义的DateUtils工具类及其重要功能。 首先,`DateUtils`...
`DateUtils`不是Java标准库的一部分,但许多开源框架如Apache Commons Lang提供了这个工具类,它提供了丰富的日期处理函数,提高了开发效率。 `DateUtils`类主要包含以下功能: 1. **日期格式化与解析**: - `...
在这个名为"flex 学习项目中总结的时间处理工具类DateUtils"的项目中,我们可能找到了一个针对Flex开发环境优化的日期处理工具。Flex是一种基于Adobe ActionScript的开源框架,主要用于构建富互联网应用(RIA)。 `...
Java日期处理工具类DateUtils详解 Java日期处理工具类DateUtils是Java中一个非常有用的工具类,它提供了一系列日期和时间处理相关的操作,涵盖了日期和时间的格式化、解析、计算等多方面的内容。本文将详细介绍...
一些日期的处理,获取当前时间、date日期和字符串相互转化等
Java 中的 DateUtils 工具类是 Java 语言中的一种常用工具类,用于处理日期和时间的转换。该工具类提供了多种日期和时间的转换方法,包括 String 转 Timestamp、String 转 Date、Date 转 String、Date 转 Timestamp ...
DateUtils类是Java中的一个工具类,提供了多种日期和时间相关的方法,能够帮助开发者更方便地处理日期和时间相关的操作。DateUtils类中提供了多种方法,包括获取当前日期、获取当前日期时间、将字符串日期转换为日期...
在Java编程中,Date类是处理日期和时间的基础类,但在实际开发中,由于Date类本身的API设计并不十分友好,通常我们会使用工具类来简化日期的处理工作。本主题聚焦于一个名为"Date日期操作工具类"的实用工具,它提供...
Java 中DateUtils日期工具类的实例详解 Java 中DateUtils日期工具类是 Java 语言中对日期类型的操作的重要...在实际应用中,DateUtils 日期工具类可以极大地简化日期类型的处理,使得开发者可以更方便地处理日期类型。
3. **rrule**:`rrule` 模块实现了 RFC 2445 规范中的重复规则,可以方便地处理复杂的日期间隔,如每天、每周二、每季度的最后一天等。 ```python from dateutil.rrule import rrule, DAILY dates = rrule(DAILY...
`DateUtils.java` 是一个Java编程中的自定义工具类,专门用于处理日期和时间相关的操作。在Java开发中,处理日期和时间是非常常见的需求,例如计算两个日期之间的差距、格式化日期显示、获取当前时间等。`DateUtils`...
"日期处理工具类"通常是指自定义的类或使用Java内置的日期时间API来执行与日期相关的操作,如日期加减、日期格式化以及获取特定周或日等功能。在本案例中,我们有一个名为`DateUtils`的类,它可能包含了这些实用方法...
DateUtils 是一个 Java 日期工具类,提供了日期相关的常用方法和变量,方便在项目开发中使用。该类提供了多种日期格式化方式,包括年月日、时分秒、年月日时分等,并提供了字符串转换为日期、日期比较等方法。 常用...
在Java编程语言中,日期和时间的处理是一个常见的任务,而`DateUtils`类通常是为了简化这类操作而自定义的工具类。这个`DateUtils.zip`压缩包包含了一个名为`Time的帮助类DateUtils.txt`的文件,我们可以从中学习到...
标题中的"StringUtils", "NumberUtils" 和 "DateUtils" 是Java开发中常见的工具类库,分别用于处理字符串、数字和日期。这些工具类通常由Apache Commons Lang、Guava或者Spring Framework等开源框架提供,旨在提高...
1. **DateUtils**: `java.util.Date` 和 `java.time` 包含日期和时间的操作,但DateUtils通常是Apache Commons Lang库中的一个工具类,提供更方便的日期处理方法,如格式化、解析、比较和日期的加减操作。...
在Java编程语言中,日期和时间的处理是常见的任务,而`DateUtils`通常是一个自定义的工具类,用于简化日期相关的操作。`DateUtils.rar`这个压缩包包含了一个名为`DateUtils.java`的源代码文件,我们可以推测这是一个...