I'll be back!

            Focus on BPM, celebrate PegaRULES Process Commander (PRPC)
          posts - 76, comments - 161, trackbacks - 0, articles - 2
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          JAVA中日期格式轉換

          Posted on 2006-09-25 10:34 zolly 閱讀(9200) 評論(0)  編輯  收藏
          /**
          ???*?字符串轉換為java.util.Date<br>
          ???*?支持格式為?yyyy.MM.dd?G?'at'?hh:mm:ss?z?如?'2002-1-1?AD?at?22:10:59?PSD'<br>
          ???*?yy/MM/dd?HH:mm:ss?如?'2002/1/1?17:55:00'<br>
          ???*?yy/MM/dd?HH:mm:ss?pm??如?'2002/1/1?17:55:00?pm'<br>
          ???*?yy-MM-dd?HH:mm:ss?如?'2002-1-1?17:55:00'?<br>
          ???*?yy-MM-dd?HH:mm:ss?am?如?'2002-1-1?17:55:00?am'?<br>
          ???*?
          @param ?time?String?字符串<br>
          ???*?
          @return ?Date?日期<br>
          ???
          */

          ??
          public ? static ?Date?stringToDate(String?time) {
          ????SimpleDateFormat?formatter;
          ????
          int ?tempPos = time.indexOf( " AD " )?;
          ????time
          = time.trim()?;
          ????formatter?
          = ? new ?SimpleDateFormat?( " yyyy.MM.dd?G?'at'?hh:mm:ss?z " );
          ????
          if (tempPos >- 1 ) {
          ??????time
          = time.substring( 0 ,tempPos) +
          ???????????
          " 公元 " + time.substring(tempPos + " AD " .length()); // china
          ??????formatter? = ? new ?SimpleDateFormat?( " yyyy.MM.dd?G?'at'?hh:mm:ss?z " );
          ????}

          ????tempPos
          = time.indexOf( " - " );
          ????
          if (tempPos >- 1 && (time.indexOf( " ? " ) < 0 )) {
          ??????formatter?
          = ? new ?SimpleDateFormat?( " yyyyMMddHHmmssZ " );
          ????}

          ????
          else ? if ((time.indexOf( " / " ) >- 1 )? && (time.indexOf( " ? " ) >- 1 )) {
          ??????formatter?
          = ? new ?SimpleDateFormat?( " yyyy/MM/dd?HH:mm:ss " );
          ????}

          ????
          else ? if ((time.indexOf( " - " ) >- 1 )? && (time.indexOf( " ? " ) >- 1 )) {
          ??????formatter?
          = ? new ?SimpleDateFormat?( " yyyy-MM-dd?HH:mm:ss " );
          ????}

          ????
          else ? if ((time.indexOf( " / " ) >- 1 )? && (time.indexOf( " am " ) >- 1 )? || (time.indexOf( " pm " ) >- 1 )) {
          ??????formatter?
          = ? new ?SimpleDateFormat?( " yyyy-MM-dd?KK:mm:ss?a " );
          ????}

          ????
          else ? if ((time.indexOf( " - " ) >- 1 )? && (time.indexOf( " am " ) >- 1 )? || (time.indexOf( " pm " ) >- 1 )) {
          ??????formatter?
          = ? new ?SimpleDateFormat?( " yyyy-MM-dd?KK:mm:ss?a " );
          ????}

          ????ParsePosition?pos?
          = ? new ?ParsePosition( 0 );
          ????java.util.Date?ctime?
          = ?formatter.parse(time,?pos);

          ????
          return ?ctime;
          ??}

          ??
          /**
          ???*?將java.util.Date?格式轉換為字符串格式'yyyy-MM-dd?HH:mm:ss'(24小時制)<br>
          ???*?如Sat?May?11?17:24:21?CST?2002?to?'2002-05-11?17:24:21'<br>
          ???*?
          @param ?time?Date?日期<br>
          ???*?
          @return ?String???字符串<br>
          ???
          */

          ???

          ??
          public ? static ?String?dateToString(Date?time) {
          ????SimpleDateFormat?formatter;
          ????formatter?
          = ? new ?SimpleDateFormat?( " yyyy-MM-dd?HH:mm:ss " );
          ????String?ctime?
          = ?formatter.format(time);

          ????
          return ?ctime;
          ??}

          ??
          /**
          ???*?將java.util.Date?格式轉換為字符串格式'yyyy-MM-dd?HH:mm:ss?a'(12小時制)<br>
          ???*?如Sat?May?11?17:23:22?CST?2002?to?'2002-05-11?05:23:22?下午'<br>
          ???*?
          @param ?time?Date?日期<br>
          ???*?
          @param ?x?int?任意整數如:1<br>
          ???*?
          @return ?String?字符串<br>
          ???
          */

          ??
          public ? static ?String?dateToString(Date?time, int ?x) {
          ????SimpleDateFormat?formatter;
          ????formatter?
          = ? new ?SimpleDateFormat?( " yyyy-MM-dd?KK:mm:ss?a " );
          ????String?ctime?
          = ?formatter.format(time);

          ????
          return ?ctime;
          ??}

          ??
          /**
          ???*取系統當前時間:返回只值為如下形式
          ???*2002-10-30?20:24:39
          ???*?
          @return ?String
          ???
          */

          ??
          public ? static ?String?Now() {
          ????
          return ?dateToString( new ?Date());
          ??}


          ??
          /**
          ???*取系統當前時間:返回只值為如下形式
          ???*2002-10-30?08:28:56?下午
          ???*
          @param ?hour?為任意整數
          ???*
          @return ?String
          ???
          */

          ??
          public ? static ?String?Now( int ?hour) {
          ????
          return ?dateToString( new ?Date(),hour);
          ??}

          ??
          /**
          ???*取系統當前時間:返回值為如下形式
          ???*2002-10-30
          ???*
          @return ?String
          ???
          */

          ??
          public ? static ?String?getYYYY_MM_DD() {
          ????
          return ?dateToString( new ?Date()).substring( 0 , 10 );

          ??}

          ??
          /**
          ???*取系統給定時間:返回值為如下形式
          ???*2002-10-30
          ???*
          @return ?String
          ???
          */

          ???
          public ? static ?String?getYYYY_MM_DD(String?date) {
          ????
          return ?date.substring( 0 , 10 );

          ??}


          ??
          public ? static ?String?getHour() {
          ????SimpleDateFormat?formatter;
          ????formatter?
          = ? new ?SimpleDateFormat?( " H " );
          ????String?ctime?
          = ?formatter.format( new ?Date());
          ????
          return ?ctime;
          ????}


          ??
          public ? static ?String?getDay() {
          ??????SimpleDateFormat?formatter;
          ????formatter?
          = ? new ?SimpleDateFormat?( " d " );
          ????String?ctime?
          = ?formatter.format( new ?Date());
          ????
          return ?ctime;
          ????}


          ??
          public ? static ?String?getMonth() {
          ????SimpleDateFormat?formatter;
          ????formatter?
          = ? new ?SimpleDateFormat?( " M " );
          ????String?ctime?
          = ?formatter.format( new ?Date());
          ????
          return ?ctime;
          ????}

          ??

          ??
          public ? static ?String?getYear() {
          ????SimpleDateFormat?formatter;
          ????formatter?
          = ? new ?SimpleDateFormat?( " yyyy " );
          ????String?ctime?
          = ?formatter.format( new ?Date());
          ????
          return ?ctime;
          ????}

          ??????
          ??
          public ? static ?String?getWeek() {
          ????SimpleDateFormat?formatter;
          ????formatter?
          = ? new ?SimpleDateFormat?( " E " );
          ????String?ctime?
          = ?formatter.format( new ?Date());
          ????
          return ?ctime;
          ????}
          ?

          在jsp頁面中的日期格式和sqlserver中的日期格式不一樣,怎樣統一?


          在頁面上顯示輸出時,用下面的函數處理一下

          public?class?DateUtil(){
          ????
          public?static?String?fmtShortEnu(Date?myDate)?{
          ????SimpleDateFormat?formatter?
          =?new?SimpleDateFormat("yyyy/MM/dd");
          ????String?strDate?
          =?formatter.format(myDate);
          ????
          return?strDate;
          ??}

          }

          new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          new java.text.SimpleDateFormat("yyyy-MM-dd")
          建議還是把sqlserver的字段類型改成varchar的吧,用字符串處理可以完全按照自己的意愿處理,沒有特殊的需求,不要使用date型


          字串日期格式轉換
          用的API是SimpleDateFormat,它是屬於java.text.SimpleDateFormat,所以請記得import進來!

          用法:
          SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          這一行最重要,它確立了轉換的格式,yyyy是完整的西元年,MM是月份,dd是日期, 至於HH:mm:ss就不需要我再解釋了吧!
          ps:為什麼有的格式大寫,有的格式小寫,那是怕避免混淆,例如MM是月份,mm是分;HH是24小時制,而hh是12小時制

          1.字串轉日期:
           2002-10-8 15:30:22要把它轉成日期,可以用
           Date date=sdf.parse("2002-10-8 15:30:22");
          2.日期轉字串
           假如把今天的日期轉成字串可用
           String datestr=sdf.format(new Date());
           這個字串的內容便類似2002-10-08 14:55:38

          透過這個API我們便可以隨心所欲的將日期轉成我們想要的字串格式,例如希望將日期輸出成2002年10月08日,
          我們可以這麼寫:
          SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
          String datestr=sdf.format(new Date());
          datestr便會依照我們設定的格式輸出

          //對日期格式的轉換成("yyyy-MM-dd")格式的方法
          public?java.sql.Date?Convert(String?str)
          ??
          {
          ????java.text.SimpleDateFormat?sdf?
          =?new?java.text.SimpleDateFormat("yyyy-MM-dd");
          ????
          try
          ????
          {
          ??????java.util.Date?d?
          =?sdf.parse(str);
          ??????java.sql.Date?d1?
          =?new?java.sql.Date(d.getTime());
          ??????
          return?d1;
          ????}

          ????
          catch(Exception?ex)
          ????
          {
          ??????ex.printStackTrace();
          ??????
          return?null;
          ????}

          ??}


          應用如下:
          ctmt.setDate(7,this.Convert(info.getManBirth()));? // @DATETIME

          獲得本月一日、本星期星期一、昨天的date對象的方法:(摘自:http://www.aygfsteel.com/qclass/archive/2006/09/24/71589.html)

          ????????GregorianCalendar?cal??=???new??GregorianCalendar();
          ????????Date?now??
          =???new??Date();
          ????????cal.setTime(now);
          ????????cal.setFirstDayOfWeek(GregorianCalendar.MONDAY);???
          //?設置一個星期的第一天為星期1,默認是星期日?
          ????????SimpleDateFormat?dateutil??=???new??SimpleDateFormat(?"?yyyy-MM-dd?"?);
          ????????System.out.println(?
          "?now=?"?+?dateutil.format(cal.getTime()));??//?今天?
          ????????
          ????????cal.add(GregorianCalendar.DATE,?
          -?1?);
          ????????System.out.println(?
          "?now=?"?+?dateutil.format(cal.getTime()));?//?昨天?????????
          ????????
          ????????cal.set(GregorianCalendar.DAY_OF_WEEK,?GregorianCalendar.MONDAY);
          ????????System.out.println(?
          "?now=?"?+?dateutil.format(cal.getTime()));???//?本周1?

          ????????cal.set(GregorianCalendar.DAY_OF_MONTH,??
          1?);?
          ????????System.out.println(?
          "?now=?"?+?dateutil.format(cal.getTime()));?//?本月1日?


          摘自:http://www.aygfsteel.com/galaxyp/archive/2006/04/13/40826.html

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


          網站導航:
           
          主站蜘蛛池模板: 勐海县| 台安县| 称多县| 游戏| 西吉县| 安图县| 井研县| 建昌县| 凌海市| 河西区| 岱山县| 穆棱市| 瑞昌市| 唐海县| 兴文县| 周口市| 平顶山市| 淮北市| 光泽县| 花莲县| 若尔盖县| 云霄县| 文化| 焦作市| 曲沃县| 汉寿县| 额敏县| 漳平市| 盐池县| 阜新| 仙游县| 南川市| 柞水县| 稷山县| 沐川县| 焦作市| 闸北区| 泰宁县| 河曲县| 阿勒泰市| 康乐县|