java 转换 小函数(不断增加中。。。)
- //char数组转换成byte数组
- private 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();
- }
- //byte数组转换成char数组
- private 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();
- }
- //常用函数
- package com.boomdts.weather_monitor.util;
- import java.nio.charset.Charset;
- import java.io.*;
- import java.text.SimpleDateFormat;
- import java.text.DecimalFormat;
- import java.util.GregorianCalendar;
- import java.util.Calendar;
- import java.util.Date;
- import java.math.BigDecimal;
- public class CommonFunctions
- {
- //世纪秒(是指1970年1月1日0时0分到指定时间过去的秒数)转换为年月日,时分秒
- public static String centurySecondToDateTime(long time)
- {
- Calendar ca = Calendar.getInstance();
- //Date d = ca.getTime();
- //long l = ca.getTimeInMillis();
- //ca.set(1970, 0, 1);
- //long L1970 = ca.getTimeInMillis();
- //ca.setTime(d);
- //ca.setTimeInMillis(l);
- String out = "";
- GregorianCalendar gc = new GregorianCalendar();
- //System.out.print( "上传上来的毫秒数 :" + time*1000 );
- gc.setTimeInMillis(time * 1000);
- SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- out = sdformat.format(gc.getTime());
- return out;
- }
- //将十六进制字符串转换为double, 41AEF5C2
- public static float hexStrToFloat(String str)
- {
- float result = 0;
- try
- {
- int temp = Integer.parseInt(str.trim(), 16);
- result = Float.intBitsToFloat(temp);
- }
- catch (NumberFormatException e)
- {
- long ltemp = Long.parseLong(str.trim(), 16);
- //long ltemp = Integer.parseInt(str.trim(), 16);
- result = Float.intBitsToFloat((int)ltemp);
- }
- //只保留小数点后两位
- //result = (float)(Math.round(result*100))/100;
- return result;
- }
- //输入16进制字符串(如 5a5b5c),输出相反顺序的16进制字符串(5c5b5a)。
- public static String reverseOrder(String s)
- {
- char[] chA = s.toCharArray();
- int l = s.length();
- for(int i =0; i< l/2; i=i+2)
- {
- char cTmp1= 0;
- char cTmp2= 0;
- cTmp1 = chA[i];
- cTmp2 = chA[i+1];
- chA[i] = chA[l-i-2];
- chA[i+1] = chA[l-i-1];
- chA[l-i-2] = cTmp1;
- chA[l-i-1] = cTmp2;
- }
- String sRet = new String(chA);
- return sRet;
- }
- /**
- * 字符串转换成十六进制字符串
- * @param String str 待转换的ASCII字符串
- * @return String 如: [616C6B]
- */
- public static String strToHexStr(String str)
- {
- char[] chars = "0123456789ABCDEF".toCharArray();
- StringBuilder sb = new StringBuilder("");
- byte[] bs = str.getBytes();
- int bit;
- for (int i = 0; i < bs.length; i++)
- {
- bit = (bs[i] & 0x0f0) >> 4;
- sb.append(chars[bit]);
- bit = bs[i] & 0x0f;
- sb.append(chars[bit]);
- //sb.append(' ');
- }
- return sb.toString().trim();
- }
- /**
- * 十六进制转换字符串
- * @param String str Byte字符串(Byte之间无分隔符 如:[616C6B])
- * @return String 对应的字符串
- */
- public static String hexStrToStr(String hexStr)
- {
- String str = "0123456789ABCDEF";
- char[] hexs = hexStr.toCharArray();
- byte[] bytes = new byte[hexStr.length() / 2];
- int n;
- for (int i = 0; i < bytes.length; i++)
- {
- n = str.indexOf(hexs[2 * i]) * 16;
- n += str.indexOf(hexs[2 * i + 1]);
- bytes[i] = (byte) (n & 0xff);
- }
- return new String(bytes);
- }
- /**
- * bytes转换成十六进制字符串
- * @param byte[] b byte数组
- * @return String 每个Byte值之间空格分隔
- */
- public static String byteToHexStr(byte[] b)
- {
- String stmp="";
- StringBuilder sb = new StringBuilder("");
- for (int n=0;n<b.length;n++)
- {
- stmp = Integer.toHexString(b[n] & 0xFF);
- sb.append((stmp.length()==1)? "0"+stmp : stmp);
- //sb.append(" ");
- }
- return sb.toString().toUpperCase().trim();
- }
- /**
- * bytes字符串转换为Byte值
- * @param String src Byte字符串,每个Byte之间没有分隔符
- * @return byte[]
- */
- public static byte[] hexStrToBytes(String src)
- {
- int m=0,n=0;
- int cc = src.length();
- if(cc%2 != 0){
- System.out.println("函数 hexStrToBytes 输入的字符为奇数,这有可能会有问题,输入的字符个数是: " + cc);
- }
- int l=src.length()/2;
- byte[] ret = new byte[l];
- String sSub ;
- for (int i = 0; i < l; i++)
- {
- sSub = src.substring(i*2,i*2+2);
- ret[i] = (byte)( Integer.parseInt(sSub, 16) );
- /*
- m=i*2+1;
- n=m+1;
- ret[i] = Byte.decode("0x" + src.substring(i*2, m) + src.substring(m,n));
- */
- }
- return ret;
- }
- /**
- * String的字符串转换成unicode的String
- * @param String strText 全角字符串
- * @return String 每个unicode之间无分隔符
- * @throws Exception
- */
- public static String strToUnicode(String strText)
- throws Exception
- {
- char c;
- StringBuilder str = new StringBuilder();
- int intAsc;
- String strHex;
- for (int i = 0; i < strText.length(); i++)
- {
- c = strText.charAt(i);
- intAsc = (int) c;
- strHex = Integer.toHexString(intAsc);
- if (intAsc > 128)
- str.append("\\u" + strHex);
- else // 低位在前面补00
- str.append("\\u00" + strHex);
- }
- return str.toString();
- }
- /**
- * unicode的String转换成String的字符串
- * @param String hex 16进制值字符串 (一个unicode为2byte)
- * @return String 全角字符串
- */
- public static String unicodeToString(String hex)
- {
- int t = hex.length() / 6;
- StringBuilder str = new StringBuilder();
- for (int i = 0; i < t; i++)
- {
- String s = hex.substring(i * 6, (i + 1) * 6);
- // 高位需要补上00再转
- String s1 = s.substring(2, 4) + "00";
- // 低位直接转
- String s2 = s.substring(4);
- // 将16进制的string转为int
- int n = Integer.valueOf(s1, 16) + Integer.valueOf(s2, 16);
- // 将int转换为字符
- char[] chars = Character.toChars(n);
- str.append(new String(chars));
- }
- return str.toString();
- }
- /**
- * 截取byte数据
- * @param b 是byte数组
- * @param j 是大小
- * @return
- */
- public static byte[] cutOutByte(byte[] b,int j){
- if(b.length==0 || j==0){
- return null;
- }
- byte[] bjq = new byte[j];
- for(int i = 0; i<j;i++){
- bjq[i]=b[i];
- }
- return bjq;
- }
- /**
- * 合并两个byte数组
- * @param pByteA
- * @param pByteB
- * @return
- */
- public static byte[] getMergeBytes(byte[] pByteA, byte[] pByteB){
- int aCount = pByteA.length;
- int bCount = pByteB.length;
- byte[] b = new byte[aCount + bCount];
- for(int i=0;i<aCount;i++){
- b[i] = pByteA[i];
- }
- for(int i=0;i<bCount;i++){
- b[aCount + i] = pByteB[i];
- }
- return b;
- }
- /**
- * 字符转换为日期类型
- * @param dateString
- * @return
- */
- public static Date parseDateTime(String dateString) {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date result = null;
- try {
- result = df.parse(dateString);
- } catch (Exception e) {
- }
- return result;
- }
- /**
- * 两个日期相减计算多少天
- * @param firstDate
- * @param lastDate
- * @return A double days
- */
- public static int compareDateToDays(Date firstDate, Date lastDate) {
- if (firstDate == null || lastDate == null) {
- System.out.print("NULL");
- }
- long time1 = firstDate.getTime();
- long time2 = lastDate.getTime();
- long tmpCal = time2 - time1;
- long mm = 24 * 60 * 60 * 1000;
- int days = (int) (tmpCal / mm);
- return Math.abs(days);
- }
- }
java 转换 小函数(不断增加中。。。)的更多相关文章
- JS_ECMA基本语法中的几种封装的小函数-2
大家好!今天继续给大家写一下ECMA中的剩下的小函数以及实用的实例: 首先先给大家说一下字符串.数组.数学方法以及json的一点小知识点: 字符串方法: str.length str.charAt(i ...
- JS_ECMA基本语法中的几种封装的小函数
先来回顾一下我们的字符串: 字符串方法: str.length str.charAt(i):取字符串中的某一个; str.indexOf('e');找第一个出现的位置;找不到返回-1; str.l ...
- java代码将excel文件中的内容列表转换成JS文件输出
思路分析 我们想要把excel文件中的内容转为其他形式的文件输出,肯定需要分两步走: 1.把excel文件中的内容读出来: 2.将内容写到新的文件中. 举例 一张excel表中有一个表格: 我们需要将 ...
- FastReport调用Delphi中的人民币大写转换自定义函数
FastReport调用Delphi中的人民币大写转换自定义函数 FastReport调用Delphi中的人民币大写转换自定义函数 function TJzpzEdit1.MoneyCn(mmje ...
- JS_ECMA基本语法中的几种封装的小函数-1
今天给大家介绍js ECMA中几个封装的小函数以及一些常用的函数小案例: 1,找重复的函数 <script> //在数组里面找重复: function findInArr(n,arr){ ...
- java 11-8 在大串中查找小串的案例
1.统计大串中小串出现的次数 举例: 在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun& ...
- java的小程序在html中的运行测试
java的小程序在html中的运行测试,打开vs2012,以网站模式打开,生成,调用iis临时服务器运行.
- 样条函数后续(java)--可在hive中执行的函数
之前写的样条插值算法只能在本地执行,但是我想要的是可在hive中执行的jar包,为了符合我的要求,经过痛苦.气愤.悲伤等一系列过程,终于实现了: 想要实现可在hive中执行的jar包,以下是具体步骤: ...
- java基础课程笔记 static 主函数 静态工具类 classpath java文档注释 静态代码块 对象初始化过程 设计模式 继承 子父类中的函数 继承中的构造函数 对象转型 多态 封装 抽象类 final 接口 包 jar包
Static那些事儿 Static关键字 被static修饰的变量成为静态变量(类变量) 作用:是一个修饰符,用于修饰成员(成员变量,成员方法) 1.被static修饰后的成员变量只有一份 2.当成员 ...
随机推荐
- bzoj3315:[Usaco2013 Nov]Pogo-Cow
思路:首先可以写出n^3dp的状态转移方程:f[i][j]=max{f[j][k]+val[i]},f[i][j]表示最后一步跳到点从j点跳到i点的最大价值(状态不能设成f[i],因为j对后面的决策是 ...
- web项目嵌入Jetty运行的两种方式(Jetty插件和自制Jetty服务器)
在开发Java web项目时候,可以在项目中嵌入Jetty服务的方式来运行web程序. 由于最近开发web项目,自己使用的是比较旧的eclipse不支持导入tomcat来运行项目,于是就学习了下使用项 ...
- iOS SEL的简单总结
@interface Person : NSObject + (void)test1; - (void)test2; @end // 根据.h文件中定义的Person类和方法 执行完这行代码 在内存中 ...
- 【ASP.NET+MVC4+Web+编程】读书笔记
模型:数据和业务逻辑 视图:展示 控制器:接收视图输入数据,通过模型层业务逻辑处理后 返回给视图 分离关注点(模型 视图 控制器).惯例优先原则 browser-->routing-->c ...
- 小课堂Week8 例外处理设计的逆袭Part1
小课堂Week8 例外处理设计的逆袭Part1 今天和大家讲一本书,书名是<例外处理设计的逆袭>. 为什么想讲这本书,是因为,例外处理在程序代码中到处存在,但是这些到底该如何写好,总觉得有 ...
- 1020. Tree Traversals (序列建树)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- go语言实现的目录共享程序
其实程序很小,只不过是想写点东西了.后天晚上要回学校考试了,转眼已经出来了69天了,2个月多一点.工资加上老妈赞助的钱,不知道能不能买台电脑,作为程序员一直用着i3-3217u实在难受.回去找同学拷点 ...
- 如何应用CLR线程池来管理多线程
class Program { static void Main(string[] args) { int intWorkerT ...
- Cygwin安装与配置
Cygwin可以在windows环境下模拟Linux系统,而且可以重用Linux下面丰富的脚本工具.windows的cmd太弱了.Cygwin是由Cygnus(天鹅座) Solution公司开发,不过 ...
- java指令集
0x00 nop 什么都不做 0x01 aconst_null 将null推送至栈顶 0x02 iconst_m1 将int型-1推送至栈顶 0x03 iconst_0 将int型0 ...