posts - 23,comments - 66,trackbacks - 0

          java中日期格式的處理

          找到兩篇文章,轉載原始出處已經不知道了,只能給出我看到的頁面。
          第一篇:http://www.kehui.net/html/article/36/36080.html
          /**
          * @author imagebear
          */

          日期問題
          1、獲取服務器端當前日期:

          <%@ page import="java.util.Date"%>
          <% Date myDate = new Date(); %>


          2、獲取當前年、月、日:


          <%@ page import="java.util.Date"%>
          <% Date myDate = new Date(); int thisYear = myDate.getYear() + 1900;//thisYear = 2003 int thisMonth = myDate.getMonth() + 1;//thisMonth = 5 int thisDate = myDate.getDate();//thisDate = 30 %>

          3、按本地時區輸出當前日期


          <%@ page import="java.util.Date"%>
          <% Date myDate = new Date(); out.println(myDate.toLocaleString()); %>


          輸出結果為:
          2003-5-30 (文章中這個錯了,我的試驗數據是:“2006-3-4 14:58:13”)
          4、獲取數據庫中字段名為”publish_time“、類型為Datetime的值


          <%@ page import="java.util.Date"%>
          <% ...連接數據庫... ResultSet rs = ... Date sDate = rs.getDate("publish_time"); %>

          5、按照指定格式打印日期

          <%@ page import="java.util.Date"%>
          <%@ page import="java.text.DateFormat"%>
          <% Date dNow = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz"); out.println("It is " + formatter.format(dNow)); %>

          輸出的結果為:
          It is 星期五 2003.05.30 at 11:30:46 上午 CST
          (更為詳盡的格式符號請參看SimpleDateFormat類,
          這里用DataFormat可以得到很好的日期格式,非常方便
          )
          6、將字符串轉換為日期

          <%@ page import="java.util.Date"%>
          <%@ page import="java.text.DateFormat"%>
          <% String input = "1222-11-11"; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date t = null; try{ t = formatter.parse(input); out.println(t); }catch(ParseException e) { out.println("unparseable using " + formatter); } %>

          輸出結果為:
          Fri Nov 11 00:00:00 CST 1222
          7、計算日期之間的間隔

          <%@ page import="java.util.Date"%>
          <%@ page import="java.text.DateFormat"%>
          <% String input = "2003-05-01"; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date d1 = null; try{ d1 = formatter.parse(input); }catch(ParseException e) { out.println("unparseable using " + formatter); } Date d2 = new Date(); long diff = d2.getTime() - d1.getTime(); out.println("Difference is " + (diff/(1000*60*60*24)) + " days."); %>

          輸出結果為:
          Difference is 29 days.
          8、日期的加減運算
          方法:用Calendar類的add()方法


          <%@ page import="java.util.*"%>
          <%@ page import="java.text.*"%>
          <% Calendar now = Calendar.getInstance(); SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz"); out.println("It is now " + formatter.format(now.getTime())); now.add(Calendar.DAY_OF_YEAR,-(365*2)); out.println(" "); out.println("Two years ago was " + formatter.format(now.getTime())); %>

          輸出結果為:
          It is now 星期五 2003.05.30 at 01:45:32 下午 CST
          Two years ago was 星期三 2001.05.30 at 01:45:32 下午 CST
          9、比較日期
          方法:用equals()、before()、after()方法

          <%@ page import="java.util.*"%>
          <%@ page import="java.text.*"%>
          <% DateFormat df = new SimpleDateFormat("yyy-MM-dd"); Date d1 = df.parse("2000-01-01"); Date d2 = df.parse("1999-12-31"); String relation = null; if(d1.equals(d2)) relation = "the same date as"; else if(d1.before(d2)) relation = "before"; else relation = "after"; out.println(d1 +" is " + relation + ' ' + d2); %>

          輸出結果為:
          Sat Jan 01 00:00:00 CST 2000 is after Fri Dec 31 00:00:00 CST 1999
          10、記錄一件事所花費的時間
          方法:調用兩次System.getTimeMillis()方法,求差值

          <%@ page import="java.text.*"%>
          <% long t0,t1; t0 = System.currentTimeMillis(); out.println("Cyc starts at " + t0); int k = 0; for(int i =0;i<100000;i++) t1 =" System.currentTimeMillis();">

          輸出結果為:
          Cyc starts at 1054275312432
          Cyc ends at 1054275312442
          This run took 10ms.

          其它:如何格式化小數

          <%@ page import="java.text.*"%>
          <% DecimalFormat df = new DecimalFormat(",###.00"); double aNumber = 33665448856.6568975; String result = df.format(aNumber); out.println(result); %>

          輸出結果為:
          33,665,448,856.66

          在網上經常看到有人問如何將 獲得當前時間并轉換成
          yyyy-MM-dd 年-月-日
          hh:mm:ss 小時-分鐘-秒
          yyyy-MM-dd HH:mm:ss 年-月-日 小時-分鐘-秒
          三種格式 下面就是 jsp GUI 的使用方法



          <%@ page import="com.Mamak.util.TimeString" %>
          <% //獲得當前日期時間
          String nowDate = TimeString.getNowTime("yyyy-MM-dd");
          String nowTime = TimeString.getNowTime("HH:mm:ss");
          String nowDateTime = TimeString.getNowTime("yyyy-MM-dd HH:mm:ss");
          out.println("nowDate: "+nowDate);
          out.println("nowTime: "+nowTime);
          out.println("nowDateTime: "+nowDateTime); %>
          //******************************************************
          //GUI 或java 小程序獲得得當前日期
          public class Test()
          {
          public static void main(String abc[])
          {
          //直接包名點類名點方法名使用
          System.out.println("nowDate: "+com.Mamak.util.TimeString.getNowTime("yyyy-MM-dd"));
          System.out.println("nowTime: "+com.Mamak.util.TimeString.getNowTime("HH:mm:ss"));
          System.out.println("nowDateTime: "+com.Mamak.util.TimeString.getNowTime("yyyy-MM-dd HH:mm:ss"));
          }
          }
          //******************************************************
          //獲得時間的bean 文件名 TimeString.java
          package com.Mamak.util;

          import java.text.SimpleDateFormat;
          import java.util.Calendar;

          public class TimeString
          {

          public TimeString()
          {
          }

          public static String getNowTime(String timeFormat)
          {
          SimpleDateFormat lformat = new SimpleDateFormat(timeFormat);
          Calendar now = Calendar.getInstance();
          String nowstr = lformat.format(now.getTime());
          return nowstr;
          }

          public static String getNotTime()
          {
          return getNowTime("yyyy-MM-dd");
          }
          }

          作者Blog:http://blog.csdn.net/stonewang/

          第二篇:http://www.heci.net/view.asp?id=198

          java日期格式轉換測試

          來源:www.heci.net | 時間:2005-8-10 17:49:24

          [更多內容請訪問 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 on 2006-03-11 18:24 rd2pm 閱讀(4366) 評論(1)  編輯  收藏

          FeedBack:
          # re: java中日期格式的處理
          2007-03-27 14:12 | s
          d  回復  更多評論
            

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


          網站導航:
           

          主站蜘蛛池模板: 剑川县| 桂平市| 荔浦县| 锡林郭勒盟| 汶川县| 朝阳市| 九江县| 怀仁县| 忻城县| 蓬安县| 普定县| 华坪县| 富民县| 桑日县| 淮阳县| 浠水县| 灯塔市| 循化| 崇明县| 新平| 获嘉县| 日喀则市| 永兴县| 兰坪| 兰西县| 南江县| 额敏县| 肇州县| 盈江县| 华池县| 桐乡市| 芦溪县| 普宁市| 镇雄县| 酒泉市| 平塘县| 聂荣县| 新田县| 太仓市| 托克托县| 云和县|