如何将String date=“2008-01-01”
转换为时间类型,因为我数据库中的字段是DateTime类型的

解决方案 »

  1.   

    private static final String  FORMAT_1 = "yyyy-MM-dd";
    private static final String  FORMAT_2 = "yyyy-MM-dd HH:ss"; //24小时至
    private static final String  FORMAT_1 = "yyyy-MM-dd hh:ss"; //12小时至....................................
    date = formatDate(date, FORMAT_1, FORMAT_2);private static String formatDate(String inputDate, String inputDateFormat, String outputDateFormat) throws Exception {
    if (CL0Util.isNullString(inputDate)) {
    return CL0Util.getSpace(0);
    }
    return formatDate(getDate(inputDate, inputDateFormat), outputDateFormat);
    }public static String formatDate(Date inputDate, String outputFormat) {
    if (null == inputDate) {
    return null;
    } if (null == outputFormat) {
    throw new NullPointerException("The output format is null.");
    } try {
    SimpleDateFormat sdf = new SimpleDateFormat(outputFormat); return sdf.format(inputDate);
    } catch (Exception ex) {
    throw new IllegalArgumentException("The output format is invalid.");
    }
    }public static Date getDate(String inputDateTime, String inputFormat) {
    if (null == inputDateTime) {
    return null;
    } if (null == inputFormat) {
    throw new NullPointerException("The input format is null.");
    }

    try {
    SimpleDateFormat sdf = new SimpleDateFormat(inputFormat);
    return sdf.parse(inputDateTime);
    } catch (Exception ex) {
    throw new IllegalArgumentException("The input format is invalid.");
    }
    }
      

  2.   

    oracle中有to_date函数,
    或者你自己转换成java.sql.Date再使用setDate....需要时间的话需要Timestamp
      

  3.   

    用java.text.SimpleDateFormat就可以
      

  4.   


    String begin="2008-03-10";
    java.text.SimpleDateFormat localTime   =    new   java.text.SimpleDateFormat("yyyy-MM-dd");
       Date newbegin = localTime.parse(begin);