📄 strutil.java
字号:
package com.frontMachine.util;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @公共函数类
* @author kingchen
* @version 1.0
*/
public class StrUtil
{
public static boolean checkEmpty(String XML,String s[])
{
int i = 0;
for(i=0;i<s.length;i++)
{
if(getXMLData(XML,s[i]).equals(""))
{
return false;
}
}
return true;
}
public static byte[] packageXML(String XML)
{
byte[] xmlbyte = XML.getBytes();
int len = xmlbyte.length;
/*int a1=len%256;
int a2=(len/256)%256;
int a3=(len/256/256)%256;
int a4=len/256/256/256;
ob[0]=(byte)a1;
ob[1]=(byte)a2;
ob[2]=(byte)a3;
ob[3]=(byte)a4;*/
byte[] ob = new byte[len+4];
byte[] lenByte = IntToBytesL(len);
ob[0]=lenByte[0];
ob[1]=lenByte[1];
ob[2]=lenByte[2];
ob[3]=lenByte[3];
for(int i=0;i<xmlbyte.length;i++)
{
ob[i+4]=xmlbyte[i];
}
return ob;
}
public static String showNull(String str)
{
try
{
if(str==null)
{
return "";
}
if("null".equals(str))
{
return "";
}
}
catch (RuntimeException e)
{
System.out.println(e);
}
return str;
}
public static String getXMLData(String XML,String name)
{
try
{
if(XML==null || name==null)
{
return "";
}
int len1 = name.length();
int i = XML.indexOf("<"+name+">");
if(i>=0)
{
int j=XML.indexOf("</"+name+">");
if(j>=0)
{
return (XML.substring(i+len1+2,j).trim());
}
return "";
}
return "";
}catch(Exception e)
{
return "";
}
}
public static String setXMLData(String XML,String name,String data)
{
try
{
if(XML==null || name==null)
{
XML="";
}
String XML2=XML;
int len=XML.length();
int len1=name.length();
int i= XML.indexOf("<"+name);
if(i>=0)
{
int j=XML.indexOf("/"+name);
if(j>=0)
{
return (XML.substring(0,i+len1+2)+data+XML2.substring(j-1,len));
}
return XML;
}
return XML;
}catch(Exception e)
{
return XML;
}
}
public static String getCurrentDate()
{
SimpleDateFormat dateformat=new SimpleDateFormat("yyyyMMdd");
Date currentdate = new Date();
return (""+dateformat.format(currentdate));
}
public static String getCurrentTime()
{
SimpleDateFormat dateformat=new SimpleDateFormat("HHmmss");
Date currenttime = new Date();
return (""+dateformat.format(currenttime));
}
public static String getCurrDateTime()
{
SimpleDateFormat dateformat=new SimpleDateFormat("yyyyMMddHHmmss");
Date currenttime = new Date();
return (""+dateformat.format(currenttime));
}
/**************Int 低位放前面 begin *************/
public static int BytesToIntL(byte[] b)
{
return (int)((((b[3] & 0xff) << 24) | ((b[2] & 0xff) << 16) | ((b[1] & 0xff) << 8) | ((b[0] & 0xff) << 0)));
}
public static byte[] IntToBytesL(int x)
{
byte[] b = new byte[4];
b[3] = (byte)(x >> 24);
b[2] = (byte)(x >> 16);
b[1] = (byte)(x >> 8);
b[0] = (byte)(x >> 0);
return b;
}
/**************int 低位放前面 end *************/
/**************int 高位放前面 begin *************/
public static int BytesToIntB(byte[] b)
{
try
{
int value = (int)((((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16) | ((b[2] & 0xff) << 8) | ((b[3] & 0xff) << 0)));
return value;
}
catch (RuntimeException e)
{
return -1;
}
}
public static byte[] IntToBytesB(int x)
{
byte[] b = new byte[4];
b[0] = (byte)(x >> 24);
b[1] = (byte)(x >> 16);
b[2] = (byte)(x >> 8);
b[3] = (byte)(x >> 0);
return b;
}
/**************Int 高位放前面 end *************/
/**************Float 低位放前面 begin *************/
public static byte[] FloatToBytesL(float x)
{
return IntToBytesL(Float.floatToRawIntBits(x));
}
public static float BytesToFloatL(byte[] b)
{
return Float.intBitsToFloat(BytesToIntL(b));
}
/**************Float 低位放前面 end *************/
/**************Float 高位放前面 begin *************/
public static byte[] FloatToBytesB(float x)
{
return IntToBytesB(Float.floatToRawIntBits(x));
}
public static float BytesToFloatB(byte[] b)
{
return Float.intBitsToFloat(BytesToIntB(b));
}
/**************Float 高位放前面 end *************/
public static String byte2hex(byte[] b)
{
if(b==null)
{
return "";
}
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++)
{
//System.out.println("-----------["+b[n]+"]---------");
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
// if (n<b.length-1) hs=hs+":";
}
return hs.toUpperCase();
}
public static byte[] hex2byte(String hex)
{
//System.out.println(hex);
int len = hex.length();
byte[] b;
try
{
int byteint = 0;
String swap = "";
if (len % 2 != 0)
{
return new byte[len/2];
}
char[] arr = hex.toCharArray();
b = new byte[len/2];
for (int i = 0, j = 0, l = len; i < l; i++, j++)
{
swap = "" + arr[i++] + arr[i];
//System.out.println("----"+(Integer.parseInt(swap,16)));
byteint = Integer.parseInt(swap,16)&0xFF;
b[j] = new Integer(byteint).byteValue();
}
return b;
}
catch (NumberFormatException e)
{
return new byte[len/2];
}
}
public static void main(String[] args)
{
System.out.println(getCurrentTime());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -