public class SortMaoPao { /**
* @param b
* @return
* 网络字节转换成主机字节
*/
private static long ByteArraytoLong(byte[] b) {
long iOutcome = 0;
byte bLoop;
for (int i = 0; i < 8; i++) {
bLoop = b[i];
iOutcome += (bLoop & 0xff) << (8 * i);
}
return iOutcome;
}
public static void main(String[] args){
byte[] b=new byte[8];
b[0]=22;
b[1]=-126;
b[2]=67;
b[3]=26;
b[4]=109;
b[5]=125;
b[6]=-29;
b[7]=64;
Date mydate=new Date(ByteArraytoLong(b));
SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String etime=format.format(mydate);
System.out.println(etime);
}
}
得到时间为:1970-01-19 00:48:02,时间是错误的,时间应该是2009-04-12 09:XX:XX请教下,代码哪里有问题?
* @param b
* @return
* 网络字节转换成主机字节
*/
private static long ByteArraytoLong(byte[] b) {
long iOutcome = 0;
byte bLoop;
for (int i = 0; i < 8; i++) {
bLoop = b[i];
iOutcome += (bLoop & 0xff) << (8 * i);
}
return iOutcome;
}
public static void main(String[] args){
byte[] b=new byte[8];
b[0]=22;
b[1]=-126;
b[2]=67;
b[3]=26;
b[4]=109;
b[5]=125;
b[6]=-29;
b[7]=64;
Date mydate=new Date(ByteArraytoLong(b));
SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String etime=format.format(mydate);
System.out.println(etime);
}
}
得到时间为:1970-01-19 00:48:02,时间是错误的,时间应该是2009-04-12 09:XX:XX请教下,代码哪里有问题?
System.out.println(System.currentTimeMillis());
SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(format.format(new Date()));看下当前的时间就知道你的ByteArraytoLong函数的返回值不行
但是看起来好像是写错了
传输的网络字节,数值是:22,-126,67,26,109,125,-29,64
那么我就需要将网络字节转换成主机字节这个时候得到的时间值是:1970-01-19 00:48:02 显然这是错误的2边传输的字节数组值是对的,所以我就不明白问题出在哪了?
public static void main(String[] args){
long t = System.currentTimeMillis(); //得出当前系统时间的long值,并打印
System.out.println(t);
byteArray[0]=22;
byteArray[1]=-126;
byteArray[2]=67;
byteArray[3]=26;
byteArray[4]=109;
byteArray[5]=125;
byteArray[6]=-29;
byteArray[7]=64; long t1 = ByteArraytoLong(byteArray);
System.out.println(t1);
这样看来long肯定不是一样的,只有明天问问Delphi的那个人,
它的八个字节是什么,是不是和我的一样?如果他的字节和我接收的一样,但是他能得到正确的时间类型
而我不能的话,这个时候该如何处理呢?
iOutcome += ((long)(bLoop & 0xff)) << (8 * i);
那个long的强制转换不能少。
import java.text.*;
import java.util.*;public class Time {
static byte[] byteArray = new byte[8];
private static long ByteArraytoLong(byte[] b) {
long iOutcome = 0;
byte bLoop;
for (int i = 0; i <b.length; i++) {
bLoop = b[i];
iOutcome += ((long)(bLoop & 0x000000ff)) << (8 * i);
}
return iOutcome;
}
private static byte[] LongtoByte(long l){
for (int i=0; i<8; i++){
byteArray[i] = new Long(l & 0xFF).byteValue();
l >>= 8;
System.out.print(byteArray[i] +" ");
}
System.out.println();
return byteArray;
} public static void main(String[] args){
long t = System.currentTimeMillis(); //得出当前系统时间的long值,并打印
System.out.println(t);
LongtoByte(t); //转换成字符数组,数组的低位对应着long的低位 String str = Long.toBinaryString(t);
System.out.println(str);
long t1 = ByteArraytoLong(byteArray);
System.out.println(t1);
Date mydate=new Date(1239506503788l);
SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String etime=format.format(mydate);
System.out.println(etime);
}
}
结果:
1239508726819
35 52 122 -104 32 1 0 0
10010000010011000011110100011010000100011
1239508726819
2009-04-11 21:21:43
Date mydate=new Date(t1);
SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String etime=format.format(mydate);
System.out.println(etime);
所以按照你的意思来说,是我接收的数组错误,但是我是按照协议来的,
那到底是不是我的接收错误,只有明天才能知道,在这里先谢谢你
网络字节转换成主机字节 ByteArraytoLong(byte[] b) ,代码是错误的。
iOutcome += ((long)(bLoop & 0x000000ff)) << (8 * i);
需要强制转换成long类型,这样就对了justinavril:
接收过来的数组没有问题,我觉得问题可能是处在Delphi和Java的时间类型不一样
2009-04-13 09:06:09
Delphi 好像是Double类型的,转换后是: 39916.3792726389
Java 可以转换成long 转换后是:1239584802062所以觉得这里才是问题的所在
Delphi:
2009-04-13 09:16:35 转换成字节数组:
41
178
96
94
140
125
227
64
明显和Java的不一样
2:既然要转换成long的,那么你接受的数据的高低位就要弄清楚,我的意思是说你的b[0]是最高为还是最低位,这样移位运算出来以后就不会有问题了。
1:&运算符默认返回是int的,不会因为你多加0而变成其他的类型。既然是int的,4个字节。那么你移位的时候最多可能要左移56字节
2:你这样移位大于32字节的时候是错误的,用Math.pow()吧。
3:就像楼上所说的应该最后有几字节是0的,但你的数据转换以后是很大很大很大的年份。
4:我猜delphi和java可能得出的时间不是一个单位(delphi是微秒?java是毫秒),要不怎么会差这么多呢?要么就是数据错误?
由于两者的日期类型的起始日期不一样,即相同的“0”值在两者中表示不同的日期。那么在Java与Delphi之间进行日期值的通信时就需要进行一个转换。 -25,-43,-60,51,-114,125,-29,64
Delphi转换成double 是 39916.4438194444那么现在我只要把这个数组也转换成double得到 39916.4438194444然后在按照Delphi的时间方式解析
就可以得到正确的时间了帖子刚加了100分,希望大家在帮帮忙
然后这个毫秒数设为t1,那么现在的问题就是把1970-1-2和1899-12-30这两个时间段的毫秒数算出来,那么设为t2.这样我们需要的long型整数就是t1+t2.
Delphi转换成double 是 39916.4438194444 那么现在我只要把这个数组也转换成double得到 39916.4438194444然后在按照Delphi的时间方式解析
就可以得到正确的时间了
大家在帮帮忙
答:将字节数组在JAVA中转成Double,这很方便。如:楼主的数据:
-25,-43,-60,51,-114,125,-29,64 在JAVA中转成Double代码://按LITTLE-ENDIAN 方式读取b[],返回long
public static long htol(byte[] b)
{ long l=
(long)b[7]<<56L & 0xff00000000000000L |
(long)b[6]<<48L & 0x00ff000000000000L |
(long)b[5]<<40L & 0x0000ff0000000000L |
(long)b[4]<<32L & 0x000000ff00000000L |
(long)b[3]<<24L & 0x00000000ff000000L |
(long)b[2]<<16L & 0x0000000000ff0000L |
(long)b[1]<< 8L & 0x000000000000ff00L |
(long)b[0]<< 0L & 0x00000000000000ffL ;
return l;
}
//-25,-43,-60,51,-114,125,-29,64
byte[] bb1=new byte[8];
bb1[0]=(byte)-25;
bb1[1]=(byte)-43;
bb1[2]=(byte)-60;
bb1[3]=(byte)51;
bb1[4]=(byte)-114;
bb1[5]=(byte)125;
bb1[6]=(byte)-29;
bb1[7]=(byte)64; long ll=htol(bb1);
System.out.println("JAVA double:"+Double.longBitsToDouble(ll));
程序运行结果:
JAVA double:39916.443819444445
import java.text.*;public class TimeExchange {
final static long DAY_MILLS = 24*60*60*1000;
final static long HOUR_MILLS = 60*60*1000;
public static GregorianCalendar setTime(String str){
try{
SimpleDateFormat timeFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss");
Date date = timeFormat.parse(str);
date = new Date(date.getTime());
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
return calendar;
}catch (Exception e){
System.err.println("Error!");
}
return new GregorianCalendar(Locale.CHINA);
}
public static void main(String args[]){
double t1 = 39916.4438194444;
String str2 = "1899-12-30 00:00:00"; GregorianCalendar g2 = setTime(str2);
long l = g2.getTimeInMillis();
System.out.println(l);
String[] time = (t1+"").split("\\."); long day = new Long(time[0]);
long hour = new Long(time[1]);
long t2 = day*DAY_MILLS;// + hour*HOUR_MILLS;
System.out.println(t2);
long t = (long)(l + t2);
System.out.println(t);
System.out.println(System.currentTimeMillis());
}
}
结果:
-2209136400000
3448742400000
1239606000000
1239596141544
private static long ByteArraytoLong(byte[] b) {
long iOutcome = 0;
byte bLoop;
for (int i = 0; i < 8; i++) {
bLoop = b[i];
iOutcome += ((long) (bLoop & 0xff)) << (8 * i);
}
return iOutcome;
}
public static void main(String[] args) {
byte[] b=new byte[8];
b[0]=-25;
b[1]=-43;
b[2]=-60;
b[3]=51;
b[4]=-114;
b[5]=125;
b[6]=-29;
b[7]=64;
Long lon=ByteArraytoLong(b);
System.out.println(lon.doubleValue());
System.out.println(Double.longBitsToDouble(lon));
}
输出:
4.6757188878480722E18
39916.443819444445这样更是方便一点,让我惭愧的是,我开始是用lon.doubleValue()来转成double,
而不是用Double.longBitsToDouble()谢谢justinavril,jiangnaisong还有其他的热心帮助的人结贴去了