一.获取UUID
UUID uuid = UUID.randomUUID(); String str = uuid.toString(); // 去掉“-” String s = str.replaceAll("-", "");
二.获取固定格式的时间字符串
//获取当前时间对象 Date d = new Date(); //创建日期格式化类对象,”yyyy/MM/dd HH:mm:ss”是我们希望的日期出现格式 SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”); System.out.println(sdf.format(d));
三.获取[0, 100)之间的int整数
double d = Math.random(); int i = (int)(d*100);
四.把 Java util.Date 转成 sql.Date
java.util.Date utilDate = new java.util.Date(); java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
五.Java中将String转化为Int
String str = "123"; try { int a = Integer.parseInt(str); } catch (NumberFormatException e) { e.printStackTrace(); }
六.Java中将Int转String
int num=100; //1 String s1=""+num; System.out.println(s1); //2 String s2 =String.valueOf(num); System.out.println(s2); //3 Integer i =new Integer(num); String s3 =i.toString(); System.out.println(s3); //4 String s4 =Integer.toString(i); System.out.println(s4);
七.== 和 equals的使用
String s1,s2,s3 = "abc", s4 ="abc" ; s1 = new String("abc"); s2 = new String("abc"); s1==s2 是 false //两个变量的内存地址不一样,也就是说它们指向的对象不 一样, s1.equals(s2) 是 true //两个变量的所包含的内容是abc,故相等。
八.判断左边的对象是否是右边的类的实例
double obj=1; if(obj instanceof Double){ System.out.println("true"); }
九.Strng中常用方法
// 1.获取字符串的长度(字符的个数)-------length() String str = "adsfaxsdfas沙发上案发地方"; System.out.println(str.length()); // 18 // 2.获取指定索引处的字符----charAt(int index) String str = "adsfaxsdfas沙发上案发地方"; System.out.println(str.charAt(12)); // 发 // 3.获取str在字符串对象中第一次出现的索引-----String.indexOf(String str) String str1="012345"; String str2="23"; System.out.println( str1.indexOf(str2) ); // 2 //4.从start开始截取字符串 ---substring(int start) String str = "adsfaxsdfas沙发上案发地方"; System.out.println(str.substring(1)); // dsfaxsdfas沙发上发地方 //5.从start开始,到end结束截取字符串。包括start,不包括end ---- String substring(int start,int end) String str = "adsfaxsdfas沙发上案发地方"; System.out.println(str.substring(1, 12)); // dsfaxsdfas沙 //6.判断字符串对象是否以指定的字符开头,参数toffset为指定从哪个下标开始 ----startsWith(String prefix,int toffset) String str = "adsfaxsdfas沙发上案发地方"; System.out.println(str.startsWith("f", 3)); //true System.out.println(str.startsWith("f", 4)); // false //7.判断字符串对象是否以指定的字符结尾 ----endsWith(String str) String str = "adsfaxsdfas沙发上案发地方"; System.out.println(str.endsWith("x")); // false System.out.println(str.endsWith("方")); // true //8.判断指定字符串是否为空 ----isEmpty() //9.去除字符串两端空格 ----trim() String str3 = " a c e x u a n x u a n "; System.out.println(str3.trim()); System.out.println(str3); //10.去除字符串中指定的的字符,然后返回一个新的字符串 ----split() public static void main(String[] args) { String str = "adsfaxsdfas沙发上发地方"; String array[] = str.split("a"); printString(array); } public static void printString(String a[]) { for(int i=0;i<a.length;i++) { System.out.print(a[i]); } } // dsfxsdfs沙发上发地方 //11.截取字符串中指定位置的字符组成一个新的字符串 ----subSequence(int beginIndex,int endIndex ) String str = "adsfaxsdfas沙发上发地方"; System.out.println(str.subSequence(1, 10)); // dsfaxsdfa //12.将指定字符替换成另一个指定的字符 ----replace(char oldChar, char newChar) String str = "adsfaxsdfas沙发上发地方"; System.out.println(str.replace('a', 's')); // sdsfsxsdfss沙发上发地方 //13.用新的内容替换全部旧内容 ----replaceAll(String regex,String replasement) String str4 = "Hello,world!"; System.out.println(str4.replaceAll("l", "&")); // He&&o,wor&d! //14.返回指定字符出现的最后一次的下标 ----lastIndexOf(String str) String str4 = "Hello,world!"; System.out.println(str4.lastIndexOf("l")); // 9 //15.查看字符串中是都含有指定字符 ----contains(CharSequence s) String str4 = "Hello,world!"; System.out.println(str4.contains("l")); // true //16. 在原有的字符串的基础上加上指定字符串 ----concat(String str) String str5 = "dr"; System.out.println(str5.concat("eam")); // dream
十.单实例Singleton 示例
public class SimpleSingleton { private static SimpleSingleton singleInstance = new SimpleSingleton(); //Marking default constructor private //to avoid direct instantiation. private SimpleSingleton() { } //Get instance for class SimpleSingleton public static SimpleSingleton getInstance() { return singleInstance; } }
十一.得到当前方法的名字
String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
十二.列出文件和目录
File dir = new File("directoryName"); String[] children = dir.list(); if (children == null) { // Either dir does not exist or is not a directory } else { for (int i=0; i < children.length; i++) { // Get filename of file or directory String filename = children[i]; } } // It is also possible to filter the list of returned files. // This example does not return any files that start with `.'. FilenameFilter filter = new FilenameFilter() { public boolean accept(File dir, String name) { return !name.startsWith("."); } }; children = dir.list(filter); // The list of files can also be retrieved as File objects File[] files = dir.listFiles(); // This filter only returns directories FileFilter fileFilter = new FileFilter() { public boolean accept(File file) { return file.isDirectory(); } }; files = dir.listFiles(fileFilter);