import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateUtil {
public static String getDateToString(Date date)
{
return getDateToString(date,"MM-dd-yyyy");
}
public static String getDateToString(Date date, String format)
{
SimpleDateFormat df = new SimpleDateFormat(format);
String str = df.format(date);
return str;
}
public static Date getStrToDate(String date)
{
return getStrToDate(date, "MM-dd-yyyy HH:mm:ss");
}
public static Date getStrToDate(String str, String format)
{
SimpleDateFormat df = new SimpleDateFormat(format);
Date date = new Date();;
try
{
date = df.parse(str);
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
/**
* get cuurent Date return java.util.Date type
*/
public static Date getNowUtilDate() {
return new Date();
}
/**
* get current Date return java.sql.Date type
*
* @return
*/
public static java.sql.Date getNowSqlDate() {
return new java.sql.Date(System.currentTimeMillis());
}
/**
* get the current timestamp return java.sql.timestamp
*
* @return
*/
public static Timestamp getNowTimestamp() {
return new Timestamp(System.currentTimeMillis());
}
/**
*
* @param year
* @param month
* @param date
* @param hour
* @param minute
* @param second
* @return
*/
public static Timestamp createTimestamp(int year, int month, int date,
int hour, int minute, int second) {
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, date, hour, minute, second);
cal.set(Calendar.MILLISECOND, 0);
return new Timestamp(cal.getTimeInMillis());
}
/**
*
* @param year
* @param month
* @param date
* @return
*/
public static Timestamp createTimestamp(int year, int month, int date) {
return createTimestamp(year, month, date, 0, 0, 0);
}
/**
*
* @param year
* @param month
* @param date
* @param hour
* @param minute
* @param second
* @return
*/
public static java.util.Date createUtilDate(int year, int month, int date,
int hour, int minute, int second) {
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, date, hour, minute, second);
cal.set(Calendar.MILLISECOND, 0);
return new java.util.Date(cal.getTimeInMillis());
}
/**
*
* @param year
* @param month
* @param date
* @return
*/
public static java.util.Date createUtilDate(int year, int month, int date) {
return createUtilDate(year, month, date, 0, 0, 0);
}
/**
*
* @param year
* @param month
* @param date
* @param hour
* @param minute
* @param second
* @return
*/
public static java.sql.Date createSqlDate(int year, int month, int date,
int hour, int minute, int second) {
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, date, hour, minute, second);
cal.set(Calendar.MILLISECOND, 0);
return new java.sql.Date(cal.getTimeInMillis());
}
/**
*
* @param java.util.Date:date
* @return java.sql.Date: YYYY-MM-DD:HH:00:00
*/
public static java.sql.Timestamp createSqlDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.set(getYear(date), getMonth(date)-1, getDayOfMonth(date), getHour(date), 0, 0);
cal.set(Calendar.MILLISECOND, 0);
return new java.sql.Timestamp(cal.getTimeInMillis());
}
/**
*
* @param java.util.Date:date
* @return java.sql.Date: YYYY-MM-DD:HH:00:00
*/
public static java.sql.Timestamp createPrevHourSqlDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.set(getYear(date), getMonth(date)-1, getDayOfMonth(date), getHour(date)-1, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
return new java.sql.Timestamp(cal.getTimeInMillis());
}
/**
*
* @param java.util.Date:date
* @param int hour interval
* @return java.sql.Date: YYYY-MM-DD:HH:00:00
*/
public static java.sql.Date createPrevHourSqlDate(Date date, int hours) {
Calendar cal = Calendar.getInstance();
cal.set(getYear(date), getMonth(date), getDayOfMonth(date), getHour(date)-hours, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
return new java.sql.Date(cal.getTimeInMillis());
}
/**
*
* @param year
* @param month
* @param date
* @return
*/
public static java.sql.Date createSqlDate(int year, int month, int date) {
return createSqlDate(year, month, date, 0, 0, 0);
}
/**
* format java.util.Date java.sql.Date,java.sql.Timestamp to the string with format,
* the format default is "yyyy-MM-dd HH:mm:ss"
* @param time
* @param format
* @return
*/
public static String convertDateToString(java.util.Date date, String format) {
if (format == null || format.length() == 0)
format = "yyyy-MM-dd HH:mm:ss";
DateFormat sf = new SimpleDateFormat(format);
return sf.format(date);
}
/**
* use "yyyy-MM-dd HH:mm:ss" format the java.util.Date java.sql.Date,java.sql.Timestamp to the string
* @param time
* @return
*/
public static String convertDateToString(java.util.Date date) {
return convertDateToString(date, "yyyy-MM-dd HH:mm:ss");
}
/**
* create Timestamp with giving String
* @param dateString
* @return
*/
public static Timestamp createTimestamp(String dateString) {
try {
if (dateString == null || dateString.length() == 0)
return null;
Date tmpDate = createUtilDate(dateString);
if (tmpDate != null)
return new java.sql.Timestamp(tmpDate.getTime());
else
return Timestamp.valueOf(dateString);
}
catch (Exception e) {}
return null;
}
/**
* get the Timestamp with String with pattern
* @param dateString
* @param pattern
* @return
*/
public static Timestamp createTimestamp(String dateString,
String pattern) {
try {
if (dateString == null || dateString.length() == 0)
return null;
if(pattern==null || pattern.length()==0 )
return createTimestamp(dateString);
DateFormat sf = new SimpleDateFormat(pattern.trim());
Date tmpDate = sf.parse(dateString.trim());
if(tmpDate !=null)
return new Timestamp(tmpDate.getTime());
}
catch (Exception e) {
//e.printStackTrace();
}
return null;
}
/**
* create java.sql.Date with giving String
* @param dateString
* @return
*/
public static java.sql.Date createSqlDate(String dateString) {
try {
if (dateString == null || dateString.length() == 0)
return null;
Date tmpDate = createUtilDate(dateString);
if (tmpDate != null)
return new java.sql.Date(tmpDate.getTime());
else
return java.sql.Date.valueOf(dateString);
}
catch (Exception e) {}
return null;
}
/**
* get the sql date with String with pattern
* @param dateString
* @param pattern
* @return
*/
public static java.sql.Date createSqlDate(String dateString,
String pattern) {
try {
if (dateString == null || dateString.length() == 0)
return null;
if(pattern==null || pattern.length()==0 )
return createSqlDate(dateString);
DateFormat sf = new SimpleDateFormat(pattern.trim());
Date tmpDate = sf.parse(dateString.trim());
if(tmpDate !=null)
return new java.sql.Date(tmpDate.getTime());
}
catch (Exception e) {
//e.printStackTrace();
}
return null;
}
/**
* get the util date with String with pattern
* @param dateString
* @param pattern
* @return
*/
public static java.util.Date createUtilDate(String dateString,
String pattern) {
try {
if (dateString == null || dateString.length() == 0)
return null;
if(pattern==null || pattern.length()==0 )
return createUtilDate(dateString);
DateFormat sf = new SimpleDateFormat(pattern.trim());
return sf.parse(dateString.trim());
}
catch (Exception e) {
//e.printStackTrace();
}
return null;
}
/**
* create Timestamp with giving String, the string Foramt is must "yyyy-MM-dd HH:mm:ss" or "yyyy/MM/dd HH:mm:ss"
* or "yyyy-MM-dd" or "yyyy/MM/dd"
* @param dateString
* @return
*/
public static java.util.Date createUtilDate(String dateString) {
try {
if (dateString == null || dateString.length() == 0)
return null;
String pattern = "";
dateString = dateString.trim();
if (dateString.length() == 10) {
if (dateString.indexOf("/") != -1)
pattern = "yyyy/MM/dd";
else if (dateString.indexOf("-") != -1)
pattern = "yyyy-MM-dd";
else
return null;
}
else if (dateString.length() == 19) {
if (dateString.indexOf("/") != -1)
pattern = "yyyy-MM-dd HH:mm:ss";
else if (dateString.indexOf("-") != -1)
pattern = "yyyy-MM-dd HH:mm:ss";
}
else if (dateString.length() == 8) {
pattern = "yyyyMMdd";
}
DateFormat sf = new SimpleDateFormat(pattern);
return sf.parse(dateString.trim());
}
catch (Exception e) {
//e.printStackTrace();
}
return null;
}
/**
* convert the java.util.Date ,java.sql.Date ,java.sql.Timestamp to Calendar
*
* @param time
* @return
*/
public static Calendar convertDateToCalendar(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal;
}
/**
* convert the calendar to Timestamp
* @param cal
* @return
*/
public static Timestamp convertCalendarToTimes(Calendar cal) {
return new Timestamp(cal.getTimeInMillis());
}
/**
* convert the calendar to java.util.Date
* @param cal
* @return
*/
public static java.util.Date convertCalendarToUtilDate(Calendar cal) {
return new java.util.Date(cal.getTimeInMillis());
}
/**
* convert the calendar to java.sql.Date
* @param cal
* @return
*/
public static java.sql.Date convertCalendarToSqlDate(Calendar cal) {
return new java.sql.Date(cal.getTimeInMillis());
}
/**
*
* @param date
* @return
*/
public static int getYear(java.util.Date date) {
return convertDateToCalendar(date).get(Calendar.YEAR);
}
/**
*
* @param date
* @return
*/
public static int getMonth(java.util.Date date) {
return convertDateToCalendar(date).get(Calendar.MONTH) + 1;
}
public static String getTimeString(java.util.Date date)
{
String timeString = null;
if (getDayOfMonth(date)<10)
{
timeString = "0"+getDayOfMonth(date);
}
else
{
timeString = "" + getDayOfMonth(date);
}
if (getMonth(date)<10)
timeString = timeString + "0"+getMonth(date)+getYear(date);
else
timeString = timeString + getMonth(date)+getYear(date);
return timeString;
}
/**
*
* @param date
* @return
*/
public static int getDayOfMonth(java.util.Date date) {
return convertDateToCalendar(date).get(Calendar.DAY_OF_MONTH);
}
/**
*
* @param date
* @return
*/
public static int getHour(java.util.Date date) {
return convertDateToCalendar(date).get(Calendar.HOUR_OF_DAY);
}
/**
*
* @param date
* @return
*/
public static int getMinute(java.util.Date date) {
return convertDateToCalendar(date).get(Calendar.MINUTE);
}
/**
*
* @param date
* @return
*/
public static int getSecond(java.util.Date date) {
return convertDateToCalendar(date).get(Calendar.SECOND);
}
/**
*
* @param date
* @return
*/
public static int getMillisecond(java.util.Date date) {
return convertDateToCalendar(date).get(Calendar.MILLISECOND);
}
/**
*
* @param date
* @return
*/
public static long getMilliseconds(java.util.Date date) {
return date.getTime();
}
/**
*
* @param date
* @return
*/
public static int getDayOfWeek(java.util.Date date) {
return convertDateToCalendar(date).get(Calendar.DAY_OF_WEEK);
}
/**
*
* @param addpart
* @param times
* @param addnum
* @return
*/
public static Timestamp dateAdd(String addpart, Timestamp times, int addnum) {
Calendar cal = convertDateToCalendar(times);
if ("y".equals(addpart)) {
cal.add(Calendar.YEAR, addnum);
}
else if ("M".equals(addpart)) {
cal.add(Calendar.MONTH, addnum);
}
else if ("d".equals(addpart)) {
cal.add(Calendar.DATE, addnum);
}
else if ("H".equals(addpart)) {
cal.add(Calendar.HOUR, addnum);
}
else if ("m".equals(addpart)) {
cal.add(Calendar.MINUTE, addnum);
}
else if ("s".equals(addpart)) {
cal.add(Calendar.SECOND, addnum);
}
else {
return null;
}
return convertCalendarToTimes(cal);
}
/**
* check the year is leap Year or not.
* @param date
* @return
*/
public static boolean isLeapYear(java.util.Date date) {
return isLeapYear(getYear(date));
}
/**
* check the year is leap Year or not
* @param year
* @return
*/
public static boolean isLeapYear(int year) {
GregorianCalendar cal = new GregorianCalendar();
return cal.isLeapYear(year);
}
/**
* get the date , set the hour ,minute ,second is 0
* @param time
* @return
*/
public static Timestamp formatAsTimestamp(Date time) {
return new Timestamp(formatAsUtilDate(time).getTime());
}
/**
* get the date , set the hour ,minute ,second is 0
* @param dt
* @return
*/
public static java.util.Date formatAsUtilDate(java.util.Date date) {
Calendar cd = Calendar.getInstance();
cd.setTime(date);
cd.set(Calendar.HOUR_OF_DAY, 0);
cd.set(Calendar.MINUTE, 0);
cd.set(Calendar.SECOND, 0);
cd.set(Calendar.MILLISECOND, 0);
return cd.getTime();
}
/**
* get the date , set the hour ,minute ,second is 0
* @param date
* @return
*/
public static java.sql.Date formatAsSqlDate(java.sql.Date date) {
return new java.sql.Date(formatAsUtilDate(date).getTime());
}
/**
*
* @param java.util.Date:date
* @return java.sql.Date: YYYY-MM-DD:HH:00:00
*/
public static Date createPrevHourDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.set(getYear(date), getMonth(date)-1, getDayOfMonth(date), getHour(date)-1, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
public static Date createPrevDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.set(getYear(date), getMonth(date)-1, getDayOfMonth(date)-1, getHour(date), 0, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
//get the start and the end of the statTime
public static Date[] getDailyPeriod(Date statTime)
{
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
String strTime = df.format(statTime);
Date startTime = statTime;
try
{
startTime = df.parse(strTime);
}
catch (ParseException e)
{
logger.fatal(e);
}
Date endTime = new Date(startTime.getTime() + 24*3600*1000);
return new Date[]{startTime, endTime};
}
//get the start and the end of the week of the statTime
public static Date[] getWeeklyPeriod(Date statTime)
{
Calendar begin = Calendar.getInstance();
begin.setTime(statTime);
begin.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
begin.set(Calendar.HOUR_OF_DAY, 0);
begin.set(Calendar.MINUTE, 0);
begin.set(Calendar.SECOND, 0);
begin.set(Calendar.MILLISECOND, 0);
Calendar end = (Calendar)begin.clone();
end.add(Calendar.DATE, 7);
end.add(Calendar.MILLISECOND, -1);
return new Date[]{begin.getTime(), end.getTime()};
}
//get the start and the end of the month of the statTime
public static Date[] getMonthlyPeriod(Date statTime)
{
Calendar begin = Calendar.getInstance();
begin.setTime(statTime);
begin.set(Calendar.DATE, 1);
begin.set(Calendar.HOUR_OF_DAY, 0);
begin.set(Calendar.MINUTE, 0);
begin.set(Calendar.SECOND, 0);
begin.set(Calendar.MILLISECOND, 0);
Calendar end = (Calendar)begin.clone();
end.add(Calendar.MONTH, 1);
end.add(Calendar.MILLISECOND, -1);
return new Date[]{begin.getTime(), end.getTime()};
}
/**
* get the week days by the day
* the days is from the sunday to the saturday
* @param day
* @return
*/
private static Date[] getWeekDays(Calendar c)
{
int weekIndex = c.get(Calendar.DAY_OF_WEEK);
Date[] weekDay = new Date[7];
int count = 0;
for(int i=Calendar.SUNDAY; i<=Calendar.SATURDAY; i++)
{
weekDay[count++] = new GregorianCalendar(c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DATE)+i-weekIndex).getTime();
}
return weekDay;
}
//if the hour is "12-01-2008 09"
//then return the date like {12-01-2008 09:00:00, 12-01-2008 10:00:00}
public static Date[] getDailyTopTime(String hour)
{
Date begin = getStrToDate(hour+":00:00","MM-dd-yyyy HH:mm:ss");
Date end = new Date(begin.getTime() + 3600*1000);
return new Date[]{begin, end};
}
/**
* get the month days by the day
* the days is from the 1 to the last day
* @param Calendar
* @return
*/
private static Date[] getMonthDays(Calendar c)
{
cleanTime(c);
c.set(Calendar.DATE,1);
int firstDate = c.getActualMinimum(Calendar.DAY_OF_MONTH);
int lastDate = c.getActualMaximum(Calendar.DAY_OF_MONTH);
Date[] monthDay = new Date[lastDate - firstDate + 1];
for(int i=0; i<monthDay.length; i++)
{
monthDay[i]= new GregorianCalendar(c.get(Calendar.YEAR),c.get(Calendar.MONTH),firstDate + i).getTime();
}
return monthDay;
}
//if the hour is "12-01-2008 09"
//then return the date like {12-01-2008 09:00:00, 12-01-2008 10:00:00}
public static Date[] getDailyTime(String hour)
{
Date begin = getStrToDate(hour+":00:00","MM-dd-yyyy HH:mm:ss");
Date end = new Date(begin.getTime() + 3600*1000);
return new Date[]{begin, end};
}
//set the hour, minute, second, millisecond to 0
private static void cleanTime(Calendar c)
{
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
}
//get the first day of the Last month
private static Date getLastMonthStatTime(Date startTime)
{
Calendar c = Calendar.getInstance();
c.setTime(startTime);
c.set(Calendar.DATE, 1);
c.add(Calendar.MONTH, -1);
Date lastMonthStatTime = c.getTime();
return lastMonthStatTime;
}
//get the timeString like "00:00","00:15"-"23:45"
private static String getTime(int quarter)
{
if (quarter < 0 || quarter > 95)
{
return null;
}
if (quarter == 0)
{
return "00:00";
}
int hour = quarter / 4;
int minute = (quarter % 4) * 15;
return fill(hour, 2, "0") + ":" + fill(minute, 2, "0");
}
private static String fill(int value, int length, String fillChar)
{
String fillValue = String.valueOf(value);
int fillLength = length - fillValue.length();
for (int i = 0; i < fillLength; i++)
{
fillValue = fillChar + fillValue;
}
return fillValue;
}
}
分享到:
相关推荐
在Java编程语言中,`DateUtil`通常是一个自定义的工具类,用于处理与日期和时间相关的操作。这个工具类可以极大地简化日期处理任务,提供比Java内置的`java.util.Date`和`java.time`包更友好的API。在本文中,我们将...
标题中的"PyPI 官网下载 | types-python-dateutil-0.1.4.tar.gz"表明这是一个在Python Package Index(PyPI)上发布的软件包,名为`types-python-dateutil`,版本为0.1.4,其打包形式是tar.gz。PyPI是Python社区广泛...
Python的`dateutil`模块是Python编程中处理日期和时间的一个强大工具,尤其适用于解析不规则的日期字符串和处理各种时间间隔。在64位操作系统上使用时,它提供了与32位系统相同的功能,但可以处理更大的数据范围。本...
Python的`dateutil`模块是Python标准库`datetime`模块的一个强大扩展,它提供了一系列高级的时间解析和计算功能。在OpenERP7(现称为Odoo7)开发中,`dateutil`模块扮演了重要的角色,因为它能帮助开发者处理复杂的...
`DateUtil`类通常被设计为一个工具类,用于提供方便、高效和灵活的日期处理功能。这个类集成了多种方法,帮助开发者进行日期格式化、获取当前时间等操作。下面我们将深入探讨`DateUtil`类可能包含的一些核心知识点。...
DateUtil 日期工具类
在处理日期和时间以及解析复杂字符串时,Python提供了多个工具包,其中“dateUtil”和“pyparsing”是两个非常实用的库。 “dateUtil”库,全称为“python-dateutil”,是一个强大的扩展,用于处理日期和时间。它...
标题中的"numpy+matplotlib+six+dateutil+pytz+pyparsing.zip"是一个包含多个Python第三方库的压缩包,适用于Python 2.7版本,并且是针对Windows 7 64位系统的。这个压缩包里的每个子文件都是一个单独的安装程序,...
python源码安装包python-dateutil-2.8.0.tar,解压后 python setup.py install进行安装.
Java中的DateUtil时间工具类是开发者在处理日期和时间时常用的一个自定义工具类。它通常包含了一系列静态方法,用于简化Java内置的日期和时间API的使用,提高代码的可读性和可维护性。在实际开发中,由于Java 8之前...
dateutil模块
python-dateutil-2.2.win32-py2.7.exe,python-dateutil-2.2.win32-py2.7.exe
Python 项目开发实战 - dateutil 模块轻松计算日期 本文将详细介绍使用 dateutil 模块来简化日期计算的过程。日期计算是大部分系统都需要用到的,但它的计算比较复杂,因此很容易出现 Bug。开发高品质软件时要尽量...
在Java编程中,DateUtil是一个常见的工具类,用于处理日期和时间相关的操作。这个类通常包含了一系列静态方法,便于开发者进行日期格式化、日期比较、日期计算等常见任务。下面我们将详细探讨DateUtil中的关键知识点...
Python的`dateutil`库是处理日期和时间的利器,特别是其`parser`模块,能够灵活地解析各种格式的日期字符串。在版本2.5.0中,这个库提供了强大的功能,使得开发者在处理日期相关的任务时更加方便。在这个版本中,...
这里我们关注的是`DateUtil`工具类,它专门用于处理日期转换格式的操作。`DateUtil`工具类通常包含了对日期进行格式化、解析、比较等常用功能,使得在Android项目中处理日期变得更加便捷。 在Java中,日期对象主要...
资源名称:DateUtil 日期时间转换工具类 内容概要:日期时间转换工具类,包括基本的Date类型,String类型,TimeStamp类型,LocalDateTime类型,LocalDate类型之间的互相转换,还提供了许多与时间获取,时间计算有关...
在Python 3.4版本中,`matplotlib`依赖于两个关键库:`dateutil`和`numpy`。这两个库分别提供了高级日期和时间处理功能以及强大的数值计算能力。 `numpy`(Numerical Python)是Python科学计算的核心库,它引入了...
python_dateutil-2.8.0-py2.py3-none-any.whl
本文将深入探讨一个名为`DateUtil`的工具类,它为开发者提供了便利的方法来处理日期时间。`DateUtil`通常由开发者自定义,包含一系列静态方法,以简化日期时间操作。以下是对`DateUtil`类可能包含的一些核心功能的...