posts - 48,  comments - 5,  trackbacks - 0

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

          /**
           * 日期時間顯示的多種格式類
           * 以不同方法實現日期時間的不同顯示格式
           * @author 逍湘
           * QQ:297187963
           * E-mail:tylz7758@163.com
           * @version 1.0
           * @time 2007年7月26日 上午10時23分51秒
           */
          public class Datetime {
           
           /**
            * 以字符串格式顯示日期時間(Thu Jul 26 10:23:51 CST 2007)
            * @return datetime
            */
           public String getDatetime_String1(){
            String datetime=new Date().toString();
            return datetime;
           }
           
           /**
            * 以字符串格式顯示日期時間(26 Jul 2007 02:23:51 GMT)
            * @return datetime
            */
           public String getDatetime_String2(){
            String datetime=new Date().toGMTString();
            return datetime;
           } 
           
           /**
            * 以系統格式顯示日期時間(yy-MM-dd 上午HH:mm)
            * @return datetime
            */
           public String getDatetime_System(){
            DateFormat dt=DateFormat.getInstance();
            String datetime=dt.format(new Date()).toString();
            return datetime;
           }
           
           /**
            * 以中國格式顯示日期時間(xxxx年xx月xx日 下午xx時xx分xx秒)
            * @return datetime
            */
           public String getDatetime_China(){
             DateFormat datetime1=DateFormat.getDateInstance(DateFormat.LONG,Locale.CHINA);
             DateFormat datetime2=DateFormat.getTimeInstance(DateFormat.LONG,Locale.CHINA);
             String datetime=datetime1.format(new Date())+" "+datetime2.format(new Date());
            return datetime;
           }
           
           /**
            * 以常用格式顯示日期時間(yyyy-MM-dd HH:mm:ss+MILLISECOND)
            * @return datetime
            */
           public String getDatetime_Standard(){
              Calendar now=Calendar.getInstance();
              String datetime =
                           now.get(Calendar.YEAR)+"-" +
                           (now.get(Calendar.MONTH)+1)+"-" +
                           now.get(Calendar.DAY_OF_MONTH)+" "+
                           now.get(Calendar.HOUR)+":"+
                           now.get(Calendar.MINUTE)+":"+
                           now.get(Calendar.SECOND)+
                           now.get(Calendar.MILLISECOND);               
              return datetime;
           }
           
           /**
            * 以常用格式顯示日期時間(yyyy-MM-dd HH:mm:ss)
            * @return datetime
            */
           public String getDatetime(){  
              String datetime =new Date().toLocaleString();               
              return datetime;
           } 

           /**
            * 以簡單系統格式顯示日期時間(yyyy-MM-dd)
            * @return
            */
           public String getDatetime_SimpleDateFormat (){
            try {
             SimpleDateFormat f = new java.text.SimpleDateFormat("yyyy-MM-dd");
             String sDate = f.format(new java.util.Date());
             java.util.Date dt = f.parse(sDate);
             java.sql.Date sqlDate = new java.sql.Date(dt.getTime());
             String datetime=sqlDate.toString();
             return datetime;
            } catch (Exception ee) {
             ee.printStackTrace();
             return null;
            }
           }
           
           
           /**
            * 測試 顯示結果
            * @param args
            */
           public static void main(String[] args) {
            // TODO 自動生成方法存根
            System.out.println("以字符串格式顯示日期時間: "+new Datetime().getDatetime_String1());
            System.out.println("以字符串格式顯示日期時間: "+new Datetime().getDatetime_String2());
            System.out.println("以系統格式顯示日期時間: "+new Datetime().getDatetime_System());
            System.out.println("以中國格式顯示日期時間: "+new Datetime().getDatetime_China());
            System.out.println("以常用格式顯示日期時間: "+new Datetime().getDatetime_Standard());
            System.out.println("以常用格式顯示日期時間: "+new Datetime().getDatetime());
            System.out.println("以簡單系統格式顯示日期: "+new Datetime().getDatetime_SimpleDateFormat());
           }

          }

          posted on 2007-11-21 20:54 逍湘 閱讀(1894) 評論(2)  編輯  收藏


          FeedBack:
          # re: java函數顯示日期時間的多種格式
          2009-06-04 21:26 | 方石兵


          您寫的代碼 清晰 很容易看懂!

          寫的好!  回復  更多評論
            
          # re: java函數顯示日期時間的多種格式
          2009-06-04 21:27 | 方石兵


          我加你的QQ 行不  回復  更多評論
            

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


          網站導航:
           

          留言簿(2)

          隨筆檔案(49)

          文章檔案(17)

          最新隨筆

          積分與排名

          • 積分 - 26313
          • 排名 - 1495

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 江安县| 松滋市| 调兵山市| 班玛县| 宕昌县| 黄平县| 扬中市| 安塞县| 辉南县| 福鼎市| 安仁县| 平舆县| 开远市| 仁怀市| 白银市| 海淀区| 望江县| 射阳县| 深州市| 乌拉特中旗| 张掖市| 墨玉县| 云浮市| 汝南县| 赤城县| 会东县| 东明县| 游戏| 治县。| 呼和浩特市| 兰州市| 广水市| 葫芦岛市| 徐闻县| 天峻县| 乐亭县| 伊金霍洛旗| 西平县| 卫辉市| 开封县| 汤阴县|