java日期工具类

package com.djzh.common.utils;

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

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;


/**
 * 日期实用工具类
 * 
 */
public class DateUtil {

    public static final Logger log = Logger.getLogger(DateUtil.class);
    
    /**
     * 返回当前日期
     * 
     * @return String
     */
    public static String getDate() {
        return DateFormatUtils.format(new Date(), "yyyy-MM-dd");
    }

    /**
     * 返回日期yyyy-MM-dd
     * 
     * @return String
     */
    public static String getDate(Date date) {
        return DateFormatUtils.format(date, "yyyy-MM-dd");
    }

    /**
     * 返回当前日期时间
     * 
     * @return String
     */
    public static String getDateTime() {
        return DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 返回当前日期时间
     * 
     * @return String
     */
    public static String getDateRoundMinute() {
        return DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm");
    }
    /**
     * 获取日期200-10-5
     * 
     * @param str
     *            原日期类型yyyy-MM-dd HH:mm:ss.S
     * @return
     */
    public static String getDateTime(String str) {
        if (str == null || str.equals(""))
            return "";
        DateFormat df = DateFormat.getDateInstance();
        Date d = null;
        try {
            d = df.parse(str);
        } catch (Exception e) {
            return null;
        }
        return DateFormatUtils.format(d, "yyyy-MM-dd");
    }

    /**
     * 返回当前时间
     * 
     * @return String
     */
    public static String getTime() {
        return DateFormatUtils.format(new Date(), "HHmmssSSS");
    }

    /**
     * 返回当前时间
     * 
     * @return String
     */
    public static String getCurrentTime() {
        return DateFormatUtils.format(new Date(), "HH:mm:ss");
    }

    /**
     * 获取月的实现
     * 
     * @param date
     * @return String
     */
    public static String getEndTime(Date date, int month, boolean endmonth) {
        String ds = null;
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, month);
        if (endmonth) {
            int i = c.getActualMaximum(Calendar.DAY_OF_MONTH);
            c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), i, 23, 59, 59);
        }
        ds = DateFormatUtils.format(c.getTime(), "yyyy-MM-dd HH:mm:ss");
        return ds;

    }

    /**
     * 获取天的时限
     * 
     * @param date
     * @return String
     */
    public static String getEndTime(Date date, int days) {
        String ds = null;
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        c.add(Calendar.DATE, days);
        ds = DateFormatUtils.format(c.getTime(), "yyyy-MM-dd HH:mm:ss");
        return ds;
    }

    /**
     * 返回中文日期
     * 
     * @return String
     */
    public static String getGBDate(String dateStr) {
        DateFormat df = DateFormat.getDateInstance();
        String gbdate = null;
        try {
            Date date = df.parse(dateStr);
            gbdate = DateFormatUtils.format(date, "yyyy年MM月dd日");
        } catch (ParseException pe) {
            log.info(pe.getMessage());
        }
        return gbdate;
    }

    /**
     * 返回日期
     * 
     * @return String
     */
    public static String getStringDate(String dateStr, String pattern) {
        DateFormat df = DateFormat.getDateInstance();
        String gbdate = null;
        try {
            Date date = df.parse(dateStr);
            gbdate = DateFormatUtils.format(date, pattern);
        } catch (ParseException pe) {
            log.info(pe.getMessage());
        }
        return gbdate;
    }

    /**
     * 返回中文日期时间
     * 
     * @return String
     */
    public static String getGBDateTime(String dateStr) {
        DateFormat df = DateFormat.getDateInstance();
        String gbdate = null;
        try {
            Date date = df.parse(dateStr);
            gbdate = DateFormatUtils.format(date, "yyyy年MM月dd日 HH时mm分ss秒");
        } catch (ParseException pe) {
            log.info(pe.getMessage());
        }
        return gbdate;
    }

    /**
     * 返回日期格式
     * 
     * @return Date
     */
    public static Date getDate(String dateStr) {
        DateFormat df = DateFormat.getDateInstance();
        try {
            Date date = df.parse(dateStr);
            return date;
        } catch (Exception e) {
        }
        return null;
    }

    public static String getShortDate(String dateStr) {
        if (dateStr == null || "".equals(dateStr))
            return null;
        DateFormat df = DateFormat.getDateInstance();
        try {
            Date date = df.parse(dateStr);
            return DateFormatUtils.format(date, "yyyy-MM-dd");
        } catch (Exception e) {
            log.error("日期格式有误" + e.getMessage());
        }
        return null;
    }

    public static Date getDate(String dateStr, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            Date date = sdf.parse(dateStr);
            return date;
        } catch (Exception e) {
        
        }
        return null;
    }

    /**
     * 返回日期格式
     * 
     * @return String
     */
    public static String getDateStr(Date date) {
        String dateStr = null;
        try {
            dateStr = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss.S");
            return dateStr;
        } catch (Exception e) {
            
        }
        return null;
    }
    
    /**
     * 返回日期格式
     * 
     * @return String
     */
    public static String getDateStr(Date date, String pattern) {
        String dateStr = null;
        try {
            dateStr = DateFormatUtils.format(date, pattern);
            return dateStr;
        } catch (Exception e) {
            
        }
        return "";
    }

    /**
     * 返回时间类型
     * 
     * @param aValue
     * @return
     */
    public static java.sql.Timestamp parseTimestampFromFormats(String aValue) {
        if (StringUtils.isEmpty(aValue))
            return null;
        DateFormat df = DateFormat.getDateTimeInstance();
        if (aValue.indexOf(":") == -1) {
            aValue = " 00:00:00";
        } else if (aValue.indexOf(":") != -1
                && aValue.indexOf(":") == aValue.lastIndexOf(":")) {
            aValue = aValue + ":00";
        }
        try {
            Date date = df.parse(aValue);
            return new java.sql.Timestamp(date.getTime());
        } catch (Exception e) {
        
        }

        return null;
    }

    public static java.sql.Timestamp now() {
        return new java.sql.Timestamp(new java.util.Date().getTime());
    }

    /**
     * 获取服务器的系统时间
     * 
     * @return Date 日期 格式:yyyy-MM-dd HH:mm:ss
     */
    public static String getCurrentServerTime() {
        return getDateTime();
    }

    /**
     * 把时间格式转换成到分钟
     * 
     * @param String
     *            日期 格式:yyyy-MM-dd HH:mm:ss
     * @return String 日期 格式:yyyy-MM-dd HH:mm
     */
    public static String getDateTimeStringToMinute(String date) {
        if (date == null || date.equals(""))
            return "";
        try {
            String dateStr = DateFormatUtils.format(DateFormat
                    .getDateTimeInstance().parse(date), "yyyy-MM-dd HH:mm");
            return dateStr;
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获取时间到秒
     * 
     * @param date
     *            日期
     * @return
     */
    public static String getDateStrToSecond(Date date) {
        if (date == null)
            return "";
        String dateStr = null;
        try {
            dateStr = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
            return dateStr;
        } catch (Exception e) {
            log.info(e.getMessage());
            return null;
        }
    }

    /**
     * 获取时间到分
     * 
     * @param date
     *            日期
     * @return
     */
    public static String getDateStrToMinute(Date date) {
        if (date == null)
            return "";
        String dateStr = null;
        try {
            dateStr = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm");
            return dateStr;
        } catch (Exception e) {

            return "";
        }
    }

    /**
     * 获取时间到日
     * 
     * @param date
     *            日期
     * @return
     */
    public static String getDateStrToDay(Date date) {
        if (date == null)
            return "";
        String dateStr = null;
        try {
            dateStr = DateFormatUtils.format(date, "yyyy-MM-dd");
            return dateStr;
        } catch (Exception e) {
            
            log.info(e.getMessage());
            return null;
        }
    }

    public static Date getDateFromString(String dateStr) throws Exception {
        DateFormat df = DateFormat.getDateInstance();
        Date date = df.parse(dateStr);
        return date;

    }

    public static Date getDateTimeFromString(String dateStr) throws Exception {
        DateFormat df = DateFormat.getDateTimeInstance();
        Date date = df.parse(dateStr);
        return date;
    }

//    /**
//     * 检查文本框中的日期时间格式
//     * 
//     * @param source
//     *            Text
//     * 
//     * @return
//     */
//    public static boolean checkDateTime(Text text) {
//
//        if (text.getText() != null && text.getText().trim().length() > 0) {
//            String str = text.getText().trim();
//            Date date = null;
//            try {
//                date = changeDate(str);
//                String sdate = DateUtil.getDateStrToMinute(date);
//                text.setText(sdate);
//            } catch (Exception e1) {
//                MessageDialog.showMessage("时间格式错误,请重新输入,正确格式为:2006-06-06 6:6");
//            
//                text.setText("");
//                
//                return false;
//            }
//        }
//        return true;
//    }
//    /**
//     * 检查文本框中的日期时间格式
//     * 
//     * @param source
//     *            Text
//     * 
//     * @return
//     */
//    public static boolean checkDateTime(WText text) {
//
//        if (text.getText() != null && text.getText().trim().length() > 0) {
//            String str = text.getText().trim();
//            Date date = null;
//            try {
//                date = changeDate(str);
//                String sdate = DateUtil.getDateStrToMinute(date);
//                text.setText(sdate);
//            } catch (Exception e1) {
//                MessageDialog.showMessage("时间格式错误,请重新输入,正确格式为:2006-06-06 6:6");
//            
//                text.setText("");
//                
//                return false;
//            }
//        }
//        return true;
//    }
//    /**
//     * 检查文本框中的日期时间格式
//     * 
//     * @param source
//     *            StyledText
//     * 
//     * @return
//     */
//    public static boolean checkDateTime(StyledText text) {
//
//        if (text.getText() != null && text.getText().trim().length() > 0) {
//            String str = text.getText().trim();
//            Date date = null;
//            try {
//                date = changeDate(str);
//                String sdate = DateUtil.getDateStrToMinute(date);
//                text.setText(sdate);
//            } catch (Exception e1) {
//                MessageDialog.showMessage("时间格式错误,请重新输入,正确格式为:2006-06-06 6:6");
//    
//                text.setText("");
//                return false;
//            }
//        }
//        return true;
//
//    }
//
//    /**
//     * 检查文本框中的日期格式
//     * 
//     * @param source
//     *            Text
//     * 
//     * @return
//     */
//    public static boolean checkDate(Text text) {
//
//        if (text.getText() != null && text.getText().trim().length() > 0) {
//            String str = text.getText().trim();
//            Date date = null;
//            try {
//                date = changeDate(str);
//                String sdate = DateUtil.getDateStrToDay(date);
//                text.setText(sdate);
//            } catch (Exception e1) {
//                MessageDialog.showMessage("时间格式错误,请重新输入,正确格式为:2006-06-06");
//    
//                text.setText("");
//                return false;
//            }
//        }
//        return true;
//    }
//    /**
//     * 检查文本框中的日期格式
//     * 
//     * @param source
//     *            Text
//     * 
//     * @return
//     */
//    public static boolean checkDate(WText text) {
//
//        if (text.getText() != null && text.getText().trim().length() > 0) {
//            String str = text.getText().trim();
//            Date date = null;
//            try {
//                date = changeDate(str);
//                String sdate = DateUtil.getDateStrToDay(date);
//                text.setText(sdate);
//            } catch (Exception e1) {
//                MessageDialog.showMessage("时间格式错误,请重新输入,正确格式为:2006-06-06");
//        
//                text.setText("");
//                return false;
//            }
//        }
//        return true;
//    }
//    /**
//     * 检查日期输入
//     * @param text
//     * @return
//     */
//    public static boolean checkDateInput(Text text) {
//
//        if (text.getText() != null && text.getText().trim().length() > 0) {
//            String str = text.getText().trim();
//            Date date = null;
//            try {
//                date = changeDate(str);
//                String sdate = DateUtil.getDateStrToDay(date);
//                text.setText(sdate);
//            } catch (Exception e1) {
//                text.setText("");
//                text.setFocus();
//                MessageDialog.showMessage("时间格式错误,请重新输入,正确格式为:2006-06-06");
//                return false;
//            }
//        }
//        return true;
//    }
//
//    /**
//     * 检查文本框中的日期格式
//     * 
//     * @param source
//     *            StyledText
//     * 
//     * @return
//     */
//    public static void checkDate(StyledText source) {
//
//        source.addFocusListener(new FocusAdapter() {
//
//            public void focusLost(FocusEvent e) {
//                StyledText text = (StyledText) e.getSource();
//                if (text.getText() != null
//                        && text.getText().trim().length() > 0) {
//                    String str = text.getText().trim();
//                    Date date = null;
//                    try {
//                        date = changeDate(str);
//                        String sdate = DateUtil.getDateStrToDay(date);
//                        text.setText(sdate);
//                    } catch (Exception e1) {
//                        MessageDialog
//                                .showMessage("时间格式错误,请重新输入,正确格式为:2006-06-06");
//                        
//                        text.setText("");
//                    }
//                }
//            }
//        });
//    }

    private static Date changeDate(String str) throws Exception {
        Date date = null;
        if (str.indexOf(" ") >= 0) {
            String[] strArray = str.split(":");
            if (strArray != null) {
                if (strArray.length == 2) {
                    str = str + ":00";
                }
            }
            date = DateUtil.getDateTimeFromString(str);

        } else {

            date = DateUtil.getDateFromString(str);

        }

        return date;
    }

    /**
     * 获取限时急件时间
     * 
     * @throws ParseException
     */
    public static String getLimitDispatchTime() {
        try {
            String dateTime = DateUtil.getCurrentServerTime();
            DateFormat datef = DateFormat.getDateTimeInstance();

            SimpleDateFormat m_DateFormatter = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm");
            Date sDate = datef.parse(dateTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(sDate);
            cal.add(Calendar.HOUR, 2);
            Date eDate = cal.getTime();
            String str = m_DateFormatter.format(eDate);

            return str;
        } catch (Exception e) {
            log.info(e.getMessage());
            return null;
        }

    }

    /**
     * 获取特急件时间
     * 
     * @throws ParseException
     */

    public static String getEspecialDispatchTime() {
        try {
            String dateTime = DateUtil.getCurrentServerTime();
            DateFormat datef = DateFormat.getDateTimeInstance();

            SimpleDateFormat m_DateFormatter = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm");
            Date sDate = datef.parse(dateTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(sDate);
            cal.add(Calendar.HOUR, 8);
            Date eDate = cal.getTime();
            String str = m_DateFormatter.format(eDate);

            return str;
        } catch (Exception e) {
            log.info(e.getMessage());
            return null;
        }

    }

    // 获取时间到分
    public static String getTimeMinute() {
        try {
            String dateTime = DateUtil.getCurrentServerTime();
            DateFormat datef = DateFormat.getDateTimeInstance();

            SimpleDateFormat m_DateFormatter = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm");
            Date sDate = datef.parse(dateTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(sDate);
            Date eDate = cal.getTime();
            String str = m_DateFormatter.format(eDate);

            return str;
        } catch (Exception e) {
            log.info(e.getMessage());
            return null;
        }

    }
    
            

    public static boolean isYesterdayORtoday(String newsCreatTime)
    {      
        try
        {
            String today = DateUtil.getDate();
            newsCreatTime = DateUtil.getDateTime(newsCreatTime);
            String yesterday = getYesterday(newsCreatTime);
            if(today.equals(newsCreatTime) || today.equals(yesterday)){
                return true;
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return false;  
    }
    
    /**
     * 获取上一天的信息
     * @param nowDate format:2006-06-17
     * @return
     */
    public static String getYesterday(String nowDate)
    {
        String yesterday = "";
        int year = 0;
        int month = 0;
        int day = 0;
        try
        {
            year = Integer.parseInt(nowDate.substring(0,nowDate.indexOf("-")));
            month = Integer.parseInt(nowDate.substring(nowDate.indexOf("-")+1,nowDate.lastIndexOf("-")));
            day = Integer.parseInt(nowDate.substring(nowDate.lastIndexOf("-")+1));
            
            
            day = day +1;
            if(day == 0)
            {
                month = month - 1;
                if(month == 0)
                {
                    //January
                    month = 12;
                    day = 31;
                    year = year - 1;
                }
                else
                {
                    //not Jan.
                    switch(month)
                    {
                    //1|3|5|7|8|10|12) day=31;;
                    case 1:
                        day = 31;
                        break;
                    case 3:
                        day = 31;
                        break;
                    case 5:
                        day = 31;
                        break;
                    case 7:
                        day = 31;
                        break;
                    case 8:
                        day = 31;
                        break;
                    case 10:
                        day = 31;
                        break;
                    case 12:
                        day = 31;
                        break;
                        //4|6|9|11) day=30;;
                    case 4:
                        day = 30;
                        break;
                    case 6:
                        day = 30;
                        break;
                    case 9:
                        day = 30;
                        break;
                    case 11:
                        day = 30;
                        break;
                    case 2:
                        if(year % 4 ==0 && year % 100 !=0 || year % 400 ==0) 
                        {
                            //leap year
                            day = 29;
                        }
                        else day = 28;
                    }
                }
            }
                     
            String monthStr = "";
            String dayStr = "";
                     
            if(month < 10)
            {
                monthStr = "0" + String.valueOf(month);
            }
            else
            {
                monthStr = String.valueOf(month);
            }
               
            if(day < 10)
            {
                dayStr = "0" + String.valueOf(day);
            }
            else
            {
                dayStr = String.valueOf(day);
            }
                     
            yesterday = String.valueOf(year) + "-" + monthStr + "-" + dayStr;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return yesterday;
    }
    
    public static int getLastDateOfMonth(Date date){
        int lastDate = 0;
        Calendar cal = Calendar.getInstance();
        cal.set(date.getYear(), date.getMonth()+1,-1);
        lastDate = cal.get(cal.DATE);
        return lastDate+1;
    }
    
    
    public static Date parseStringToDate(String usedate,String hour,String minute){
        Date date=null;
        SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            date = sd.parse(usedate+" "+hour+":"+minute);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }
    public static void main(String[] args) {
        System.out.println(DateUtil.getLastDateOfMonth(new Date()));
    }
    
    
    public static String parseStringToDate(String dateStr) {
         Date date = null;
        SimpleDateFormat sf2 = null;
        try {
            SimpleDateFormat sf1 = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.ENGLISH); 
                date = sf1.parse(dateStr);
                sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
            return sf2.format(date);
           // System.out.println(sf2.format(date));
    }
    
    public static String parseStringToDate2(String s){
        Date date = null;
        SimpleDateFormat sf2 = null;
        try {
            SimpleDateFormat sf1 = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH); 
            date = sf1.parse(s);
            sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sf2.format(date);
    }
    
    public static String parseChinaDateToDate(String s) {
        String[] strArr=s.split("月");
        if (strArr[0].equals("一")) {
            strArr[0]="01";
        }else if (strArr[0].equals("二")) {
            strArr[0]="02";
        }else if (strArr[0].equals("三")) {
            strArr[0]="03";
        }else if (strArr[0].equals("四")) {
            strArr[0]="04";
        }else if (strArr[0].equals("五")) {
            strArr[0]="05";
        }else if (strArr[0].equals("六")) {
            strArr[0]="06";
        }else if (strArr[0].equals("七")) {
            strArr[0]="07";
        }else if (strArr[0].equals("八")) {
            strArr[0]="08";
        }else if (strArr[0].equals("九")) {
            strArr[0]="09";
        }else if (strArr[0].equals("十")) {
            strArr[0]="10";
        }else if (strArr[0].equals("十一")) {
            strArr[0]="11";
        }else if (strArr[0].equals("十二")) {
            strArr[0]="12";
        }
        s=strArr[0]+strArr[1];
        Date date = null;
        SimpleDateFormat sf2 = null;
        try {
            SimpleDateFormat sf1 = new SimpleDateFormat("MM dd, yyyy", Locale.ENGLISH); 
            date = sf1.parse(s);
            sf2 = new SimpleDateFormat("yyyy-MM-dd");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sf2.format(date);
    }
    
    
}




二、
package com.djzh.common.utils;

import java.io.PrintStream;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

/**
 * 日期工具类
 * @author hanlin
 *
 */
public class DateTimeUtils
{
  public static Date getNowDate()
  {
    Date currentTime = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String dateString = formatter.format(currentTime);
    ParsePosition pos = new ParsePosition(0);
    Date currentTime_2 = formatter.parse(dateString, pos);
    return currentTime_2;
  }

  public static Date getNowDateShort() {
    Date currentTime = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String dateString = formatter.format(currentTime);
    ParsePosition pos = new ParsePosition(8);
    Date currentTimeFormat = formatter.parse(dateString, pos);
    return currentTimeFormat;
  }

  public static String getYyyyMMddHhmmStrDate(Date date) throws Exception{
    SimpleDateFormat df =   new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
    return df.format(date);
  }
  
  public static String getStrDate(Date date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    return sdf.format(date);
  }

  public static String getYyyyMMStrDate(Date date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
    return sdf.format(date);
  }

  public static String getYyyyStrDate(Date date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
    return sdf.format(date);
  }

  public static Integer getStrIntegerDate(Date date)
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    return new Integer(sdf.format(date));
  }

  public static String getStrYyyyMMdd(Date date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    return sdf.format(date);
  }

  public static String getStrYyyyMMddHhmmss(Date date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    return sdf.format(date);
  }

  public static int getIntervalYear(int interval)
  {
    Date currentTime = new Date();
    return currentTime.getYear() + interval;
  }

  public static Date strToDate(String strDate) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    ParsePosition pos = new ParsePosition(0);
    Date strtodate = formatter.parse(strDate, pos);
    return strtodate;
  }

  public static String getStringDateShort()
  {
    Date currentTime = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String dateString = formatter.format(currentTime);
    return dateString;
  }

  public static String dateToStr(Date dateDate)
  {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String dateString = formatter.format(dateDate);
    return dateString;
  }

  public static String dateToLocalStr(Date dateDate) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
    String dateString = formatter.format(dateDate);
    return dateString;
  }
  public static String dateToLocalWeekStr(Date dateDate) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 E", Locale.CHINA);
    String dateString = formatter.format(dateDate);
    return dateString;
  }

  public static String dateToLocalStrYearAndMonth(Date dateDate)
  {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月");
    String dateString = formatter.format(dateDate);
    return dateString;
  }

  public static long getDays(String date1, String date2)
  {
    if ((date1 == null) || (date1.equals("")))
      return 0L;
    if ((date2 == null) || (date2.equals(""))) {
      return 0L;
    }
    SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    Date mydate = null;
    try {
      date = myFormatter.parse(date1);
      mydate = myFormatter.parse(date2);
    } catch (Exception localException) {
    }
    long day = (date.getTime() - mydate.getTime()) / 86400000L;
    return day;
  }

  public static String getNextDay(String nowdate, String delay)
  {
    try
    {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      String mdate = "";
      Date d = strToDate(nowdate);
      long myTime = d.getTime() / 1000L + Integer.parseInt(delay) * 24 * 60 * 60;
      d.setTime(myTime * 1000L);
      return format.format(d);
    } catch (Exception e) {
    }
    return "";
  }

  public static String getCurrentWeekOfMonth(Calendar calendar)
  {
    String strWeek = "";
    int dw = calendar.get(7);
    if (dw == 1)
      strWeek = "星期天";
    else if (dw == 2)
      strWeek = "星期一";
    else if (dw == 3)
      strWeek = "星期二";
    else if (dw == 4)
      strWeek = "星期三";
    else if (dw == 5)
      strWeek = "星期四";
    else if (dw == 6)
      strWeek = "星期五";
    else if (dw == 7) {
      strWeek = "星期六";
    }
    return strWeek;
  }

  public static int getQuarterByStrDate(String date)
  {
    if (date.split("-").length < 3) {
      return 0;
    }
    int month = Integer.parseInt(date.split("-")[1]);

    if ((month > 0) && (month <= 3))
      return 1;
    if ((month > 3) && (month <= 6))
      return 2;
    if ((month > 6) && (month <= 9))
      return 3;
    if ((month > 9) && (month <= 12)) {
      return 4;
    }
    return 0;
  }

  public static Date getDateByYyyyMMddHHmmss(String yMdhms)
  {
    try
    {
      SimpleDateFormat ymdHmsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      if (yMdhms.length() > 19) {
        yMdhms = yMdhms.substring(0, 19);
      }
      return ymdHmsFormat.parse(yMdhms); } catch (ParseException e) {
    }
    return new Date();
  }

  public static Date getDateByYyyyMmDdHhMmSs(String ymdhms)
  {
    try {
      SimpleDateFormat ymdHmsFormat = new SimpleDateFormat("yyyyMMddHHmmss");
      return ymdHmsFormat.parse(ymdhms); } catch (ParseException e) {
    }
    return new Date();
  }

  public static String getYyyyMMddHHmmssStr(Date date)
  {
    SimpleDateFormat ymdHmsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return ymdHmsFormat.format(date);
  }

  public static Date getYyyyMMddHHmmssDate(String date)
  {
    try
    {
      SimpleDateFormat ymdHmsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      return ymdHmsFormat.parse(date); } catch (ParseException e) {
    }
    return new Date();
  }

  public static String getYyyyMMddHHmmssHan(Date date)
  {
    SimpleDateFormat ymdHmsFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
    return ymdHmsFormat.format(date);
  }

  public static Boolean dateCompare(String myDate)
    throws ParseException
  {
    Date nowdate = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    Date d = sdf.parse(myDate);
    return Boolean.valueOf(d.before(nowdate));
  }

  public static String parseFormatDateByString(String time)
  {
    String now = null;
    try {
      if (StringUtils.trimToNull(time) == null) {
        return null;
      }
      Date date = new SimpleDateFormat("yyyyMMddHHmmss").parse(time);
      now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return now;
  }

  public static String getFormatDate(Date date) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
    return format.format(date);
  }

  public static void main(String[] args) throws ParseException
  {
    System.out.println(getStrYyyyMMddHhmmss(new Date()));
    System.out.println(getStrYyyyMMdd(new Date()));
  }
}

 

posted @ 2018-12-27 11:47  张永光  阅读(186)  评论(0编辑  收藏  举报