1. //char数组转换成byte数组
  2. private byte[] getBytes (char[] chars) {
  3. Charset cs = Charset.forName ("UTF-8");
  4. CharBuffer cb = CharBuffer.allocate (chars.length);
  5. cb.put (chars);
  6. cb.flip ();
  7. ByteBuffer bb = cs.encode (cb);
  8. return bb.array();
  9. }
  10. //byte数组转换成char数组
  11. private char[] getChars (byte[] bytes) {
  12. Charset cs = Charset.forName ("UTF-8");
  13. ByteBuffer bb = ByteBuffer.allocate (bytes.length);
  14. bb.put (bytes);
  15. bb.flip ();
  16. CharBuffer cb = cs.decode (bb);
  17. return cb.array();
  18. }
  1. //常用函数
  2.  
  3. package com.boomdts.weather_monitor.util;
  4.  
  5. import java.nio.charset.Charset;
  6. import java.io.*;
  7. import java.text.SimpleDateFormat;
  8. import java.text.DecimalFormat;
  9. import java.util.GregorianCalendar;
  10. import java.util.Calendar;
  11. import java.util.Date;
  12. import java.math.BigDecimal;
  13.  
  14. public class CommonFunctions
  15. {
  16.  
  17. //世纪秒(是指1970年1月1日0时0分到指定时间过去的秒数)转换为年月日,时分秒
  18.  
  19. public static String centurySecondToDateTime(long time)
  20. {
  21. Calendar ca = Calendar.getInstance();
  22. //Date d = ca.getTime();
  23. //long l = ca.getTimeInMillis();
  24. //ca.set(1970, 0, 1);
  25. //long L1970 = ca.getTimeInMillis();
  26. //ca.setTime(d);
  27. //ca.setTimeInMillis(l);
  28.  
  29. String out = "";
  30. GregorianCalendar gc = new GregorianCalendar();
  31. //System.out.print( "上传上来的毫秒数 :" + time*1000 );
  32. gc.setTimeInMillis(time * 1000);
  33. SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  34. out = sdformat.format(gc.getTime());
  35.  
  36. return out;
  37. }
  38.  
  39. //将十六进制字符串转换为double, 41AEF5C2
  40. public static float hexStrToFloat(String str)
  41. {
  42. float result = 0;
  43. try
  44. {
  45. int temp = Integer.parseInt(str.trim(), 16);
  46. result = Float.intBitsToFloat(temp);
  47. }
  48. catch (NumberFormatException e)
  49. {
  50. long ltemp = Long.parseLong(str.trim(), 16);
  51. //long ltemp = Integer.parseInt(str.trim(), 16);
  52. result = Float.intBitsToFloat((int)ltemp);
  53. }
  54. //只保留小数点后两位
  55. //result = (float)(Math.round(result*100))/100;
  56. return result;
  57. }
  58.  
  59. //输入16进制字符串(如 5a5b5c),输出相反顺序的16进制字符串(5c5b5a)。
  60. public static String reverseOrder(String s)
  61. {
  62.  
  63. char[] chA = s.toCharArray();
  64. int l = s.length();
  65. for(int i =0; i< l/2; i=i+2)
  66. {
  67. char cTmp1= 0;
  68. char cTmp2= 0;
  69. cTmp1 = chA[i];
  70. cTmp2 = chA[i+1];
  71. chA[i] = chA[l-i-2];
  72. chA[i+1] = chA[l-i-1];
  73. chA[l-i-2] = cTmp1;
  74. chA[l-i-1] = cTmp2;
  75. }
  76. String sRet = new String(chA);
  77. return sRet;
  78. }
  79. /**
  80. * 字符串转换成十六进制字符串
  81. * @param String str 待转换的ASCII字符串
  82. * @return String 如: [616C6B]
  83. */
  84. public static String strToHexStr(String str)
  85. {
  86.  
  87. char[] chars = "0123456789ABCDEF".toCharArray();
  88. StringBuilder sb = new StringBuilder("");
  89. byte[] bs = str.getBytes();
  90. int bit;
  91.  
  92. for (int i = 0; i < bs.length; i++)
  93. {
  94. bit = (bs[i] & 0x0f0) >> 4;
  95. sb.append(chars[bit]);
  96. bit = bs[i] & 0x0f;
  97. sb.append(chars[bit]);
  98. //sb.append(' ');
  99. }
  100. return sb.toString().trim();
  101. }
  102.  
  103. /**
  104. * 十六进制转换字符串
  105. * @param String str Byte字符串(Byte之间无分隔符 如:[616C6B])
  106. * @return String 对应的字符串
  107. */
  108. public static String hexStrToStr(String hexStr)
  109. {
  110. String str = "0123456789ABCDEF";
  111. char[] hexs = hexStr.toCharArray();
  112. byte[] bytes = new byte[hexStr.length() / 2];
  113. int n;
  114.  
  115. for (int i = 0; i < bytes.length; i++)
  116. {
  117. n = str.indexOf(hexs[2 * i]) * 16;
  118. n += str.indexOf(hexs[2 * i + 1]);
  119. bytes[i] = (byte) (n & 0xff);
  120. }
  121. return new String(bytes);
  122. }
  123.  
  124. /**
  125. * bytes转换成十六进制字符串
  126. * @param byte[] b byte数组
  127. * @return String 每个Byte值之间空格分隔
  128. */
  129. public static String byteToHexStr(byte[] b)
  130. {
  131. String stmp="";
  132. StringBuilder sb = new StringBuilder("");
  133. for (int n=0;n<b.length;n++)
  134. {
  135. stmp = Integer.toHexString(b[n] & 0xFF);
  136. sb.append((stmp.length()==1)? "0"+stmp : stmp);
  137. //sb.append(" ");
  138. }
  139. return sb.toString().toUpperCase().trim();
  140. }
  141.  
  142. /**
  143. * bytes字符串转换为Byte值
  144. * @param String src Byte字符串,每个Byte之间没有分隔符
  145. * @return byte[]
  146. */
  147. public static byte[] hexStrToBytes(String src)
  148. {
  149. int m=0,n=0;
  150. int cc = src.length();
  151. if(cc%2 != 0){
  152. System.out.println("函数 hexStrToBytes 输入的字符为奇数,这有可能会有问题,输入的字符个数是: " + cc);
  153. }
  154. int l=src.length()/2;
  155. byte[] ret = new byte[l];
  156. String sSub ;
  157. for (int i = 0; i < l; i++)
  158. {
  159. sSub = src.substring(i*2,i*2+2);
  160. ret[i] = (byte)( Integer.parseInt(sSub, 16) );
  161.  
  162. /*
  163. m=i*2+1;
  164. n=m+1;
  165. ret[i] = Byte.decode("0x" + src.substring(i*2, m) + src.substring(m,n));
  166. */
  167. }
  168. return ret;
  169. }
  170.  
  171. /**
  172. * String的字符串转换成unicode的String
  173. * @param String strText 全角字符串
  174. * @return String 每个unicode之间无分隔符
  175. * @throws Exception
  176. */
  177. public static String strToUnicode(String strText)
  178. throws Exception
  179. {
  180. char c;
  181. StringBuilder str = new StringBuilder();
  182. int intAsc;
  183. String strHex;
  184. for (int i = 0; i < strText.length(); i++)
  185. {
  186. c = strText.charAt(i);
  187. intAsc = (int) c;
  188. strHex = Integer.toHexString(intAsc);
  189. if (intAsc > 128)
  190. str.append("\\u" + strHex);
  191. else // 低位在前面补00
  192. str.append("\\u00" + strHex);
  193. }
  194. return str.toString();
  195. }
  196.  
  197. /**
  198. * unicode的String转换成String的字符串
  199. * @param String hex 16进制值字符串 (一个unicode为2byte)
  200. * @return String 全角字符串
  201. */
  202. public static String unicodeToString(String hex)
  203. {
  204. int t = hex.length() / 6;
  205. StringBuilder str = new StringBuilder();
  206. for (int i = 0; i < t; i++)
  207. {
  208. String s = hex.substring(i * 6, (i + 1) * 6);
  209. // 高位需要补上00再转
  210. String s1 = s.substring(2, 4) + "00";
  211. // 低位直接转
  212. String s2 = s.substring(4);
  213. // 将16进制的string转为int
  214. int n = Integer.valueOf(s1, 16) + Integer.valueOf(s2, 16);
  215. // 将int转换为字符
  216. char[] chars = Character.toChars(n);
  217. str.append(new String(chars));
  218. }
  219. return str.toString();
  220. }
  221. /**
  222. * 截取byte数据
  223. * @param b 是byte数组
  224. * @param j 是大小
  225. * @return
  226. */
  227. public static byte[] cutOutByte(byte[] b,int j){
  228. if(b.length==0 || j==0){
  229. return null;
  230. }
  231. byte[] bjq = new byte[j];
  232. for(int i = 0; i<j;i++){
  233. bjq[i]=b[i];
  234. }
  235. return bjq;
  236. }
  237. /**
  238. * 合并两个byte数组
  239. * @param pByteA
  240. * @param pByteB
  241. * @return
  242. */
  243. public static byte[] getMergeBytes(byte[] pByteA, byte[] pByteB){
  244. int aCount = pByteA.length;
  245. int bCount = pByteB.length;
  246. byte[] b = new byte[aCount + bCount];
  247. for(int i=0;i<aCount;i++){
  248. b[i] = pByteA[i];
  249. }
  250. for(int i=0;i<bCount;i++){
  251. b[aCount + i] = pByteB[i];
  252. }
  253. return b;
  254. }
  255.  
  256. /**
  257. * 字符转换为日期类型
  258. * @param dateString
  259. * @return
  260. */
  261. public static Date parseDateTime(String dateString) {
  262. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  263. Date result = null;
  264. try {
  265. result = df.parse(dateString);
  266. } catch (Exception e) {
  267. }
  268. return result;
  269. }
  270.  
  271. /**
  272. * 两个日期相减计算多少天
  273. * @param firstDate
  274. * @param lastDate
  275. * @return A double days
  276. */
  277. public static int compareDateToDays(Date firstDate, Date lastDate) {
  278. if (firstDate == null || lastDate == null) {
  279. System.out.print("NULL");
  280. }
  281. long time1 = firstDate.getTime();
  282. long time2 = lastDate.getTime();
  283. long tmpCal = time2 - time1;
  284. long mm = 24 * 60 * 60 * 1000;
  285. int days = (int) (tmpCal / mm);
  286. return Math.abs(days);
  287. }
  288.  
  289. }

