程序 人生

          程序 人生

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            11 Posts :: 2 Stories :: 18 Comments :: 0 Trackbacks

          常用鏈接

          留言簿(1)

          隨筆檔案(11)

          文章檔案(2)

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          一個完整的日期實現類

          package com.nyhr.util;

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

          /**
          ?* <p>Title: 日期時間處理</p>
          ?* <p>Description: 工具類</p>
          ?* <p>Copyright: Copyright (c) 2005</p>
          ?* <p>
          ?* @version 1.0
          ?* @author?
          ?*/
          public class DateUtil
          {
          ??? /**
          ???? * 缺省的DateFormat對象,可以將一個java.util.Date格式化成yyyy-mm-dd輸出
          ???? */
          ??? private static DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.SIMPLIFIED_CHINESE);

          ??? /**
          ???? * 私有構造函數
          ???? */
          ??? private DateUtil()
          ??? {
          ??? }

          ??? /**
          ???? * <p>返回一個當前的時間,并按格式轉換為字符串</p>
          ???? * 例:17:27:03
          ???? * @return String
          ???? */
          ??? public static String getNowTime()
          ??? {
          ??????? GregorianCalendar gcNow = new GregorianCalendar();
          ??????? java.util.Date dNow = gcNow.getTime();
          ??????? DateFormat df = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.SIMPLIFIED_CHINESE);
          ??????? return df.format(dNow);
          ??? }

          ??? /**
          ???? * <p>返回一個當前日期,并按格式轉換為字符串</p>
          ???? * 例:2004-4-30
          ???? * @return String
          ???? */
          ??? public static String getNowDate()
          ??? {
          ??????? GregorianCalendar gcNow = new GregorianCalendar();
          ??????? java.util.Date dNow = gcNow.getTime();
          ??????? DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.SIMPLIFIED_CHINESE);
          ??????? return df.format(dNow);
          ??? }

          ??? /**
          ???? * <p>返回一個當前日期和時間,并按格式轉換為字符串</p>
          ???? * 例:2004-4-30 17:27:03
          ???? * @return String
          ???? */
          ??? public static String getNowDateTime()
          ??? {
          ??????? GregorianCalendar gcNow = new GregorianCalendar();
          ??????? java.util.Date dNow = gcNow.getTime();
          ??????? DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.SIMPLIFIED_CHINESE);
          ??????? return df.format(dNow);
          ??? }

          ??? /**
          ???? * <p>返回當前年</p>
          ???? * @return int
          ???? */
          ??? public static int getThisYear()
          ??? {
          ??????? GregorianCalendar gcNow = new GregorianCalendar();
          ??????? return gcNow.get(GregorianCalendar.YEAR);
          ??? }

          ??? /**
          ???? * 返回本月
          ???? * @return int
          ???? */
          ??? public static int getThisMonth()
          ??? {
          ??????? GregorianCalendar gcNow = new GregorianCalendar();
          ??????? return gcNow.get(GregorianCalendar.MONTH) + 1;
          ??? }

          ??? /**
          ???? * 返回今天是本月的第幾天
          ???? * @return int 從1開始
          ???? */
          ??? public static int getToDayOfMonth()
          ??? {
          ??????? GregorianCalendar gcNow = new GregorianCalendar();
          ??????? return gcNow.get(GregorianCalendar.DAY_OF_MONTH);
          ??? }
          ??? /**
          ???? * 返回當前的小時
          ???? * @return int
          ???? */
          ??? public static int getHour()
          ??? {
          ??? ? GregorianCalendar gcNow = new GregorianCalendar();
          ???????? return gcNow.get(GregorianCalendar.HOUR);
          ??? }
          ??? /**
          ???? * 返回當前的分鐘
          ???? * @return int 返回當前的分鐘
          ???? */
          ??? public static int getMinute()
          ??? {
          ??? ? GregorianCalendar gcNow = new GregorianCalendar();
          ???????? return gcNow.get(GregorianCalendar.MINUTE);
          ??? }
          ??? /**
          ???? * 返回當前的秒數
          ???? * @return int 第幾秒
          ???? */
          ??? public static int getSecond()
          ??? {
          ??? ? GregorianCalendar gcNow = new GregorianCalendar();
          ???????? return gcNow.get(GregorianCalendar.SECOND);
          ??? }
          ??? /**
          ???? * 返回今天是本年的第幾周
          ???? * @return int 從1開始
          ???? */
          ????
          ??? public static int getToWeekOfYear()
          ??? {
          ??? ?GregorianCalendar gcNow = new GregorianCalendar();
          ??? ?return gcNow.get(GregorianCalendar.WEEK_OF_YEAR);
          ??? }
          ??? /**
          ???? * 返回一格式化的日期
          ???? * @param time long
          ???? * @return String yyyy-mm-dd 格式
          ???? */
          ??? public static String formatDate(java.util.Date date)
          ??? {
          ??????? if (date == null) return "";
          ??????? else return df.format(date);
          ??? }

          ??? /**
          ???? * 返回一格式化的日期
          ???? * @param time long
          ???? * @return String 2005-6-17 格式
          ???? */
          ??? public static String formatSDate(java.util.Date date)
          ??? {
          ??????? if (date == null) return "";
          ??????? else
          ??????? {
          ??????????? SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
          ??????????? return bartDateFormat.format(date);
          ??????? }
          ??? }

          ??? /**
          ???? * 返回已添加指定時間間隔的日期
          ???? * @param interval? 表示要添加的時間間隔("y":年;"d":天;"m":月;如有必要可以自行增加)
          ???? * @param number??? 表示要添加的時間間隔的個數
          ???? * @param date????? java.util.Date()
          ???? * @return String?? 2005-5-12格式的日期字串
          ???? */
          ??? public static String dateAdd(String interval, int number,
          ??????????? java.util.Date date)
          ??? {
          ??????? String strTmp = "";
          ??????? GregorianCalendar gc = new GregorianCalendar();
          ??????? gc.setTime(date);
          ??????? //加若干年
          ??????? if (interval.equals("y"))
          ??????? {
          ??????????? int currYear = gc.get(Calendar.YEAR);
          ??????????? gc.set(Calendar.YEAR, currYear + number);
          ??????? }
          ??????? //加若干月
          ??????? else if (interval.equals("m"))
          ??????? {
          ??????????? int currMonth = gc.get(Calendar.MONTH);
          ??????????? gc.set(Calendar.MONTH, currMonth + number);
          ??????? }
          ??????? //加若干天
          ??????? else if (interval.equals("d"))
          ??????? {
          ??????????? int currDay = gc.get(Calendar.DATE);
          ??????????? gc.set(Calendar.DATE, currDay + number);
          ??????? }
          ??????? //加若小時
          ??????? else if (interval.equals("h"))
          ??????? {
          ??????????? int currDay = gc.get(Calendar.HOUR);
          ??????????? gc.set(Calendar.HOUR, currDay + number);
          ??????? }
          ??????? SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
          ??????? strTmp = bartDateFormat.format(gc.getTime());
          ??????? return strTmp;
          ??? }

          ??? /**
          ???? * <p>返回兩個日期之間的單位間隔數</p>
          ???? * @param a java.util.Date
          ???? * @param b java.util.Date
          ???? * @return int 間隔數
          ???? */
          ??? public static int dateDiff(java.util.Date a, java.util.Date b)
          ??? {
          ??????? int tempDifference = 0;
          ??????? int difference = 0;
          ??????? Calendar earlier = Calendar.getInstance();
          ??????? Calendar later = Calendar.getInstance();

          ??????? if (a.compareTo(b) < 0)
          ??????? {
          ??????????? earlier.setTime(a);
          ??????????? later.setTime(b);
          ??????? }
          ??????? else
          ??????? {
          ??????????? earlier.setTime(b);
          ??????????? later.setTime(a);
          ??????? }

          ??????? while (earlier.get(Calendar.YEAR) != later.get(Calendar.YEAR))
          ??????? {
          ??????????? tempDifference = 365 * (later.get(Calendar.YEAR) - earlier.get(Calendar.YEAR));
          ??????????? difference += tempDifference;

          ??????????? earlier.add(Calendar.DAY_OF_YEAR, tempDifference);
          ??????? }

          ??????? if (earlier.get(Calendar.DAY_OF_YEAR) != later.get(Calendar.DAY_OF_YEAR))
          ??????? {
          ??????????? tempDifference = later.get(Calendar.DAY_OF_YEAR)
          ??????????????????? - earlier.get(Calendar.DAY_OF_YEAR);
          ??????????? difference += tempDifference;

          ??????????? earlier.add(Calendar.DAY_OF_YEAR, tempDifference);
          ??????? }

          ??????? return difference;
          ??? }

          /**
          ???? * <p>該方法是獲得到每月1號星期一的數據</p>
          ???? * @return?-返回一個數字
          ???? */
          ??? /**
          ???? * <p>該方法是獲得到每月1號星期一的數據</p>
          ???? * @return?-返回一個數字
          ???? */
          ??? public static int getDate(int curYear,int curMonth ,int curDate)
          ??? {
          ??? ?int day1 = 0;
          ??? ?Calendar cal = Calendar.getInstance();
          ??? ?cal.clear();
          ??? ?cal.set(curYear,curMonth-1,curDate);
          ??? ?int dayOfWeek = cal.get(cal.DAY_OF_WEEK);???? ?
          ??? ?System.out.println("curDate="+curDate +" dayOfWeek "+dayOfWeek);
          ??? ?switch(dayOfWeek)
          ??? ?{
          ??? ??case 1:???//星期天
          ??? ???day1=0;
          ??? ???break;
          ??? ??case 2:???//星期一
          ??? ???day1=1;
          ??? ???break;
          ??? ??case 3:???//星期二
          ??? ???day1=2;
          ??? ???break;
          ??? ??case 4:???//星期三
          ??? ???day1=3;
          ??? ???break;
          ??? ??case 5:???//星期四
          ??? ???day1=4;
          ??? ???break;
          ??? ??case 6:???//星期五
          ??? ???day1=5;
          ??? ???break;
          ??? ??case 7:???//星期六
          ??? ???day1=6;
          ??? ???break;
          ??? ?}??
          ??? ?return day1;
          ??? }
          ??? public static String checkTime(int id)
          ??? {
          ??????? String bol = "";
          ??????? Calendar tt = Calendar.getInstance();?????
          ??????? String currDate=getNowDate();???
          ?????? System.out.println("currDate="+currDate);
          ??????? int result = tt.get(Calendar.DAY_OF_WEEK);
          ?
          ??????? int shour = tt.get(Calendar.HOUR_OF_DAY);
          ???????
          ??????? if (id == 3)
          ??????? {
          ??????????? switch (result)
          ??????????? {
          ??????????????? case 1:
          ??????????????????? break;
          ??????????????? case 7:
          ??????????????????? if ((shour >= 8) && (shour < 12))
          ??????????????????? {
          ??????????????????????? bol = "disabled";
          ??????????????????????? break;
          ??????????????????? }
          ??????????????? default:
          ??????????????????? if ((shour >= 8) && (shour < 12))
          ??????????????????? {
          ??????????????????????? bol = "disabled";
          ??????????????????????? break;
          ??????????????????? }
          ??????????????????? else if ((shour >= 14) && (shour < 17))
          ??????????????????? {
          ??????????????????????? bol = "disabled";
          ??????????????????????? break;
          ??????????????????? }
          ??????????? }
          ??????? }
          ??????? return bol;
          ??? }
          ??? /**
          ???? * <p>該方法是將字符型轉變成日期型</p>
          ???? * @param strX -傳入字符類型
          ???? * @return -返回日期類型
          ???? */
          ??? public static Date getStrDate(String strX)
          ??? {
          ??? ?Date date1=new Date();
          ??? ?if (!strX.equals(""))
          ??? ?{
          ??? ??try
          ??? ??{
          ??? ???date1=(DateFormat.getDateInstance()).parse(strX);
          ??? ??}
          ??? ??catch(Exception ex)
          ??? ??{
          ??? ???Debug.log("hhh","getStrDate()",ex.toString());
          ??? ???//System.out.println(ex.toString());
          ??? ??}
          ??? ?}
          ??? ?else
          ??? ?{
          ??? ?? GregorianCalendar gcNow = new GregorianCalendar();
          ??? ?????? date1 = gcNow.getTime();
          ??? ?}
          ??? ?
          ??? ?return date1;
          ??? }
          ???
          ??? /**
          ???? * <p>比較兩日期字符串的大小</p>
          ???? * @param d1
          ???? * @param d2
          ???? * @return (d1>d2)?2:(d1=d2)?1:0
          ???? */
          ??? public static int compareDate(String d1, String d2)
          ??? {
          ??? ?short vl = 1;
          ??? ?GregorianCalendar gc = new GregorianCalendar();
          ??????? gc.setTime(getStrDate(d1));
          ??????? int year = gc.get(GregorianCalendar.YEAR);
          ??????? int month = gc.get(GregorianCalendar.MONTH);
          ??????? int day = gc.get(GregorianCalendar.DAY_OF_MONTH);
          ??????? gc.setTime(getStrDate(d2));
          ??????? int tempYear = gc.get(GregorianCalendar.YEAR);
          ??????? int tempMonth = gc.get(GregorianCalendar.MONTH);
          ??????? int tempDay = gc.get(GregorianCalendar.DAY_OF_MONTH);
          ??????? if(year !=? tempYear)
          ??????? {
          ??????? ?if (year>tempYear)
          ??????? ??vl = 2;
          ??????? ?else
          ??????? ??vl = 0;
          ??????? }
          ??????? else
          ??????? {
          ??????? ?if (month != tempMonth)
          ??????? ?{
          ??????? ??if(month>tempMonth)
          ??????? ???vl = 2;
          ??????? ??else
          ??????? ???vl = 0;
          ??????? ?}
          ??????? ?else
          ??????? ?{
          ??????? ??if (day != tempDay)
          ??????? ??{
          ??????? ???if (day > tempDay)
          ??????? ????vl = 2;
          ??????????? ??else
          ??????????? ???vl = 0;
          ??????? ??}
          ??????? ?}
          ??????? }
          ??????? return vl;
          ??? }
          ???
          }

          posted on 2006-06-30 10:04 程序-人生 閱讀(1085) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 闽侯县| 鹿邑县| 蓝山县| 巨鹿县| 阿荣旗| 翁源县| 勃利县| 昆山市| 漯河市| 城口县| 长武县| 马关县| 宾川县| 忻城县| 贡嘎县| 万盛区| 崇阳县| 嘉鱼县| 利川市| 辽源市| 陵水| 个旧市| 阜南县| 二连浩特市| 梁山县| 禄劝| 鲁山县| 怀远县| 德令哈市| 郸城县| 三穗县| 原平市| 新田县| 普兰县| 辽阳县| 白城市| 抚顺县| 浦东新区| 太康县| 宁远县| 叙永县|