<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    I'll be back!

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

    JAVA中日期格式轉(zhuǎn)換

    Posted on 2006-09-25 10:34 zolly 閱讀(9198) 評論(0)  編輯  收藏
    /**
    ???*?字符串轉(zhuǎn)換為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?格式轉(zhuǎn)換為字符串格式'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?格式轉(zhuǎn)換為字符串格式'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?任意整數(shù)如: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;
    ??}

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

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


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

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

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

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

    ??}

    ??
    /**
    ???*取系統(tǒng)給定時間:返回值為如下形式
    ???*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中的日期格式不一樣,怎樣統(tǒng)一?


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

    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型


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

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

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

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

    //對日期格式的轉(zhuǎn)換成("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://m.tkk7.com/qclass/archive/2006/09/24/71589.html)

    ????????GregorianCalendar?cal??=???new??GregorianCalendar();
    ????????Date?now??
    =???new??Date();
    ????????cal.setTime(now);
    ????????cal.setFirstDayOfWeek(GregorianCalendar.MONDAY);???
    //?設(shè)置一個星期的第一天為星期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://m.tkk7.com/galaxyp/archive/2006/04/13/40826.html

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


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 亚洲av色香蕉一区二区三区蜜桃| 一二三四免费观看在线电影| 亚洲AV综合色区无码一二三区| 亚洲国产成人高清在线观看| 又粗又硬又黄又爽的免费视频 | 91嫩草免费国产永久入口| 一级做a爰片久久免费| 亚洲欧美国产国产一区二区三区 | 97免费人妻无码视频| 嫩草在线视频www免费看| 未满十八私人高清免费影院| 久久久久亚洲国产AV麻豆| 亚洲精品456人成在线| 4480yy私人影院亚洲| 亚洲AV乱码一区二区三区林ゆな | 亚洲精品动漫免费二区| 久久精品国产免费观看| 岛国精品一区免费视频在线观看 | 99无码人妻一区二区三区免费 | 91亚洲国产成人精品下载| 亚洲午夜久久久久妓女影院| 青青青国产色视频在线观看国产亚洲欧洲国产综合 | 牛牛在线精品观看免费正| 亚洲第一成年免费网站| 天堂亚洲国产中文在线| 亚洲综合久久一本伊伊区| 亚洲成年人电影在线观看| 亚洲精品国产福利片| 亚洲黄色在线网站| 久久久国产精品亚洲一区| 亚洲好看的理论片电影| 黄页网站在线观看免费高清| 免费国产美女爽到喷出水来视频| 国产在线观看免费观看不卡| 1000部拍拍拍18免费网站| 91视频免费网址| 麻豆高清免费国产一区| 69视频在线观看高清免费| 91香焦国产线观看看免费| 美女视频黄的全免费视频| 18禁超污无遮挡无码免费网站国产|