DateUtil

来源:互联网 发布:剑三丐太捏脸数据淘宝 编辑:程序博客网 时间:2024/05/12 00:12

package com.css.common.util;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 用于处理JAVA中的DATE型数据的转换
 *
 * @version 1.0
 */
public class DateUtil {
 
 private static Log log = LogFactory.getLog(DateUtil.class);

 private static String datePattern = "yyyy-MM-dd";

 private static String timePattern = "HH:mm:ss";
 
 /**
  * Return 缺省的日期格式 (yyyy-MM-dd)
  *
  * @return 在页面中显示的日期格式
  */
 public static String getDatePattern() {
  return datePattern;
 }

 /**
  * Return 缺省的时间格式 (HH:mm:ss)
  *
  * @return 在页面中显示的日期格式
  */
 public static final String getTimePattern() {
  return timePattern;
 }
 
 /**
  * Return 缺省的日期+时间格式 (yyyy-MM-dd HH:mm:ss)
  *
  * @return 在页面中显示的日期格式
  */
 public static final String getDateTimePattern() {
  return datePattern + " " +timePattern;
 }


 /**
  * 按照日期格式,将字符串解析为日期对象
  *
  * @param aMask
  *            输入字符串的格式
  * @param strDate
  *            按aMask格式排列的日期的字符串描述
  * @return Date 对象
  *
  * @throws ParseException
  */
 public static final Date convertStringToDate(String aMask, String strDate){
  SimpleDateFormat df = null;
  Date chgDate = null;
  try {
   df = new SimpleDateFormat(aMask);
   chgDate = df.parse(strDate);
  } catch (ParseException pe) {
   log.error("from string convert to date is error :"+pe.getMessage());
  }
  return chgDate;
 }
 
 /**
  * 按照日期格式,将字符串解析为日期对象.格式'yyyy-mm-dd HH:mm:ss'
  *
  * @param strDate
  *            按aMask格式排列的日期的字符串描述
  * @return Date 对象
  */
 public static final Date convertStringToDate(String strDate){
  return convertStringToDate(getDateTimePattern(),strDate);
 }
 
 /**
  * 按照日期格式,将日期对象解析为字符串
  *
  * @param aMask
  *            输入字符串的格式
  * @param aDate
  *            按aMask格式排列的日期对象
  * @return String 字符串
  *
  * @throws ParseException
  */
 public static String convertDateToString(String aMask, Date aDate){
  SimpleDateFormat df = null;
  try {
   df = new SimpleDateFormat(aMask);
  }catch(Exception ex){
   log.error("from date convert to string is error :"+ex.getMessage());
  }
  return df.format(aDate);
 }
 
 /**
  * 按照日期格式,将日期对象解析为字符串.格式'yyyy-mm-dd HH:mm:ss'
  *
  * @param strDate
  *            按aMask格式排列的日期的字符串描述
  * @return Date 对象
  */
 public static final String convertDateToString(Date aDate){
  return convertDateToString(getDateTimePattern(),aDate);
 }

 /**
  * 返回当前日期
  *
  * @param aMask 日期格式
  *
  * @return
  */
 public static String getLocalDateTime(String aMask) {
  SimpleDateFormat df = new SimpleDateFormat(aMask);
  String returnValue = df.format(new Date());
  return returnValue;
 }
 
 
 /**
  * 返回当前日期,格式'yyyy-mm-dd'
  *
  * @return
  */
 public static String getLocalDate() {
  return getLocalDateTime(getDatePattern());
 }
 
 /**
  * 返回当前时间,格式'yyyy-mm-dd HH:mm:ss'
  *
  * @return
  */
 public static final String getLocalDateTime() {
  return getLocalDateTime(getDateTimePattern());
 }
 
 /**
  * 返回当前日期
  *
  * @param aMask 日期格式
  *
  * @return
  */
 public static Date loadLocalDateTime(String aMask) {
  return convertStringToDate(aMask, getLocalDateTime(aMask));
 }
 
 /**
  * 返回当前日期,格式'yyyy-mm-dd'
  *
  * @return
  */
 public static Date loadLocalDate() {
  return loadLocalDateTime(getDatePattern());
 }
 
 /**
  * 返回当前时间,格式'yyyy-mm-dd HH:mm:ss'
  *
  * @return
  */
 public static final Date loadLocalDateTime() {
  return loadLocalDateTime(getDateTimePattern());
 }


 /**
  * 得到日历对象Calendar,格式:yyyy-MM-dd
  *
  * @return the current date
  * @throws ParseException
  */
 public static Calendar getCalendar() throws ParseException {
  Date today = new Date();
  SimpleDateFormat df = new SimpleDateFormat(datePattern);
  String todayAsString = df.format(today);
  Calendar cal = new GregorianCalendar();
  cal.setTime(convertStringToDate(getDatePattern(),todayAsString));
  return cal;
 }
 
