« java中文件操作大全(经典收藏)« »java.long.String中的方法_java字符串函数总结大全 »
java处理日期函数汇总(经典收藏)

/*
* 创建日期 2007-03-14
*
* 功能   取日期时间工具
*
*/
package com.util;

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


/**
* 说明:       取日期时间工具

*
* @author    好学蜘蛛
* @author   haoxueweb@163.com
* @version   1.0
* Company   http://freehost04.websamba.com/dudu178
*/
public class DateUtil {


public DateUtil(){}
/**
      * @see      取得当前日期(格式为:yyyy-MM-dd)
      * @return String
      */
     public String GetDate()
     {
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         String sDate = sdf.format(new Date());
         return sDate;
     }


     /**
      * @see      取得当前时间(格式为:yyy-MM-dd HH:mm:ss)
      * @return String
      */
     public static String GetDateTime()
     {
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         String sDate = sdf.format(new Date());
         return sDate;
     }


     /**
      * @see      按指定格式取得当前时间()
      * @return String
      */
     public String GetTimeFormat(String strFormat)
     {
         SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
         String sDate = sdf.format(new Date());
         return sDate;
     }


     /**
      * @see      取得指定时间的给定格式()
      * @return String
      * @throws ParseException
      */
     public String SetDateFormat(String myDate,String strFormat) throws ParseException
     {

         SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
         String sDate = sdf.format(sdf.parse(myDate));

         return sDate;
     }


     public String FormatDateTime(String strDateTime, String strFormat)
     {
         String sDateTime = strDateTime;
         try {
             Calendar Cal = parseDateTime(strDateTime);
             SimpleDateFormat sdf = null;
             sdf = new SimpleDateFormat(strFormat);
             sDateTime = sdf.format(Cal.getTime());
         } catch (Exception e) {

         }
         return sDateTime;
     }


     public static Calendar parseDateTime(String baseDate)
     {
         Calendar cal = null;
         cal = new GregorianCalendar();
         int yy = Integer.parseInt(baseDate.substring(0, 4));
         int mm = Integer.parseInt(baseDate.substring(5, 7)) - 1;
         int dd = Integer.parseInt(baseDate.substring(8, 10));
         int hh = 0;
         int mi = 0;
         int ss = 0;
         if(baseDate.length() > 12)
         {
             hh = Integer.parseInt(baseDate.substring(11, 13));
             mi = Integer.parseInt(baseDate.substring(14, 16));
             ss = Integer.parseInt(baseDate.substring(17, 19));
         }
         cal.set(yy, mm, dd, hh, mi, ss);
         return cal;
     }
     public int getDay(String strDate)
     {
         Calendar cal = parseDateTime(strDate);
         return   cal.get(Calendar.DATE);
     }


     public int getMonth(String strDate)
     {
         Calendar cal = parseDateTime(strDate);

         return cal.get(Calendar.MONTH) + 1;
     }


     public int getWeekDay(String strDate)
     {
         Calendar cal = parseDateTime(strDate);
         return cal.get(Calendar.DAY_OF_WEEK);
     }


     public String getWeekDayName(String strDate)
     {
         String mName[] = {
             "日", "一", "二", "三", "四", "五", "六"
         };
         int iWeek = getWeekDay(strDate);
         iWeek = iWeek - 1;
         return "星期" + mName[iWeek];
     }


     public int getYear(String strDate)
     {
         Calendar cal = parseDateTime(strDate);
         return cal.get(Calendar.YEAR) + 1900;
     }


     public String DateAdd(String strDate, int iCount, int iType)
     {
         Calendar Cal = parseDateTime(strDate);
         int pType = 0;
         if(iType == 0)
         {
             pType = 1;
         } else
         if(iType == 1)
         {
             pType = 2;
         } else
         if(iType == 2)
         {
             pType = 5;
         } else
         if(iType == 3)
         {
             pType = 10;
         } else
         if(iType == 4)
         {
             pType = 12;
         } else
         if(iType == 5)
         {
             pType = 13;
         }
         Cal.add(pType, iCount);
         SimpleDateFormat sdf = null;
         if(iType <= 2)
             sdf = new SimpleDateFormat("yyyy-MM-dd");
         else
             sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         String sDate = sdf.format(Cal.getTime());
         return sDate;
     }


     public String DateAdd(String strOption, int iDays, String strStartDate)
     {
         if(!strOption.equals("d"));
         return strStartDate;
     }


     public int DateDiff(String strDateBegin, String strDateEnd, int iType)
     {
         Calendar calBegin = parseDateTime(strDateBegin);
         Calendar calEnd = parseDateTime(strDateEnd);
         long lBegin = calBegin.getTimeInMillis();
         long lEnd = calEnd.getTimeInMillis();
         int ss = (int)((lBegin - lEnd) / 1000L);
         int min = ss / 60;
         int hour = min / 60;
         int day = hour / 24;
         if(iType == 0)
             return hour;
         if(iType == 1)
             return min;
         if(iType == 2)
             return day;
         else
             return -1;
     }



    
     /*****************************************
      * @功能      判断某年是否为闰年
      * @return   boolean
      * @throws ParseException
      ****************************************/
     public boolean isLeapYear(int yearNum){
      boolean isLeep = false;
         /**判断是否为闰年,赋值给一标识符flag*/
         if((yearNum % 4 == 0) && (yearNum % 100 != 0)){
          isLeep = true;
         }   else if(yearNum % 400 ==0){
          isLeep = true;
         } else {
          isLeep = false;
         }
         return isLeep;
     }

    
      /*****************************************
      * @功能      计算当前日期某年的第几周
      * @return   interger
      * @throws ParseException
      ****************************************/
     public int getWeekNumOfYear(){
      Calendar calendar = Calendar.getInstance();
      int iWeekNum = calendar.get(Calendar.WEEK_OF_YEAR);
      return iWeekNum;
     }


