javaSocket与C通信 (三)

2014-11-24 10:31:17 · 作者: · 浏览: 2
各个子类中需要重新实现
//必须有返回值 因为调用者需要根据返回值做一些操作
//这里的参数 buf是需要填写的缓冲区 start 是缓冲区开始位置 len 是可用的缓冲区长度
public int encode(byte[] buf,int start,int len)
{
return 0;
}
//这里的参数 buf是需要需要解码的缓冲区 start 是缓冲区开始位置 len 是需要解码的长度
public int decode(byte[] buf,int start,int len)
{
return 0;
}

public void getStreamID()
{
Calendar ca = Calendar.getInstance();
int year = ca.get(Calendar.YEAR);//获取年份
int month=ca.get(Calendar.MONTH)+1;//获取月份
int day=ca.get(Calendar.DATE);//获取日
int minute=ca.get(Calendar.MINUTE);//分
int hour=ca.get(Calendar.HOUR);//小时
int second=ca.get(Calendar.SECOND);//秒
int am_pm=ca.get(Calendar.AM_PM);
if (am_pm==Calendar.PM)
hour += 12;
if (hour>=24)
hour -= 24;

System.out.println(seq);

if (lastCa!=ca)
{
lastCa = ca;
seq = 12;
}
else
{
seq++;
if (seq>=100)
seq = 0;
}

//现在根据上面的字段组成StreamID字符串
//目前先使用手工的办法来写,效率高一些
StreamID[0] = (byte)(year/1000+'0');
StreamID[1] = (byte)((year-(StreamID[0]-'0')*1000)/100+'0');
StreamID[2] = (byte)((year-(StreamID[0]-'0')*1000-(StreamID[1]-'0')*100)/10+'0');
StreamID[3] = (byte)(year-(StreamID[0]-'0')*1000-(StreamID[1]-'0')*100-(StreamID[2]-'0')*10+'0');

StreamID[4] = (byte)(month/10+'0');
StreamID[5] = (byte)((month-(StreamID[4]-'0')*10)+'0');

StreamID[6] = (byte)(day/10+'0');
StreamID[7] = (byte)((day-(StreamID[6]-'0')*10)+'0');

StreamID[8] = (byte)(hour/10+'0');
StreamID[9] = (byte)((hour-(StreamID[8]-'0')*10)+'0');

StreamID[10] = (byte)(minute/10+'0');
StreamID[11] = (byte)((minute-(StreamID[10]-'0')*10)+'0');

StreamID[12] = (byte)(second/10+'0');
StreamID[13] = (byte)((second-(StreamID[12]-'0')*10)+'0');

StreamID[14] = (byte)(seq/10+'0');
StreamID[15] = (byte)((seq-(StreamID[14]-'0')*10)+'0');

System.out.println("现在时间");
System.out.println("用Calendar.getInstance().getTime()方式显示时间: " + ca.getTime());
System.out.println("用Calendar获得日期是:" + year +"年"+ month +"月"+ day + "日");
System.out.println("用Calendar获得时间是:" + hour +"时"+ minute +"分"+ second +"秒");


}


public static void main(String[] args)
{
{
newsDetail nn1 = new newsDetail();
}
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}