 public static String getYear() {
  Date date = new Date();
  return convertDateToString("yyyy", date);
 }

 public static String getMonth() {
  Date date = new Date();
  return convertDateToString("MM", date);
 }

 public static String getDay() {
  Date date = new Date();
  return convertDateToString("dd", date);
 }

 /**
  * 日期增大
  * @param date
  *            Date
  * @param field
  *            int 1年 2月 3周 5天
  * @param amount
  *            int 增加数量
  * @return Date
  */
 public static Date addDate(Date date, int field, int amount) {
  GregorianCalendar gc = new GregorianCalendar();
  gc.setTime(date);
  gc.add(field, amount);
  return gc.getTime();
 }

 /**
  * 得到Calendar的TimeInMilli
  *
  * @param date
  * @return
  */
 public static long getMillis(Date date) {
  if (date != null) {
   Calendar c = Calendar.getInstance();
   c.setTime(date);
   return c.getTimeInMillis();
  } else {
   return 0;
  }
 }

 /**
  * 日期相减
  *
  * @param date
  *            Date
  * @param day
  *            int
  * @return Date
  */
 public static Date diffDate(Date date, int day) {
  if (date != null) {
   Calendar c = Calendar.getInstance();
   long iLong = getMillis(date) - ((long) day) * 24 * 3600 * 1000;
   c.setTimeInMillis(iLong);
   return c.getTime();
  } else {
   return new Date();
  }
 }

 /**
  * 得到一年当中的星期
  *
  * @param date
  * @return
  */
 public static String getWeekOfYearByString(Date date) {
  if (date != null) {
   Calendar c = new GregorianCalendar();
   c.setFirstDayOfWeek(Calendar.MONDAY);
   c.setMinimalDaysInFirstWeek(7);
   c.setTime(date);
   int week = c.get(Calendar.WEEK_OF_YEAR);
   if (week > 9) {
    return convertDateToString("yyyy", date) + week;
   } else {
    return convertDateToString("yyyy", date) + "0" + week;
   }
  } else {
   return null;
  }
 }

 /**
  * 获取某月的第一天,或最后一天
  *
  * @param date
  *            Date
  * @param flag
  *            boolean true为第一天 false为最后一天
  * @return String
  */
 public static final String getDateByMonth(Date date, boolean flag) {
  Calendar cal = Calendar.getInstance();
  cal.setTime(date);
  int i = 0;
  if (flag) {
   i = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
  } else {
   i = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
  }
  if (i > 9) {
   return i + "";
  } else {
   return "0" + i;
  }
 }

 /**
  * 获取某周的第一天,或最后一天
  *
  * @param year
  *            int
  * @param week
  *            int
  * @param flag
  *            boolean true为第一天 false为最后一天
  * @return Date
  */
 public static Date getDateByWeek(int year, int week, boolean flag) {
  Calendar calendar = Calendar.getInstance();
  calendar.set(Calendar.YEAR, year);
  calendar.set(Calendar.WEEK_OF_YEAR, week);
  calendar.set(Calendar.DAY_OF_WEEK, 7);
  // 得到今天是星期几,星期日为1
  int datInWeek = calendar.get(Calendar.DAY_OF_WEEK);
  System.out.println(datInWeek);
  // Clone一个新的
  Calendar calendar1 = (Calendar) calendar.clone();
  if (flag) {
   calendar.add(Calendar.DAY_OF_MONTH, -(datInWeek - 2));
   return calendar.getTime();
  } else {
   calendar1.add(Calendar.DAY_OF_MONTH, 7 - datInWeek);
   return addDate(calendar.getTime(), 5, 1);
  }
 }
 
 /**
  * 日期计算
  *
  * @param date
  *            起始日期
  * @param yearNum
  *            年增减数
  * @param monthNum
  *            月增减数
  * @param dateNum
  *            日增减数
  */
 public static String calDate(String date, int yearNum, int monthNum,
   int dateNum) {
  String result = "";
  try {
   SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
   Calendar cal = Calendar.getInstance();
   cal.setTime(sd.parse(date));
   cal.add(Calendar.MONTH, monthNum);
   cal.add(Calendar.YEAR, yearNum);
   cal.add(Calendar.DATE, dateNum);
   result = sd.format(cal.getTime());
  } catch (Exception e) {
   e.printStackTrace();
  }
  return result;
 }
 
 /**
  * 得到系统时间数字:1260168985734
  * @return
  */
 public static long getLongTime(){
  return  System.currentTimeMillis();
 }
 
 /**
  * 得到系统时间数字:1260168985734
  * @return
  */
 public static String getLongDate(){
  long d = System.currentTimeMillis();
  return String.valueOf(d);
 }
 
 public static void main(String[] args) {
  
 }
}

原创粉丝点击