您的位置 首页 java

我收集的10个Java编程小技巧

编程小技巧

1、打印时间

 System.out.format("Local time: %tT", Calendar. getInstance ());

 System.out.format("%1$tY %1$tm %1$te", new Date());   

2、替换填充字符串

 MessageFormat.format("select {0} * from {1}", "booth","ykt_booth");
 private   static  final Pattern NAMES_PATTERN = Pattern.compile("\\{([^/]+?)\\}");
             Matcher matcher = NAMES_PATTERN.matcher("select {0} * from {1}");
 StringBuffer  sb = new StringBuffer();
while (matcher.find()) {
 String  match = matcher.group(1);
String replacement = Matcher.quoteReplacement("booth");
matcher.appendReplacement(sb, replacement);
}
matcher.appendTail(sb);
System.out.println(sb);
     c.String format = String.format("this is a '%s' example", "moumingcao");  

3、日期时间api

 LocalDate today = LocalDate.now(); //本月的第一天 
LocalDate firstday = today.with(TemporalAdjusters.firstDayOfMonth());       //本月的最后一天
LocalDate lastDay =today.with(TemporalAdjusters.lastDayOfMonth());  

4、复制文件

  private static  void   copy p File (File source, File target) {
        try (FileInputStream inStream = new FileInputStream(source);
             FileOutputStream  outStream = new FileOutputStream(target);
            FileChannel in = inStream.getChannel();
            FileChannel out = outStream.getChannel();) {
            in.transferTo(0, in.size(), out);
        } catch (IO Exception  e) {
            e.printStackTrace();
        }
    }  

5、计算页数的技巧

 int pageSize = (((Collection) data).size()  +  PAGE_SIZE  - 1) / PAGE_SIZE;  

6、给数字补位,4将变成000004

 String s=String.format("%0" + 6 + "d",4);  

7、NIO中的SelectKey(可以借鉴为支持多选方式的写法)

假如有一个用户,可以配置多个模式,如果每个模式按如下的方式定义。

 public static final int OP_READ = 1 << 0;
public static final int OP_WRITE = 1 << 2;
public static final int OP_CONNECT = 1 << 3;
public static final int OP_ACCEPT = 1 << 4;  

如果我们想要A既可以满足OP_READ,又满足OP_WRITE,那么可以给A配置这个值

 int s=OP_READ|OP_CONNECT;  

当我们在代码中判断A是否有OP_READ时可以这样判断

 if(OP_READ&s!=0){
    //代表A有OP_READ的权限
}  

8、集合删除的坑

 package designpatterns. iterator ;

import  java .util.ArrayList;
import java.util. Iterator ;

public class ArrayListRemove {
  
    public static void main(String[] args) {  
        ArrayList<String> list = new ArrayList<String>();
        list.add("a");  
        list.add("bb");  
        list.add("bb");  
        list.add("ccc");  
        list.add("ccc");  
        list.add("ccc");  
  
        remove(list);  
  
        for (String s : list) {  
            System.out.println("element : " + s);  
        }  
    }

    //错误示例1
    public static void remove(ArrayList<String> list) {
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            if (s.equals("bb")) {
                list.remove(s);
            }
        }
    }

//    错误示例2
//    public static void remove(ArrayList<String> list) {
//        for (String s : list) {
//            if (s.equals("bb")) {
//                list.remove(s);
//            }
//        }
//    }
    
//正确示例1
//    public static void remove(ArrayList<String> list) {
//        for (int i = list.size() - 1; i >= 0; i--) {
//            String s = list.get(i);
//            if (s.equals("bb")) {
//                list.remove(s);
//            }
//        }
//    }

//    正确示例2
//    public static void remove(ArrayList<String> list) {
//        Iterator<String> it = list.iterator();
//        while (it.hasNext()) {
//            String s = it.next();
//            if (s.equals("bb")) {
//                it.remove();
//            }
//        }
//    }
}    

9、取多个 boolean 结果的情况

 bResult |= ((Boolean) result);  

10、将一个数转为正数的廉价方法

 return  number  & 0x7fffffff;  

文章来源:智云一二三科技

文章标题:我收集的10个Java编程小技巧

文章地址:https://www.zhihuclub.com/196060.shtml

关于作者: 智云科技

热门文章

网站地图