我写了一个类,里面有两个方法,一个是将日期转换成long型变量,这样插入库中没有数据库的格式限制,还有一个是将long型变量转换成日期格式的,请看:
package com.opensource.format;import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;/**
 * Created by IntelliJ IDEA.
 * User: OpenSource
 * Date: 2003-9-3
 * Time: 23:59:47
 */
public class DateFormat
{
public long DateToLong(Date date)
{
long longDate = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                  String strdate = sdf.format(date);
longDate = new Long(strdate).longValue(); return longDate;
} public String LongToString(long LongDate)
{
String strDate = null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss"); try
{
Date date = sdf2.parse(new Long(LongDate).toString());
strDate = sdf.format(date);
}
catch (ParseException pe)
{
System.out.println(pe.getMessage());
}
return strDate;
}
}更多内容请关注 http://www.opensource.com.cn/

解决方案 »

  1.   

    先用String.subtring();方法来取字符,然后再插入
      

  2.   

    这个也和你时间设定的类型有关吧,你现在应该是date类型的??如果是timestamp当然必须用20030930021200这样的格式了。解决方法同上所说的用substring()
      

  3.   

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    try{
        Date paseDate=sdf.parse(tDate);
        System.out.println("FormatDate is:"+paseDate);
    }catch(Exception e){
        System.out.println("ERROR:"+e.getMessage());
    }