Jody

           

          java日期格式轉換測試

          java日期格式轉換測試

          來源:? 作者: [字體:]

          [更多內容請訪問 http://www.heci.net 賀詞網!]
          java日期格式轉換測試

          用到的包
          //import java.util.Locale;

          支持一鳴,就點一下↓

          import java.text.*;
          import java.util.*;

          1.-----------------------------------------
          得到系統當前時間:

          java.util.Date dt=new java.util.Date();
          System.out.print(dt);? //輸出結果是:Wed Aug 10 11:29:11 CST 2005

          SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
          System.out.print(sdf.format(dt));? //輸出結果是:2005-08-10

          2.-----------------------------------------
          把字符串轉化為java.util.Date
          方法一:
          SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
          java.util.Date dt=sdf.parse("2005-2-19");
          System.out.print(sdf.format(dt));? ? //輸出結果是:2005-2-19

          方法二:
          java.util.Date dt=null;
          DateFormat df=DateFormat.getDateInstance();
          dt=df.parse("2005-12-19");
          System.out.println(dt);? ? ? ? ? ? ? //輸出結果為:Mon Dec 19 00:00:00 CST 2005
          System.out.println(df.format(dt));? //輸出結果為:2005-2-19

          3.-----------------------------------------
          把字符串轉化為java.sql.Date
          字符串必須是"yyyy-mm-dd"格式,否則會拋出IllegalArgumentException異常
          java.sql.Date sdt=java.sql.Date.valueOf("2005-9-6");
          System.out.println(sdt);? ? ? ? ? //輸出結果為:2005-9-6

          4.-----------------------------------------
          TestApp.java

          public class TestApp {

          ? ? public static void main(String[] args) {
          ? ? ? ? System.out.println("Hello World!");
          ? ? ? ? Date d=new Date();
          ? ? ? ?
          ? ? ? ? //System.out.println(d.toLocaleString());
          ? ? ? ?
          ? ? ? ? //Calendar cld=Calendar.getInstance();
          ? ? ? ? System.out.println("Calendar.get(Calendar.DATE)"+(Calendar.getInstance().get(Calendar.DATE)));
          ? ? ? ?
          ? ? ? ? Date dt=new Date();//Date(103,-5,-6);
          ? ? ? ? System.out.println("getNowYear(Date dt)"+getYear(dt));
          ? ? ? ? System.out.println("getNowMonth(Date dt)"+getMonth(dt));
          ? ? ? ? System.out.println("getNowDate(Date dt)"+getDate(dt));
          ? ? }
          ? ?

          ? ? /**
          ? ? * 獲得當前日期的年份。
          ? ? * @return int 年份
          ? ? */
          ? ? public static int getNowYear(){
          ? ? ? ? return Calendar.getInstance().get(Calendar.YEAR);
          ? ? }

          ? ? /**
          ? ? * 獲得給定日期的年份
          ? ? * @param dt 給定日期
          ? ? * @return int 年份
          ? ? * @throws NullPointerException 如果參數年份為null,拋出異常。
          ? ? */
          ? ? public static int getYear(Date dt)throws NullPointerException{
          ? ? ? ? if(dt==null){
          ? ? ? ? ? ? throw new NullPointerException("日期參數為null");
          ? ? ? ? }else{
          ? ? ? ? ? ? Calendar cld=Calendar.getInstance();
          ? ? ? ? ? ? cld.setTime(dt);
          ? ? ? ? ? ? return cld.get(Calendar.YEAR);
          ? ? ? ? }
          ? ? }
          ? ?
          ? ? /**
          ? ? * 獲得當前日期的月份。
          ? ? * @return int 月份
          ? ? */
          ? ? public static int getNowMonth(){
          ? ? ? ? return 1+Calendar.getInstance().get(Calendar.MONTH);
          ? ? }
          ? ?
          ? ? /**
          ? ? * 獲得給定日期的月份
          ? ? * @param dt 給定日期
          ? ? * @return int 月份(1-12)
          ? ? * @throws NullPointerException 如果參數年份為null,拋出異常。
          ? ? */
          ? ? public static int getMonth(Date dt)throws NullPointerException{
          ? ? ? ? if(dt==null){
          ? ? ? ? ? ? throw new NullPointerException("日期參數為null");
          ? ? ? ? }else{
          ? ? ? ? ? ? Calendar cld=Calendar.getInstance();
          ? ? ? ? ? ? cld.setTime(dt);
          ? ? ? ? ? ? return 1+cld.get(Calendar.MONTH);
          ? ? ? ? }
          ? ? }
          ? ? /**
          ? ? * 獲得當前日期的當月的天數。
          ? ? * @return int 當月的天數
          ? ? */
          ? ? public static int getNowDate(){
          ? ? ? ? return 1+Calendar.getInstance().get(Calendar.DATE);
          ? ? }
          ? ?
          ? ? /**
          ? ? * 獲得給定日期的當月的天數
          ? ? * @param dt 給定日期
          ? ? * @return int 當月的天數
          ? ? * @throws NullPointerException 如果參數年份為null,拋出異常。
          ? ? */
          ? ? public static int getDate(Date dt)throws NullPointerException{
          ? ? ? ? if(dt==null){
          ? ? ? ? ? ? throw new NullPointerException("日期參數為null");
          ? ? ? ? }else{
          ? ? ? ? ? ? Calendar cld=Calendar.getInstance();
          ? ? ? ? ? ? cld.setTime(dt);
          ? ? ? ? ? ? return cld.get(Calendar.DATE);
          ? ? ? ? }
          ? ? }
          }

          posted @ 2006-04-06 16:39 zhaofei1394 閱讀(2087) | 評論 (0)編輯 收藏

          關于rs結果集

          ??? 用完ResultSet結果集后,一定要清空!不然它會把服務器拖死!
          ??? 結果集輸出時,就是一行的記錄也要用.next()方法。

          posted @ 2006-04-04 13:10 zhaofei1394 閱讀(273) | 評論 (0)編輯 收藏

          僅列出標題
          共2頁: 上一頁 1 2 

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 元阳县| 淄博市| 常熟市| 绥德县| 新巴尔虎左旗| 修文县| 皮山县| 石阡县| 浠水县| 兖州市| 乌海市| 常山县| 呼图壁县| 石阡县| 襄城县| 阜平县| 九龙坡区| 深泽县| 汝城县| 武山县| 盘锦市| 确山县| 甘德县| 和林格尔县| 龙川县| 浪卡子县| 武鸣县| 安多县| 循化| 鄄城县| 德昌县| 武宣县| 那坡县| 永城市| 水城县| 登封市| 翁牛特旗| 出国| 陇川县| 垦利县| 涞水县|