我想实现这样的功能:问题用红色表示了
1)取当前时间(精确到毫秒)t1:我用  public static native long currentTimeMillis() 对吗?
2)把t1转换成BYTE进行网络传输  问: 如何转?
3)接收刚才的数据包(BYTE),如何转化成时间(精确到毫秒)?
4)取当前时间t1,计算t2-t1 得到差值:毫秒,如何做?总结一下:  long BYTE如何互转?  精确到毫秒的时间如何计算差值?                                谢谢大家!

解决方案 »

  1.   

    long转 byte        调用方法byteValue( )
    byte转long         调用方法longValue( ) 
      

  2.   

            long t1 = System.currentTimeMillis();
            byte[] tb = String.valueOf(t1).getBytes();  //long转byte
            long tp = Long.parseLong(new String(tb));   //byte转long
            //currentTimeMillis返回的都是long型,而且是精确到毫秒的,直接 - 就行了
      

  3.   

    1.对
    2.正向过程   long ll = System.currentTimeMillis();
      String temp = Long.toString(ll);
      byte[] bytes = temp.getBytes();3.反向过程   String temp2 = new String(bytes);
      long ll2 = Long.parseLong(temp2);4.long l3 = System.currentTimeMillis();
      long sub = l3-ll2;
      

  4.   

    1、System.currentTimeMillis(); 或者new Date().getTime();  
       返回:
              当前时间与 1970 年 1 月 1 日午夜之间的时间差(以毫秒为单位测量)。4、 Date t1,Date t2,     差值:毫秒: t2.getTime()-t1.getTime();
      

  5.   

    给楼主一点建议..不一定非要用ByteArrayInputStream去写吧..DataInputStream也是可以的..而且什么都可以写.
      

  6.   

    应该也可以不用转,把网络传输的
    outputstream 封装成dataoutputstream,这样就可以直接用writelong写入long类型,
    inputstream封装成datainputstream,这样就可以直接用readlong读long类型
      

  7.   

    1、currentTimeMillis 返回以毫秒为单位的当前时间
    2、 public final static byte[] getBytes(long s, boolean asc) {
        byte[] buf = new byte[8];
        if (asc)
          for (int i = buf.length - 1; i >= 0; i--) {
            buf[i] = (byte) (s & 0x00000000000000ff);
            s >>= 8;
          }
        else
          for (int i = 0; i < buf.length; i++) {
            buf[i] = (byte) (s & 0x00000000000000ff);
            s >>= 8;
          }
        return buf;
      }3、public final static long getLong(byte[] buf, boolean asc) {
        if (buf == null) {
          throw new IllegalArgumentException("byte array is null!");
        }
        if (buf.length > 8) {
          throw new IllegalArgumentException("byte array size > 8 !");
        }
        long r = 0;
        if (asc)
          for (int i = buf.length - 1; i >= 0; i--) {
            r <<= 8;
            r |= (buf[i] & 0x00000000000000ff);
          }
        else
          for (int i = 0; i < buf.length; i++) {
            r <<= 8;
            r |= (buf[i] & 0x00000000000000ff);
          }
        return r;
      }4、既然t1、t2都是通过currentTimeMillis取到的以毫秒为单位的时间,差值也就是毫秒
      

  8.   

    1 我创建数据包的语句是:packet   =   new   DatagramPacket(sendbyte,   sendbyte.length,   ia,   Port); 
      没有用ByteArrayInputStream或DataInputStream 
    2 大家的建议多数和2楼相似,问:下面语句返回几个byte?
              byte[] tb = String.valueOf(t1).getBytes();  //long转byte 
      
    3 7楼的方法似乎不同,没有先转string,效率会高一些吗?
      

  9.   

    另外:1楼的方法
    long转 byte        调用方法byteValue( ) 
    byte转long        调用方法longValue( ) 
    编译是提示:test.java:13: long cannot be dereferenced
      byte b1 = L1.byteValue();
                  ^
    test.java:16: byte cannot be dereferenced
      L2 = b1.longValue();
             ^
    2 errors
      

  10.   


    1.不知道你用的是datagrampacket,以为你用的是socket呢
    2.byte数组大小,可以tb.length得到呀
    3.会高一点点,但如果你不是大型应用的话,而只是自己的一个例子的话,无所谓
      

  11.   

    我用7楼的方法有问题:
    test.java:
    import java.io.*;
    import java.lang.*;
    import java.net.*;public class test
    {

    public final static byte[] getBytes(long s, boolean asc) { 
        byte[] buf = new byte[8]; 
        if (asc) 
          for (int i = buf.length - 1; i >= 0; i--) { 
            buf[i] = (byte) (s & 0x00000000000000ff); 
            s >>= 8; 
          } 
        else 
          for (int i = 0; i < buf.length; i++) { 
            buf[i] = (byte) (s & 0x00000000000000ff); 
            s >>= 8; 
          } 
        return buf; 
      }   public final static long getLong(byte[] buf, boolean asc) { 
        if (buf == null) { 
          throw new IllegalArgumentException("byte array is null!"); 
        } 
        if (buf.length > 8) { 
          throw new IllegalArgumentException("byte array size > 8 !"); 
        } 
        long r = 0; 
        if (asc) 
          for (int i = buf.length - 1; i >= 0; i--) { 
            r <<= 8; 
            r |= (buf[i] & 0x00000000000000ff); 
          } 
        else 
          for (int i = 0; i < buf.length; i++) { 
            r <<= 8; 
            r |= (buf[i] & 0x00000000000000ff); 
          } 
        return r; 
      }  
     public test()
     {
      long L1,L2;
      //L1 = 1234567;
      L1 = System.currentTimeMillis(); ;
      System.out.println(L1);
      
      //用2楼方法
      byte[] tb = String.valueOf(L1).getBytes();
      System.out.println(tb.length); 
      L2 = Long.parseLong(new String(tb)); 
      System.out.println(L2); 
      
      //用7楼的方法
      byte[] tb2  = getBytes(L1,true);
      System.out.println(tb2.length);
      L2 = getLong(tb2,true); 
      System.out.println(L2); 
      
     }
     
     public static void main(String args[])
     {
       new test();
     }
    }结果:
    121255097685913
    1212550976859
    8
    6599369193798303744
      

  12.   

    2楼的改成这样好像好使: 
    public static long bytes2long(byte[] b)
      {
             
              int mask=0xff;
              long temp=0;
              long res=0;
              for(int i=0;i<8;i++){
                  res<<=8;
                  temp=b[i]&mask;
                  res|=temp;
              }
             return res;
      } public static byte[] long2bytes(long num)
    {
            byte[] b=new byte[8];
            for(int i=0;i<8;i++){
                 b[i]=(byte)(num>>>(56-i*8));
            }
           return b;
    }