java 转换 小函数(不断增加中。。。)的更多相关文章

  1. JS_ECMA基本语法中的几种封装的小函数-2

    大家好!今天继续给大家写一下ECMA中的剩下的小函数以及实用的实例: 首先先给大家说一下字符串.数组.数学方法以及json的一点小知识点: 字符串方法: str.length str.charAt(i ...

  2. JS_ECMA基本语法中的几种封装的小函数

      先来回顾一下我们的字符串: 字符串方法: str.length str.charAt(i):取字符串中的某一个; str.indexOf('e');找第一个出现的位置;找不到返回-1; str.l ...

  3. java代码将excel文件中的内容列表转换成JS文件输出

    思路分析 我们想要把excel文件中的内容转为其他形式的文件输出,肯定需要分两步走: 1.把excel文件中的内容读出来: 2.将内容写到新的文件中. 举例 一张excel表中有一个表格: 我们需要将 ...

  4. FastReport调用Delphi中的人民币大写转换自定义函数

    FastReport调用Delphi中的人民币大写转换自定义函数   FastReport调用Delphi中的人民币大写转换自定义函数 function TJzpzEdit1.MoneyCn(mmje ...

  5. JS_ECMA基本语法中的几种封装的小函数-1

    今天给大家介绍js ECMA中几个封装的小函数以及一些常用的函数小案例: 1,找重复的函数 <script> //在数组里面找重复: function findInArr(n,arr){ ...

  6. java 11-8 在大串中查找小串的案例

    1.统计大串中小串出现的次数 举例: 在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun& ...

  7. java的小程序在html中的运行测试

    java的小程序在html中的运行测试,打开vs2012,以网站模式打开,生成,调用iis临时服务器运行.

  8. 样条函数后续(java)--可在hive中执行的函数

    之前写的样条插值算法只能在本地执行,但是我想要的是可在hive中执行的jar包,为了符合我的要求,经过痛苦.气愤.悲伤等一系列过程,终于实现了: 想要实现可在hive中执行的jar包,以下是具体步骤: ...

  9. java基础课程笔记 static 主函数 静态工具类 classpath java文档注释 静态代码块 对象初始化过程 设计模式 继承 子父类中的函数 继承中的构造函数 对象转型 多态 封装 抽象类 final 接口 包 jar包

    Static那些事儿 Static关键字 被static修饰的变量成为静态变量(类变量) 作用:是一个修饰符,用于修饰成员(成员变量,成员方法) 1.被static修饰后的成员变量只有一份 2.当成员 ...

随机推荐

  1. bzoj3315:[Usaco2013 Nov]Pogo-Cow

    思路:首先可以写出n^3dp的状态转移方程:f[i][j]=max{f[j][k]+val[i]},f[i][j]表示最后一步跳到点从j点跳到i点的最大价值(状态不能设成f[i],因为j对后面的决策是 ...

  2. web项目嵌入Jetty运行的两种方式(Jetty插件和自制Jetty服务器)

    在开发Java web项目时候,可以在项目中嵌入Jetty服务的方式来运行web程序. 由于最近开发web项目,自己使用的是比较旧的eclipse不支持导入tomcat来运行项目,于是就学习了下使用项 ...

  3. iOS SEL的简单总结

    @interface Person : NSObject + (void)test1; - (void)test2; @end // 根据.h文件中定义的Person类和方法 执行完这行代码 在内存中 ...

  4. 【ASP.NET+MVC4+Web+编程】读书笔记

    模型:数据和业务逻辑 视图:展示 控制器:接收视图输入数据,通过模型层业务逻辑处理后 返回给视图 分离关注点(模型 视图 控制器).惯例优先原则 browser-->routing-->c ...

  5. 小课堂Week8 例外处理设计的逆袭Part1

    小课堂Week8 例外处理设计的逆袭Part1 今天和大家讲一本书,书名是<例外处理设计的逆袭>. 为什么想讲这本书,是因为,例外处理在程序代码中到处存在,但是这些到底该如何写好,总觉得有 ...

  6. 1020. Tree Traversals (序列建树)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  7. go语言实现的目录共享程序

    其实程序很小,只不过是想写点东西了.后天晚上要回学校考试了,转眼已经出来了69天了,2个月多一点.工资加上老妈赞助的钱,不知道能不能买台电脑,作为程序员一直用着i3-3217u实在难受.回去找同学拷点 ...

  8. 如何应用CLR线程池来管理多线程

        class Program     {         static void Main(string[] args)         {             int intWorkerT ...

  9. Cygwin安装与配置

    Cygwin可以在windows环境下模拟Linux系统,而且可以重用Linux下面丰富的脚本工具.windows的cmd太弱了.Cygwin是由Cygnus(天鹅座) Solution公司开发,不过 ...

  10. java指令集

    0x00 nop      什么都不做 0x01 aconst_null 将null推送至栈顶 0x02 iconst_m1   将int型-1推送至栈顶 0x03 iconst_0   将int型0 ...