工具方法

2019-03-06  本文已影响0人  懒眉

先说明一些用到的小知识,变化过程以代码说明。

short i = 10;
short j = -10;
//右移操作
short a =(short) ( i >> 2);
//10->(二进制)0000000000001010->(右移2位)0000000000000010->(10进制)2
short b =(short) ( j >> 2);
//-10->(二进制)1000000000001010->(补码-反码+1)1111111111110110->(右移2位)1111111111111101->(-1 反码)100000000000011->(10进制)-3
//左移操作
short c = (short)( i << 2);
//10->(二进制)0000000000001010->(左移2位)0000000000101000->(10进制)40
short d = (short)( j << 2);
//-10->(二进制)1000000000001010->(补码,反码+1)1111111111110110->(左移2位)1111111111011000->(-1 反码 )1000000000101000->(10进制)-40
byte i = 10;
byte j = -10;
short t = 0xff;
byte a = (byte)(i&t);
//10(10)->00001010(2)
//FF(16)->0000000011111111(2)
//00001010&0000000011111111->0000000000001010(2)->10(10)
short b = (short)(j&t);
//-10(10)->00001010(2)->11110110(补码)
//FF(16)->0000000011111111(2)
//11110110&0000000011111111->0000000011110110(2)->246(10)

很明显,-10二进制补码为11110110&0xFF之后的原码为0000000011110110。所以此操作的效果就是保留了负数的二进制补码,便于后续的使用和计算。

时间

public class DateTool {

    public static String[] presuppose = {"yyyy","MM","dd","HH","mm","ss","SSS"};
    
    public static void main(String[] args) {
        //当前时间的各种格式
        {
            Date date = new Date();
            String patten = "yyyy/MM/dd HH:mm:ss";
            SimpleDateFormat df = new SimpleDateFormat(patten);
            String returnValue = df.format(date);
            System.out.println(returnValue);
        }
        
        //时间戳转对象
        {
            //得到时间对象
            long d = System.currentTimeMillis();
            Date date = new Date(d);
            System.out.println(date);
        }
        
        //时间字符串根据格式转时间对象
        {
            String d = "20190130144025";
            String patten = "yyyyMMddHHmmss";
            SimpleDateFormat df = new SimpleDateFormat(patten);
            try {
                Date date = df.parse(d);
                System.out.println(date);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        //当前时间往前,往后
        {
            Date date = new Date();
            //当前时间
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            //后挪3天
            cal.add(Calendar.DATE, 3);
            //新的时间
            Date newDate = cal.getTime();
            String patten = "yyyy/MM/dd HH:mm:ss";
            SimpleDateFormat df = new SimpleDateFormat(patten);
            String returnValue = df.format(newDate);
            System.out.println(returnValue);
        }
        
        //计算距今天数
        {
            
            Date date;
            try {
                String d = "20190129144025";
                String patten = "yyyyMMddHHmmss";
                SimpleDateFormat df = new SimpleDateFormat(patten);
                date = df.parse(d);
                
                long t = Calendar.getInstance().getTime().getTime();
                Calendar c = Calendar.getInstance();
                c.setTime(date);
                long t1 = c.getTime().getTime();
                int day =  (int) (t / 1000 - t1 / 1000) / 3600 / 24;
                System.out.println("距今" + day +"天");
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
        }
        //时间串变时间戳
        {
            String d = "20190129144025";
            String patten = "yyyyMMdd";
            try {
                long timestamp = new SimpleDateFormat(patten).parse(d).getTime();
                System.out.println("时间戳:" + timestamp);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        //比较时间
        {
            String s = "20190129144025";
            String e = "20190129184025";
            String patten = "yyyyMMddHHmmss";
            SimpleDateFormat df = new SimpleDateFormat(patten);
            try {
                Date sd = df.parse(s);
                Date ed = df.parse(e);
                if(sd.getTime() > ed.getTime()){
                    System.out.println("sd比较大");
                }else{
                    System.out.println("ed比较大");
                }
                
                if(sd.before(ed)){
                    System.out.println("sd比较小");
                }else{
                    System.out.println("ed比较小");
                }
                
            } catch (ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
}

位图

image.png

位图目前我所知道是用于ISO8583协议的数据传输。根据位图可以知道哪些数据域有数据需要解析。下面提供64域的生成位图和解析位图的方法,如果是128域的,需要稍微扩展一下。

public class BitMapTool {
    
    static byte[] mask = new byte[128];
    static {
        for (int i = 0; i <= 9; i++)
        {
            mask[i + 48] = (byte) i;
        }
        for (int i = 0; i <= 5; i++)
        {
            mask[i + 97] = (byte) (10 + i);
        }
        for (int i = 0; i <= 5; i++)
        {
            mask[i + 65] = (byte) (10 + i);
        }
    }
    
    
    static String bitmap(int... vs){
        //第一步  将域序号塞入byte域组中,每个byte标识一个域,此方法用于64域数据
        byte[] map = new byte[64];
        for (int i = 0; i < vs.length; i++)
        {
            if (0 != vs[i])
            {
                int b = vs[i];
                map[b-1] = 1;
            }
        }
        
        StringBuffer buff = new StringBuffer();
        //64个字节分为8块,每块的8个字节转为16进制hex
        for(int j = 0 ; j < map.length/8 ; j++){
            byte[] temp = new byte[8];
            System.arraycopy(map, j*8, temp, 0, 8);
            StringBuffer sb = new StringBuffer();
            for (byte b : temp) {
                sb.append(b);
            }
            byte ret = (byte) (Integer.valueOf(sb.toString(), 2)  & 0xff);
            StringBuffer f = new StringBuffer();
            int n = ret & 0xff;
            if (n < 0x10)
                f.append("0");
            f.append(Integer.toHexString(n));

            String t =  f.toString();
            if (null != t)
                t = t.toUpperCase(Locale.getDefault());
            
            buff.append(t);
        }
        return buff.toString();
    }
    
    
    //解析位图
    public static int[] analysisBitmap(String va){
        
        int len = (va.length() / 2);
        byte[] hex = new byte[len];
        char[] achar = va.toCharArray();
        for (int i = 0; i < len; i++)
        {
            int pos = i * 2;
            hex[i] = (byte) (mask[achar[pos]] << 4 | mask[achar[pos + 1]]);
        }
        int[] x = new int[64];
        int p = 0;
        for (int i = 0; i < hex.length; i++) {
            if(0 != hex[i]){
                String t = Integer.toBinaryString(hex[i]&0xFF);
                StringBuffer sb = new StringBuffer();
                for (int j = 0; j < 8-t.length(); j++) {
                    sb.append("0");
                }
                sb.append(t);
                t = sb.toString();
                char[] c = t.toCharArray();
                for (int j = 0 ; j < c.length ; j++) {
                    if(c[j]=='1'){
                        x[p] = 8*i + j+1;
                        p++;
                    }
                }
            }
        }
        int[] re = new int[p];
        System.arraycopy(x, 0, re, 0, p);
        return re;
    }
    
    
    public static void main(String[] args) {
        String re = bitmap(4,10,17,28,36,48,53,60);
        System.out.println(re);
        int[] s = analysisBitmap(re);
        for (int i = 0; i < s.length; i++) {
            System.out.println("解析位图之后:" + s[i]);
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读