我收集的10个编程小技巧

1,125 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第11天,点击查看活动详情

编程小技巧

  1. 打印时间
System.out.format("Local time: %tT", Calendar.getInstance());

 System.out.format("%1$tY %1$tm %1$te", new Date()); 
  1. 替换填充字符串
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");
  1. 日期时间api
LocalDate today = LocalDate.now(); //本月的第一天 
LocalDate firstday = today.with(TemporalAdjusters.firstDayOfMonth());       //本月的最后一天
LocalDate lastDay =today.with(TemporalAdjusters.lastDayOfMonth());
  1. 复制文件
 private static void copypFile(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 (IOException e) {
            e.printStackTrace();
        }
    }
  1. 计算页数的技巧
int pageSize = (((Collection) data).size()  +  PAGE_SIZE  - 1) / PAGE_SIZE;
  1. 给数字补位,4将变成000004
String s=String.format("%0" + 6 + "d",4);
  1. 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的权限
}
  1. 集合删除的坑
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();
//            }
//        }
//    }
}  
  1. 取多个boolean结果的情况
bResult |= ((Boolean) result);
  1. 将一个数转为正数的廉价方法
return number & 0x7fffffff;