⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jrutility.java

📁 为公司做的质量考核接口源码,用spring,hibernate,XML实现,对XML接口编程很有帮助
💻 JAVA
字号:
/**
 * 
 */
package com.jr81.common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URLDecoder;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import sun.misc.BASE64Decoder;

/**
 * @author Administrator
 *
 */
public class JrUtility {

	static public int bytes2int(byte[] b)
	{
	         //byte[] b=new byte[]{1,2,3,4}; 
	         int mask=0xff;
	         int temp=0;
	        int res=0;
	        for(int i=0;i<4;i++){
	            res<<=8;
	            temp=b[i]&mask;
	            res|=temp;
	        }
	       return res;
	} 

	static public byte[] int2bytes(int num)
	{
	       byte[] b=new byte[4];
	       //int mask=0xff;
	       for(int i=0;i<4;i++){
	            b[i]=(byte)(num>>>(24-i*8));
	       }
	      return b;
	}		
	
	public static OutputStream StreamOutputToInput(InputStream value) throws IOException{
		int size =value.available();
		byte[] content=new byte[size];
		value.read(content,0,size);
		ByteArrayOutputStream out=new ByteArrayOutputStream(size);
		out.write(content,0,size);
		return out;
	}
	
	public static InputStream StreamInputToOutput(OutputStream value) throws IOException{
		return null;
		//int size =value();
		//byte[] content=new byte[size];
		//ByteArrayOutputStream out=new ByteArrayOutputStream(size);
		//value.read(content,0,size);
		
		//ByteArrayInputStream in=new ByteArrayInputStream();
		//out.write(content,0,size);
		//return in;
	}
	
