static long nanoTime() 
          Returns the current value of the most precise available system timer, in nanoseconds. 这个方法得到long值如何生成一个Date?下面的使用方法显然不对。import java.util.*;
public class test
{
public static void main(String[] args){
Calendar cal1=Calendar.getInstance();
cal1.setTimeInMillis(System.nanoTime());
Date d=cal1.getTime();
System.out.println("nanoTime:"+d.toLocaleString()); Calendar cal2=Calendar.getInstance();
cal2.setTimeInMillis(System.currentTimeMillis());
Date d2=cal2.getTime();
System.out.println("currentTimeMillis:"+d2.toLocaleString());
}
};---------- java ----------
nanoTime:2067-2-23 18:18:51
currentTimeMillis:2006-4-12 0:55:01输出完成 (耗时: 0 秒) - 正常终止

解决方案 »

  1.   

    public static long nanoTime()    Returns the current value of the most precise available system timer, in nanoseconds.    This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.    For example, to measure how long some code takes to execute:       long startTime = System.nanoTime();
           // ... the code being measured ...
           long estimatedTime = System.nanoTime() - startTime;
             Returns:
            The current value of the system timer, in nanoseconds.
        Since:
            1.5
      

  2.   

    nanoTime
    public static long nanoTime()返回最准确的可用系统计时器的当前值,以毫微秒为单位。 
    此方法只能用于测量已过的时间,与系统或钟表时间的其他任何时间概念无关。返回值表示从某一固定但任意的时间算起的毫微秒数(或许从以后算起,所以该值可能为负)。此方法提供毫微秒的精度,但不是必要的毫微秒的准确度。它对于值的更改频率没有作出保证。在取值范围大于约 292 年(263 毫微秒)的连续调用的不同点在于:由于数字溢出,将无法准确计算已过的时间。 例如,测试某些代码执行的时间长度:    long startTime = System.nanoTime();
       // ... the code being measured ...
       long estimatedTime = System.nanoTime() - startTime;
     
    返回:
    系统计时器的当前值,以毫微秒为单位。
    从以下版本开始: 
    1.5
      

  3.   

    试试下面的代码吧,我自己写的,你把要测试的加到两个时间之间就可以了。测试级别为毫秒级。import java.text.*;
    import java.util.*;
    public class teststr{

    public static String str1="abc";
    public static String str2="xyz";

    public static void main(String args[]){

    System.out.println(new Date()+""); Date date = new Date();
    long mills = date.getTime();


    System.out.println(mills+"millsercons");

    //int i =str1.indexOf(str2); //20
    String[] tt=str1.split(str2);//109

    Date date2 = new Date();
    long millsec = date2.getTime();

    System.out.println(millsec+"millsercons");
    System.out.println(str1.length()+"");
    }
    }