程序 人生

          程序 人生

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

          一個(gè)完整的日期實(shí)現(xiàn)類

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

          ??? /**
          ???? * 私有構(gòu)造函數(shù)
          ???? */
          ??? private DateUtil()
          ??? {
          ??? }

          ??? /**
          ???? * <p>返回一個(gè)當(dāng)前的時(shí)間,并按格式轉(zhuǎn)換為字符串</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>返回一個(gè)當(dāng)前日期,并按格式轉(zhuǎn)換為字符串</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>返回一個(gè)當(dāng)前日期和時(shí)間,并按格式轉(zhuǎn)換為字符串</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>返回當(dāng)前年</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);
          ??? }
          ??? /**
          ???? * 返回當(dāng)前的小時(shí)
          ???? * @return int
          ???? */
          ??? public static int getHour()
          ??? {
          ??? ? GregorianCalendar gcNow = new GregorianCalendar();
          ???????? return gcNow.get(GregorianCalendar.HOUR);
          ??? }
          ??? /**
          ???? * 返回當(dāng)前的分鐘
          ???? * @return int 返回當(dāng)前的分鐘
          ???? */
          ??? public static int getMinute()
          ??? {
          ??? ? GregorianCalendar gcNow = new GregorianCalendar();
          ???????? return gcNow.get(GregorianCalendar.MINUTE);
          ??? }
          ??? /**
          ???? * 返回當(dāng)前的秒數(shù)
          ???? * @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);
          ??????? }
          ??? }

          ??? /**
          ???? * 返回已添加指定時(shí)間間隔的日期
          ???? * @param interval? 表示要添加的時(shí)間間隔("y":年;"d":天;"m":月;如有必要可以自行增加)
          ???? * @param number??? 表示要添加的時(shí)間間隔的個(gè)數(shù)
          ???? * @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);
          ??????? }
          ??????? //加若小時(shí)
          ??????? 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>返回兩個(gè)日期之間的單位間隔數(shù)</p>
          ???? * @param a java.util.Date
          ???? * @param b java.util.Date
          ???? * @return int 間隔數(shù)
          ???? */
          ??? 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號(hào)星期一的數(shù)據(jù)</p>
          ???? * @return?-返回一個(gè)數(shù)字
          ???? */
          ??? /**
          ???? * <p>該方法是獲得到每月1號(hào)星期一的數(shù)據(jù)</p>
          ???? * @return?-返回一個(gè)數(shù)字
          ???? */
          ??? 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>該方法是將字符型轉(zhuǎn)變成日期型</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) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 榆中县| 井冈山市| 民乐县| 南安市| 宁明县| 莱阳市| 台东市| 金寨县| 锦屏县| 临安市| 仁化县| 威远县| 武宣县| 阳东县| 永城市| 湘阴县| 蓬莱市| 忻州市| 西贡区| 阿坝县| 绥江县| 湖州市| 芷江| 花莲市| 莒南县| 兴和县| 威宁| 平江县| 苗栗市| 泗水县| 温泉县| 临澧县| 阿尔山市| 启东市| 德保县| 特克斯县| 蒙城县| 漠河县| 石城县| 安庆市| 海阳市|