的常用函数代码! java 的
常用函数代码!
Java code /** * 将某个日期以固定格式转化成字符串 * * @param date * @return String */ public static String dateToStr(java.util.Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str = sdf.format(date); return str; }
Java code /** * 判断任意一个整数是否素数 * * @param n * @return boolean */ public static boolean isPrimes(int n) { for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { return false; } } return true; }
Java code
/** * 获得任意一个整数的阶乘,递归 * * @param n * @return n! */ public static int factorial(int n) { if (n == 1) { return 1; } return n * factorial(n - 1); }
Java code /** * 将指定 byte 数组以 16 进制的形式打印到控制台 * * @param hint * String * @param b * byte[] * @return void */ public static void printHexString(String hint, byte[] b) { System.out.print(hint); for (int i = 0; i < b.length; i++) { String hex = Integer.toHexString(b[i] &; 0xFF); if (hex.length() == 1) { hex = '0' + hex; } System.out.print(hex.toUpperCase() + " "); } System.out.println(""); }
wait();//线程等待 notify();//激活一个线程
我一直在用的一个字符串类库,自己写的用了很多年 Java code package net.java2000.tools; /** * Title: Java Bean 工具 * Description: * Copyright: Copyright (c) 2001 * Company: JAVA 世纪网 java2000.net * @author 赵学庆 * @version 1.0 */ import java.util.*; import java.util.regex.Pattern; public class StrTools { /** * 分割字符串 * * @param str String 原始字符串 * @param splitsign String 分隔符 * @return String[] 分割后的字符串数组 */ @SuppressWarnings("unchecked") public static String[] split(String str, String splitsign) { int index; if (str == null || splitsign == null) return null; ArrayList al = new ArrayList(); while ((index = str.indexOf(splitsign)) != -1) { al.add(str.substring(0, index)); str = str.substring(index + splitsign.length()); }
al.add(str); return (String[]) al.toArray(new String[0]); } /** * 替换字符串 * * @param from String 原始字符串 * @param to String 目标字符串 * @param source String 母字符串 * @return String 替换后的字符串 */ public static String replace(String from, String to, String source) { if (source == null || from == null || to == null) return null; StringBuffer bf = new StringBuffer(""); int index = -1; while ((index = source.indexOf(from)) != -1) { bf.append(source.substring(0, index) + to); source = source.substring(index + from.length()); index = source.indexOf(from); } bf.append(source); return bf.toString(); } /** * 替换字符串, 能能够在
HTML 页面上直接显示(替换双引号和小 于号) * * @param str String 原始字符串 * @return String 替换后的字符串 */ public static String htmlencode(String str) { if (str == null) { return null; } return replace("\"", "&;quot;", replace("<", "&;lt;", str)); } /**
* 替换字符
串,将被编码的转换成原始码(替换成双引号和小于 号) * * @param str String * @return String */ public static String htmldecode(String str) { if (str == null) { return null; } return replace("&;quot;", "\"", replace("&;lt;", "<", str)); } private static final String _BR = "
"; /** * 在页面上直接显示文本内容,替换小于号,空格,回车,TAB * * @param str String 原始字符串 * @return String 替换后的字符串 */ public static String htmlshow(String str) { if (str == null) { return null; } str = replace("<", "&;lt;", str); str = replace(" ", "&;nbsp;", str); str = replace("\r\n", _BR