Jody

           

          java日期格式轉(zhuǎn)換測試

          java日期格式轉(zhuǎn)換測試

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

          [更多內(nèi)容請訪問 http://www.heci.net 賀詞網(wǎng)!]
          java日期格式轉(zhuǎn)換測試

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

          支持一鳴,就點(diǎn)一下↓

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

          1.-----------------------------------------
          得到系統(tǒng)當(dāng)前時間:

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

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

          2.-----------------------------------------
          把字符串轉(zhuǎn)化為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));? ? //輸出結(jié)果是:2005-2-19

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

          3.-----------------------------------------
          把字符串轉(zhuǎn)化為java.sql.Date
          字符串必須是"yyyy-mm-dd"格式,否則會拋出IllegalArgumentException異常
          java.sql.Date sdt=java.sql.Date.valueOf("2005-9-6");
          System.out.println(sdt);? ? ? ? ? //輸出結(jié)果為: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));
          ? ? }
          ? ?

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

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

          posted on 2006-04-06 16:39 zhaofei1394 閱讀(2088) 評論(0)  編輯  收藏 所屬分類: Java語言基礎(chǔ)

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 丘北县| 新建县| 绥中县| 新乡县| 拉孜县| 赣州市| 禄丰县| 皮山县| 牡丹江市| 舞钢市| 瑞金市| 休宁县| 紫云| 岳阳市| 张家港市| 留坝县| 沅陵县| 扶余县| 清新县| 桦南县| 普安县| 泗洪县| 荆州市| 晋城| 浦东新区| 花莲市| 双鸭山市| 津市市| 蓬溪县| 潞西市| 轮台县| 大名县| 宜春市| 上蔡县| 阳西县| 休宁县| 曲阳县| 贺兰县| 临潭县| 泸溪县| 东港市|