`
Alex007
  • 浏览: 10664 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Dete工具类

 
阅读更多

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
*
*
* @fileName DateUtil.java
* @author alex.tong
* @createDate 2010-11-1
*
* @modifyBy ...
* @modifyDate ...
* < ... Why & What is modified ...  >
*
* @version 1.0
*/
public class DateUtil {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

private static SimpleDateFormat sfdate = new SimpleDateFormat("yyyy0MM");
private static SimpleDateFormat sftime = new SimpleDateFormat("yyyyMMddHHmmss");
private static SimpleDateFormat sfmonth = new SimpleDateFormat("yyyyMMDD");


private static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static SimpleDateFormat sdfYearMonth = new SimpleDateFormat("yyyy-MM");
/**
* methodName : 取指定日期的月份所在的季度值,并转换为月份,如2009-05-15 返回 06
* param :
* @param date 指定的日期
* @param formatValue 指定的掩码
* @return
* DateUtil
* @author alex.tong
* create on: Jul 8, 2009 10:36:04 AM
*/
public static String getMonth(Date date){
String resultMonth = "";
if(null!=date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH); ;
int quarterMonth = ((int)Math.floor(month/3)) * 3 + 3;
resultMonth = String.valueOf(quarterMonth);
if(quarterMonth<10){
resultMonth = "0" + String.valueOf(quarterMonth);
}
}
return resultMonth;
}

/**
* methodName : 返回指定日期 加减某个天数的日期
* param :
* @param date 日期
* @param addDay 需要增减的天数
* @return
* @throws Exception
* DateUtil
* @author philip
* create on: Jul 8, 2009 9:56:39 AM
*/
public static Date addDay(Date date,int addDay){
if(null!=date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, addDay);
return calendar.getTime();
}
return null;
}

/**
* 获得给定(当前)年份月份的 delMonth 后 年份月份
* 创建人:alex.tong 
* 创建时间:Apr 13, 2010 11:13:06 AM
*/
public static String getYearMonth(String yearMonth,int delMonth){
Date date = new Date();
if(yearMonth != null){
try {
date = sdfYearMonth.parse(yearMonth) ;
} catch (ParseException e) {
date = new Date();
}
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, delMonth);
int month = calendar.get(Calendar.MONTH) + 1;
String monthStr = month + "";
if(month < 10){
monthStr = "0" + month;
}

return  calendar.get(Calendar.YEAR) + "-" + monthStr;
}


/**
* 以yyyy0MM格式返回传入日期
*
* @param date
* @return
*/
public static String formPeriod(Date date) {
if (date != null) {
return sfdate.format(date);
}
return null;
}

/**
* 以yyyyMMddHHmmss格式返回传入日期
*
* @param date
* @return
*/
public static String formTime(Date date) {
if (date != null) {
return sftime.format(date);
}
return null;
}

/**
* 以yyyyMMDD格式返回传入日期
*
* @param date
* @return
*/
public static String formMonth(Date date) {
if (date != null) {
return sfmonth.format(date);
}
return null;
}

public static Date formDate(String year) {

Calendar calendar = Calendar.getInstance();
calendar.set(Integer.parseInt(year), 1, 1);
return calendar.getTime();

}

/**
* 根据传入年、月参数返回日期类型(1日)
*
* @param year
* @param month
* @return
*/
public static Date formDate(int year, int month) {

Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
return calendar.getTime();

}

/**
* 根据传入年、月、日参数返回日期类型
*
* @param year
* @param month
* @param date
* @return
*/
public static Date formDate(int year, int month, int date) {

Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, date, 0, 0, 0);
return calendar.getTime();

}


public static boolean isNotInDate(Date begindate, Date enddate) {
Date date = new Date();
if(begindate.getTime() < date.getTime() &&  enddate.getTime() > date.getTime()){
return false;
}
return true;
}

/**
* 取指定日期所在月最后一天
* @param date
* @return
*/
public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        final int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DATE, lastDay);
        return withoutTime(calendar.getTime());
}

/**
* 取指定日期所在月第一天
* @param date
* @return
*/
public static Date getFirstDayOfMonth(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
final int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DATE,firstDay);
return withoutTime(calendar.getTime());
}

/**
* 取出日期中的时分秒
* @param date
* @returnFri Jul 16 15:54:32 CST 2010
*/
public static Date withoutTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return pareDate(year + "/" + month + "/" + day);
}

public static Date getYesterday() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DATE, -1);
return calendar.getTime();
}

/**
* 格式化为“yyyy-MM-dd”
*
* @param date
* @return
*/
public static String toFormatDate(Date date) {
if (null == date) {
return null;
}
return sdf.format(date);
}

/**
* 格式化为“yyyy-MM-dd”
*
* @param date
* @return
*/
public static String toFormatDateTime(Date date) {
if (null == date) {
return null;
}
return dateTimeFormat.format(date);
}


public static String getDateFormat(Date date , String format){
if(format == null){
format = "yyyy-MM-dd";
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.format(date);
}

/**
*
*<p>@author: alex.tong<p>
*<p>@createDate: Jan 7, 2010<p>
*<p>@method: getDate<p>
*<p>@param date
*<p>@param days
*<p>@return: <p>
*<p>@return: Date<p>
*<p>@TODO: 指定时间date,days天后的时间 <p>
*/
public static Date getDate(Date date, int days) {
long l  = 24 * 3600 * 1000L * days +  date.getTime();
return new Date(l);
}

/**
* 取日期的0点
* @param date
* @return
*/
public static Date getDateWithoutTime(){
return getDateWithoutTime(new Date());
}

public static Date getDateWithoutTime(Date date){
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
return date;
}

public static Date pareDate(String source) {
Date date = null;
try {
date = dateFormat.parse(source);
} catch (ParseException e1) {
try {
date = sdf.parse(source);
} catch (ParseException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
return date;
}

/**
* 取某年月最大天数
* @param year
* @param month
* @return
*/
public static int getDaysOfMonth(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}

public static int getDay(Date date) {
if(date == null) date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DATE);
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics