我在oracle数据库中有一个timestamp字段,我现在将值变为字符串取出来 格式化为'yyyy-MM-dd hh24:mm:ssxff'得到如下字符串“2010-07-06 16:07:27.707000”,现在我想在java中将这个字符串转为精确到毫秒的date怎么做?不能出现精度丢失

解决方案 »

  1.   


    public static Long getTimeStemp(String str){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date d = null;
    try {
    d = sdf.parse(str);
    } catch (ParseException e) {
    e.printStackTrace();
    }
    return d.getTime();
    }
      

  2.   


    public static Long getTimeStemp(String str){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.FFF");
    Date d = null;
    try {
    d = sdf.parse(str);
    } catch (ParseException e) {
    e.printStackTrace();
    }
    return d.getTime();
    }
      

  3.   

    从string转为timestamp 再从timestamp到string 这里有问题啊 来回转换的值不同

      nowstp = nowtisp.getTimestamp("lasttime");//nowstp=2010-07-07 09:32:48.269
    s=sdf.format(nowstp);//s=2010-07-07 09:32:48.001
    为何来回转换会是毫秒级别的数据产生错误?
      

  4.   

    字母  日期或时间元素  表示  示例  
    G  Era 标志符  Text  AD  
    y  年  Year  1996; 96  
    M  年中的月份  Month  July; Jul; 07  
    w  年中的周数  Number  27  
    W  月份中的周数  Number  2  
    D  年中的天数  Number  189  
    d  月份中的天数  Number  10  
    F  月份中的星期  Number  2  
    E  星期中的天数  Text  Tuesday; Tue  
    a  Am/pm 标记  Text  PM  
    H  一天中的小时数(0-23)  Number  0  
    k  一天中的小时数(1-24)  Number  24  
    K  am/pm 中的小时数(0-11)  Number  0  
    h  am/pm 中的小时数(1-12)  Number  12  
    m  小时中的分钟数  Number  30  
    s  分钟中的秒数  Number  55  
    S  毫秒数  Number  978  
    z  时区  General time zone  Pacific Standard Time; PST; GMT-08:00  
    Z  时区  RFC 822 time zone  -0800  
      

  5.   

    package com.xuz.csdn.july07;import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;public class SimpleDateFormatTest { public static void main(String[] args) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");

    try {
    Date date = sdf.parse("2010-07-07 01:00:00.123");
    System.out.println(sdf.format(date));
    } catch (ParseException e) {
    e.printStackTrace();
    }

    }}