java.util.Date dateandtime= new java.util.Date();
dateandtime= new java.sql.Date(dateandtime.getTime());
得出的只是yy-mm-dd形式的,我想要yy-mm-dd hh-mm-ss形式,该如何实现?(dateandtime要是date类型的,不要变为String)

解决方案 »

  1.   


    System.out.println((new java.util.Date()).toLocaleString());//这个可以满足你的要求或者使用java.util.Date()类的getYear(),getMonth(),getDay(),getHours,getMinutes(),getSeconds()方法
      

  2.   


    Date类型本身就有时分秒,根本就没必要有你说的这种形式,所谓的yy-mm-dd形式只是在显示时的格式调整。
      

  3.   


    Date d = new Date();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    System.out.println(df.format(d));
      

  4.   

    SimpleDateFormat.java
    import java.text.SimpleDateFormat;
    import java.util.Date;public class SimpleDateExample {
    public static void main(String[] args) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //A
    Date dt = sdf.parse("2009-8-30");    //B
    System.out.println("英文格式:"+dt);
    System.out.println();
    sdf.applyPattern("yyyy年MM月dd日");    //C
    String str=sdf.format(dt);     //D
    System.out.println("中文格式:"+str);
    }
    }
      

  5.   

    LZ你有什么需要把date显示成那个样子。?有必要吗?你只是为了显示。或存到数据库里。只需要string 或 date类型就行了。你说的打印出来的样子。,那都是String.                Date d = new Date();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    System.out.println(df.format(d));

    System.out.println("转回date >>> " + df.parse(df.format(d)));
      

  6.   

    /** 
    * @see 获得指定时间格式 
    * @param Date 
    *            date 时间 
    * @param String 
    *            format 时间格式 
    * @return String dateStr 返回获得相应格式时间的字符串 
    */ 
    public static String getDateByFormat(Date date, String format) { 
    String dateStr = ""; 
    try { 
    if (format != null) { 
    SimpleDateFormat simFormat = new SimpleDateFormat(format, 
    Locale.CHINA); 
    dateStr = simFormat.format(date); 

    } catch (Exception e) { 
    e.printStackTrace(); 

    return dateStr; 
      

  7.   


    java.sql.Date是针对SQL语句使用,只包含日期无时间部分。
    sql.date一般是在数据库的时间字段
    util.date一般是日常日期字段
    相互转换:java.sql.Date date = new Java.sql.Date();
          java.util.Date d = new Java.util.Date(date.getTime());
    格式转化:
    Date d = new Date();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    df.format(d)JSP页面显示可以用format标签
         <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
         <fmt:formatDate value="${dateandtime} pattern="yyyy-MM-dd HH:mm:ss" />
      或struts2标签:
         <%@ taglib prefix="s" uri="/struts-tags" %>
         <s:date name="startTime" format="yyyy-MM-dd HH:mm:ss" />
      

  8.   

    时间格式化,用simpleDateFormat就行的了