	public static Date now(){
		try {
			SimpleDateFormat dateFormatter =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
			GregorianCalendar gc=new GregorianCalendar();
			return dateFormatter.parse( dateFormatter.format(gc.getTime()));
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date date(){
		try {
			SimpleDateFormat dateFormatter =new SimpleDateFormat("yyyy-MM-dd");
			GregorianCalendar gc=new GregorianCalendar();
			return dateFormatter.parse( dateFormatter.format(gc.getTime()));
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return null;
	}
	
	public static Integer year(){
		try {
			SimpleDateFormat dateFormatter =new SimpleDateFormat("yyyy");
			GregorianCalendar gc=new GregorianCalendar();
			String y=dateFormatter.format(gc.getTime());
			return Integer.valueOf(y);
		} catch (Exception e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return null;
	}
	
	public static Integer month(){
		try {
			SimpleDateFormat dateFormatter =new SimpleDateFormat("MM");
			GregorianCalendar gc=new GregorianCalendar();
			String m=dateFormatter.format(gc.getTime());
			return Integer.valueOf(m);
		} catch (Exception e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return null;
	}
	
	public static Integer day(){
		try {
			SimpleDateFormat dateFormatter =new SimpleDateFormat("dd");
			GregorianCalendar gc=new GregorianCalendar();
			String m=dateFormatter.format(gc.getTime());
			return Integer.valueOf(m);
		} catch (Exception e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return null;
	}
	
	public static Integer Quarter(){
		int q=1;
		switch (month().intValue()){
		case 1:	q= 1;break;
		case 2:	q= 1;break;
		case 3:	q= 1;break;
		case 4:	q= 2;break;
		case 5:	q= 2;break;
		case 6:	q= 2;break;
		case 7:	q= 3;break;
		case 8:	q= 3;break;
		case 9:	q= 3;break;
		case 10:	q= 4;break;
		case 11:	q= 4;break;
		case 12:	q= 4;break;
		}
		return Integer.valueOf(q);
	}
	
	public static String SystemDate(){
		SimpleDateFormat dateFormatter =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
		GregorianCalendar gc=new GregorianCalendar();
		return  dateFormatter.format(gc.getTime());	
	}
	
	public static String SystemTime(){
		SimpleDateFormat dateFormatter =new SimpleDateFormat("yyyyMMddHHmmssSSS");
		GregorianCalendar gc=new GregorianCalendar();
		return  dateFormatter.format(gc.getTime());	
	}

	public static String GetLocalDateTime() {
		return JrUtility.SystemDate();
	}
	
	public static String ToChinese(String strvalue) {
		try {
			if (strvalue == null)
				return null;
			else {
				//strvalue = new String(strvalue.getBytes("ISO8859_1"), "gb2312");
				byte[] temp = strvalue.getBytes("gb2312");//, "gb2312");
				strvalue=new String(temp);
				return strvalue;
			}
		} catch (Exception e) {
			return null;
		}
	}
	
//	从文本文件对象中读取内容并转换为字符数组 
	public static char[] readChars(File file) 
	{ 
	     CharArrayWriter caw = new CharArrayWriter(); 
	     try 
	     { 
	         Reader fr = new FileReader(file); 
	        Reader in = new BufferedReader(fr); 
	         int count = 0; 
	         char[] buf = new char[16384]; 
	        while ((count=in.read(buf)) != -1) { 
	             if (count > 0) caw.write(buf, 0, count); 
	         } 
	         in.close(); 
	     } 
	     catch (Exception e) { e.printStackTrace(); } 
	     return caw.toCharArray(); 
	} 
//	从字符串对象中读取内容并转换为字符数组 
	public static char[] readChars(String string) 
	{ 
	     CharArrayWriter caw = new CharArrayWriter(); 
	     try 
	     { 
	         Reader sr = new StringReader(string.trim()); 
	        Reader in = new BufferedReader(sr); 
	         int count = 0; 
	         char[] buf = new char[16384]; 
	        while ((count=in.read(buf)) != -1) { 
	             if (count > 0) caw.write(buf, 0, count); 
	         } 
	         in.close(); 
	     } 
	     catch (Exception e) { e.printStackTrace(); } 
	     return caw.toCharArray(); 
	} 
//	从二进制文件对象中读取内容并转换为字节数组 
	public static byte[] readBytes(File file) 
	{ 
	     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
	     try 
	     { 
	         InputStream fis = new FileInputStream(file); 
	         InputStream is = new BufferedInputStream(fis); 
	        int count = 0; 
	         byte[] buf = new byte[16384]; 
	         while ((count=is.read(buf)) != -1) { 
	             if (count > 0) baos.write(buf, 0, count); 
	         } 
	         is.close(); 
	    } 
	     catch (Exception e) { e.printStackTrace(); } 
	     return baos.toByteArray(); 
	} 
//	写字节数组内容到二进制文件 
	public static void writeBytes(File file, byte[] data) { 
	     try { 
	         OutputStream fos = new FileOutputStream(file); 
	         OutputStream os = new BufferedOutputStream(fos); 
	         os.write(data); 
	         os.close(); 
	     } 
	     catch (Exception e) { e.printStackTrace(); } 
	} 
//	写字符数组内容到文本文件 
	public static void writeChars(File file, char[] data) { 
	     try { 
	         Writer fos = new FileWriter(file); 
	         Writer os = new BufferedWriter(fos); 
	        os.write(data); 
	         os.close(); 
	     } 
	     catch (Exception e) { e.printStackTrace(); } 
	} 
	 
	public static byte[] getBytes (char[] chars) {
		Charset cs = Charset.forName ("UTF-8");
		CharBuffer cb = CharBuffer.allocate (chars.length);
		cb.put (chars);
                cb.flip ();
		ByteBuffer bb = cs.encode (cb);
		
		return bb.array();
        }
 
	public static char[] getChars (byte[] bytes) {
		Charset cs = Charset.forName ("UTF-8");
		ByteBuffer bb = ByteBuffer.allocate (bytes.length);
		bb.put (bytes);
                bb.flip ();
		CharBuffer cb = cs.decode (bb);
		
		return cb.array();
	}
	
	public static String GetClassRoot(Object obj){
		try {
			return URLDecoder.decode(obj.getClass().getResource("/").getPath(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return null;
	}
	
	// 将 s 进行 BASE64 编码 
	public static String getBASE64(String s) { 
		if (s == null) return null; 
		return (new sun.misc.BASE64Encoder()).encode( s.getBytes() ); 
	} 
	
	// 将 BASE64 编码的字符串 s 进行解码 
	public static String getFromBASE64(String s) { 
		if (s == null) return null; 
		BASE64Decoder decoder = new BASE64Decoder(); 
		try { 
			byte[] b = decoder.decodeBuffer(s); 
			return new String(b); 
		} catch (Exception e) { 
			return null; 
		} 
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(year());
		System.out.println(Quarter());
		System.out.println(month());
		System.out.println(day());
		System.out.println(now());
		System.out.println(date());
		System.out.println(SystemDate());
		System.out.println(SystemTime());
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -