getDate();
getYear();
getMonth(); 重新组合

解决方案 »

  1.   

    你要采用日期标准--日期格式化
    在JSP文件里输入
    <%@ page import="java.text.*"%>
    <%
        Locale locale=new Locale("zh","CN");
        Date date=new Date();
        String smalldatetime=new String();
        smalldatetime=DateFormat.getDateInstance(DateFormat.SHORT,locale).format(date));
        out.println("Date="+smalldatetime);
    %>
    这样smalldatetime就是只有日期了。
      

  2.   


    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-");
    String mDateTime1=formatter.format(d);//d就是时间
      

  3.   

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String mDateTime1=formatter.format(d);//d就是时间
      

  4.   

    可是我已经用一个String类型的变量来保存那个smalldatetime的值,现在怎么从String型的变量中获得smalldatetime中的年,月,日?我试过用substring()不行!!!!
      

  5.   

    如果java.util.date smalldatetime
    用java.sql.date da = new (smalldatetime);
      

  6.   

    java.util.Date  now= new java.util.Date();
      SimpleDateFormat formatter=new SimpleDateFormat ("yyyyMMdd");
      String  Get_date=formatter.format(now);
      String modtime=Get_date.substring(0,4)+"-"+Get_date.substring(4,6)+"-"+Get_date.substring(6,8);
    我把modtime的值放入数据库中的一个表,字段名叫MODTIME,然后我从表中把这个MODTIME对应的值取出,放入String tempmodtime中,tempmodtime=rs.getString("MODTIME");显示出来的是“2003-9-29 00:00:00"
    现在我只想要年,月,日,怎么办?
      

  7.   

    数据库的字段类型用Timestamp,用java.sql.Date tempmodtime = rs.getDate("MONTIME")读取,需要字符串的话,tempmodtime.toString()
    如果数据库字段类型不可改:
    char[] tempchar={'a'};
    tempmodtime.getChars(0, 10, tempchar, 0);
    String da = mew(tempchar, 0, 10);
    da中就是你想要的
      

  8.   

    如果是oracle,直接用to_char很好解決這個問題
      

  9.   

    请问djws:
    String da = mew(tempchar, 0, 10);
    这条语法对吗?
      

  10.   

    抱歉,写错了,应是
    String da = new String(tempchar, 0, 10);
    不过,我刚注意到,你的日期字符串月份和日期不会自动补零,程序改一下<%
    String tempmodtime = "2003-5-11 00:00:00";
    char[] ch = {'a','a'};
    int i = 0;
    StringBuffer sb = new StringBuffer(tempmodtime);
    while(ch[0] != ' ') {
    tempmodtime.getChars(i, i+1, ch, 0);
    i ++;
    }
    sb.delete(i-1, tempmodtime.length());
    String str = new String(sb);  //现在str中是日期字串
    sb.append('-');
    String[] ymd = {" "," "," "};  
    int end = 0;
    int index = 0;
    Integer strint = new Integer(1);
    int[] ymdint = {0, 0, 0};
    for(i=0; i<=str.length(); i++) {
        sb.getChars(i, i+1, ch, 0);
        if(ch[0] == '-') {
            //out.print("a<br>"+i+"<br>a");
    ymd[index] = new String(sb.substring(end, i));
            strint = new Integer(ymd[index]);
    ymdint[index] = strint.intValue();
    index ++;
            end = i+1;
        }
    }
    for(i=0; i<3; i++) {                   //测试用
    out.print(ymd[i]+"<br>");
    out.print(ymdint[i]);
    out.print("<br>");
    }
    %>你想要的都有了
    方法很笨,不过我没别的办法,我用的是mysql,对这类东西都用Timestamp保存,读取时用rs.getDate("xxx")得到日期,用rs.getTime("xxx")得到时间。
    另:字符串格式不对会出错的,用try把上面程序包起来比较好。