     /*****************************************
      * @功能      计算指定日期某年的第几周
      * @return   interger
      * @throws ParseException
      ****************************************/
     public int getWeekNumOfYearDay(String strDate ) throws ParseException{
      Calendar calendar = Calendar.getInstance();
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      Date curDate = format.parse(strDate);
      calendar.setTime(curDate);
      int iWeekNum = calendar.get(Calendar.WEEK_OF_YEAR);
      return iWeekNum;
     }
      
     }



     /*****************************************
      * @功能      计算某年某周的开始日期
      * @return   interger
      * @throws ParseException
      ****************************************/
     public String getYearWeekFirstDay(int yearNum,int weekNum) throws ParseException {
     
      Calendar cal = Calendar.getInstance();
      cal.set(Calendar.YEAR, yearNum);
      cal.set(Calendar.WEEK_OF_YEAR, weekNum);
      cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
      //分别取得当前日期的年、月、日
      String tempYear = Integer.toString(yearNum);
      String tempMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);
      String tempDay = Integer.toString(cal.get(Calendar.DATE));
      String tempDate = tempYear + "-" +tempMonth + "-" + tempDay;
      return SetDateFormat(tempDate,"yyyy-MM-dd");



     /*****************************************
      * @功能      计算某年某周的结束日期
      * @return   interger
      * @throws ParseException
      ****************************************/
     public String getYearWeekEndDay(int yearNum,int weekNum) throws ParseException {
      Calendar cal = Calendar.getInstance();
      cal.set(Calendar.YEAR, yearNum);
      cal.set(Calendar.WEEK_OF_YEAR, weekNum + 1);
      cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
         //分别取得当前日期的年、月、日
      String tempYear = Integer.toString(yearNum);
      String tempMonth = Integer.toString(cal.get(Calendar.MONTH) + 1);
      String tempDay = Integer.toString(cal.get(Calendar.DATE));
      String tempDate = tempYear + "-" +tempMonth + "-" + tempDay;
      return SetDateFormat(tempDate,"yyyy-MM-dd");
     }


        
     /*****************************************
      * @功能      计算某年某月的开始日期
      * @return   interger
      * @throws ParseException
      ****************************************/
     public String getYearMonthFirstDay(int yearNum,int monthNum) throws ParseException {
     
      //分别取得当前日期的年、月、日
      String tempYear = Integer.toString(yearNum);
      String tempMonth = Integer.toString(monthNum);
      String tempDay = "1";
      String tempDate = tempYear + "-" +tempMonth + "-" + tempDay;
      return SetDateFormat(tempDate,"yyyy-MM-dd");     
     }



     /*****************************************
      * @功能      计算某年某月的结束日期
      * @return   interger
      * @throws ParseException
      ****************************************/
     public String getYearMonthEndDay(int yearNum,int monthNum) throws ParseException {

        //分别取得当前日期的年、月、日
      String tempYear = Integer.toString(yearNum);
      String tempMonth = Integer.toString(monthNum);
      String tempDay = "31";
      if (tempMonth.equals("1") || tempMonth.equals("3") || tempMonth.equals("5") || tempMonth.equals("7") ||tempMonth.equals("8") || tempMonth.equals("10") ||tempMonth.equals("12")) {
       tempDay = "31";
      }
      if (tempMonth.equals("4") || tempMonth.equals("6") || tempMonth.equals("9")||tempMonth.equals("11")) {
       tempDay = "30";
      }
      if (tempMonth.equals("2")) {
       if (isLeapYear(yearNum)) {
        tempDay = "29";
       } else {
          tempDay = "28";
       }
      }
      //System.out.println("tempDay:" + tempDay);
      String tempDate = tempYear + "-" +tempMonth + "-" + tempDay;
      return SetDateFormat(tempDate,"yyyy-MM-dd"); 

     }


}

 


Tags: JAVA   |

原创文章如转载,请注明:转载自:巴士飞扬-技术BLOG : http://www.busfly.net/

本文链接地址:http://www.busfly.net/post/131.html

如果你喜欢本文,请顶一下,支持我,你的支持是我继续发好文章的最大动力。谢谢。
好东西需要分享,快把本文发给你的朋友吧~!~

     
相关文章:




◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
网站分类
分类最近文章
最近发表
最新评论及回复
最近留言
热文排行
随机推荐文章
Powered By Z-Blog   STYLE by busfly . FatMouse
Copyright © 2007 巴士飞扬技术博客. . 沪ICP备07027972号. 会员群1(J2EE为主):3769186.