java 当天日期 dateutil_日期转换处理类 DateUtil.java

/****************************************************************************

Module: 日期转换处理

Version: 1.0

Author:  chenkb

Company: excel

Description:

*****************************************************************************/

package com.fedex.qilin.core.util;

import java.sql.Timestamp;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

/**

* 日期转换工具

*/

public class DateUtil

{

public static final String C_DATE_DIVISION = "-";

public static final String C_TIME_PATTON_DEFAULT = "yyyy-MM-dd HH:mm:ss";

public static final String C_DATE_PATTON_DEFAULT = "yyyy-MM-dd";

public static final String C_DATA_PATTON_YYYYMMDD = "yyyyMMdd";

public static final String C_TIME_PATTON_HHMMSS = "HH:mm:ss";

public static final int  C_ONE_SECOND = 1000;

public static final int  C_ONE_MINUTE = 60 * C_ONE_SECOND;

public static final int  C_ONE_HOUR   = 60 * C_ONE_MINUTE;

public static final long C_ONE_DAY    = 24 * C_ONE_HOUR;

/**

* Return the current date

*

* @return - DATE

*/

public static Date getCurrentDate()

{

Calendar cal = Calendar.getInstance();

Date currDate = cal.getTime();

return currDate;

}

/**

* Return the current date string

*

* @return - 产生的日期字符串

*/

public static String getCurrentDateStr()

{

Calendar cal = Calendar.getInstance();

Date currDate = cal.getTime();

return format(currDate);

}

/**

* Return the current date in the specified format

*

* @param strFormat

* @return

*/

public static String getCurrentDateStr(String strFormat)

{

Calendar cal = Calendar.getInstance();

Date currDate = cal.getTime();

return format(currDate, strFormat);

}

/**

* Parse a string and return a date value

*

* @param dateValue

* @return

* @throws Exception

*/

public static Date parseDate(String dateValue)

{

return parseDate(C_DATE_PATTON_DEFAULT, dateValue);

}

/**

* Parse a strign and return a datetime value

*

* @param dateValue

* @return

*/

public static Date parseDateTime(String dateValue)

{

return parseDate(C_TIME_PATTON_DEFAULT, dateValue);

}

/**

* Parse a string and return the date value in the specified format

*

* @param strFormat

* @param dateValue

* @return

* @throws ParseException

* @throws Exception

*/

public static Date parseDate(String strFormat, String dateValue)

{

if (dateValue == null)

return null;

if (strFormat == null)

strFormat = C_TIME_PATTON_DEFAULT;

SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);

Date newDate = null;

try

{

newDate = dateFormat.parse(dateValue);

}

catch (ParseException pe)

{

newDate = null;

}

return newDate;

}

/**

* 将Timestamp类型的日期转换为系统参数定义的格式的字符串。

* @param aTs_Datetime 需要转换的日期。

* @return 转换后符合给定格式的日期字符串

*/

public static String format(Date aTs_Datetime)

{

return format(aTs_Datetime, C_DATE_PATTON_DEFAULT);

}

/**

* 将Timestamp类型的日期转换为系统参数定义的格式的字符串。

* @param aTs_Datetime 需要转换的日期。

* @return 转换后符合给定格式的日期字符串

*/

public static String formatTime(Date aTs_Datetime)

{

return format(aTs_Datetime, C_TIME_PATTON_DEFAULT);

}

/**

* 将Date类型的日期转换为系统参数定义的格式的字符串。

* @param aTs_Datetime

* @param as_Pattern

* @return

*/

public static String format(Date aTs_Datetime, String as_Pattern)

{

if (aTs_Datetime == null || as_Pattern == null)

return null;

SimpleDateFormat dateFromat = new SimpleDateFormat();

dateFromat.applyPattern(as_Pattern);

return dateFromat.format(aTs_Datetime);

}

/**

* @param aTs_Datetime

* @param as_Format

* @return

*/

public static String formatTime(Date aTs_Datetime, String as_Format)

{

if (aTs_Datetime == null || as_Format == null)

return null;

SimpleDateFormat dateFromat = new SimpleDateFormat();

dateFromat.applyPattern(as_Format);

return dateFromat.format(aTs_Datetime);

}

public static String getFormatTime(Date dateTime)

{

return formatTime(dateTime, C_TIME_PATTON_HHMMSS);

}

/**

* @param aTs_Datetime

* @param as_Pattern

* @return

*/

public static String format(Timestamp aTs_Datetime, String as_Pattern)

{

if (aTs_Datetime == null || as_Pattern == null)

return null;

SimpleDateFormat dateFromat = new SimpleDateFormat();

dateFromat.applyPattern(as_Pattern);

return dateFromat.format(aTs_Datetime);

}

/**

* 取得指定日期N天后的日期

* @param date

* @param days

* @return

*/

public static Date addDays(Date date, int days)

{

Calendar cal = Calendar.getInstance();

cal.setTime(date);

cal.add(Calendar.DAY_OF_MONTH, days);

return cal.getTime();

}

/**

* 计算两个日期之间相差的天数

* @param date1

* @param date2

* @return

*/

public static int daysBetween(Date date1,Date date2)

{

Calendar cal = Calendar.getInstance();

cal.setTime(date1);

long time1 = cal.getTimeInMillis();

cal.setTime(date2);

long time2 = cal.getTimeInMillis();

long between_days=(time2-time1)/(1000*3600*24);

return Integer.parseInt(String.valueOf(between_days));

}

/**

* 计算当前日期相对于"1977-12-01"的天数

* @param date

* @return

*/

public static long getRelativeDays(Date date)

{

Date relativeDate = DateUtil.parseDate("yyyy-MM-dd","1977-12-01");

return DateUtil.daysBetween(relativeDate,date);

}

public static Date getDateBeforTwelveMonth()

{

String date = "";

Calendar cla = Calendar.getInstance();

cla.setTime(getCurrentDate());

int year = cla.get(Calendar.YEAR) - 1;

int month = cla.get(Calendar.MONTH) + 1 ;

if (month >9 )

{

date = String.valueOf(year) + C_DATE_DIVISION + String.valueOf(month)

+ C_DATE_DIVISION + "01";

}

else

{

date = String.valueOf(year) + C_DATE_DIVISION + "0" + String.valueOf(month)

+ C_DATE_DIVISION + "01" ;

}

Date dateBefore = parseDate(date);

return dateBefore;

}

/**

* 传入时间字符串,加一天后返回Date

* @param date 时间 格式 YYYY-MM-DD

* @return

*/

public static Date addDate(String date)

{

if (date == null)

{

return null;

}

Date tempDate = parseDate(C_DATE_PATTON_DEFAULT, date);

String year = format(tempDate, "yyyy");

String month = format(tempDate, "MM");

String day = format(tempDate, "dd");

GregorianCalendar calendar =

new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(month)-1, Integer.parseInt(day));

calendar.add(GregorianCalendar.DATE, 1);

return calendar.getTime();

}

public static void main(String[] args)

{

//  Date date1 = DateUtil.addDays(DateUtil.getCurrentDate(),1);

//  Date date2 = DateUtil.addDays(DateUtil.getCurrentDate(),101);

//

//  System.out.println(DateUtil.getRelativeDays(date1));

//  System.out.println(DateUtil.getRelativeDays(date2));

//     Timestamp date = new Timestamp(801);

//

//        System.out.println(date);

//        String strDate = DateUtil.format(date, C_DATA_PATTON_YYYYMMDD);

//

//        System.out.println(strDate);

String date = "2006-07-31";        System.out.println(date);        Date date2 = addDate(date);        System.out.println(date2);         }}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值