java 基本操作1


输出当时日期

package com.goodhope.theWorkOfFollowUp;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

    public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String time=df.format(date);
        System.out.println(time);

        //OR--
        //Calendar cal = Calendar.getInstance();
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月d日    E");
        //System.out.println(sdf.format(cal.getTime()));
        //打印结果如:2011年11月1日    星期二
    }
}

随机获取字符串

StringBuffer buffer = new StringBuffer("_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
Random r = new Random();
int range = buffer.length();
String s = "";
for (int b = 0; b < 50; b++) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < 10; i++) {
        sb.append(buffer.charAt(r.nextInt(range)));
    }
    s += sb;
}

线程暂停和恢复

暂停:suspend()
恢复:resume()
停止:stop()

for循环下要new东西的话,必需加括号

正则匹配数字

Pattern pattern = Pattern.compile("regex");
Matcher matcher = pattern.matcher("str");
matcher.matches();

//OR---------------------------------
Pattern.matches("regex", "str");

implements

implements是一个类实现一个接口用的关键字,他是用来实现接口中定义的抽象方法。比如:people是一个接口,他里面有say这个方法。

public interface people()
{
   public say();
}

但是接口没有方法体。只能通过一个具体的类去实现其中的方法体。比如chinese这个类,就实现了people这个接口。

public class chinese implements peopel {
    public say() {
        System.out.println("你好!");
    }
}

implements 用于实现接口 interface一个类 implemengts一个接口后,必须实现接口的所有方法,当然方法你可以暂时不写代码

try catch

try{  
    在这里写你想要做的代码  
}catch(异常变量){  
    异常处理  
} 

遍历Map的四种方法

public static void main(String[] args) {

  Map map = new HashMap();
  map.put("1", "value1");
  map.put("2", "value2");
  map.put("3", "value3");
  
  //第一种:普遍使用,二次取值
  System.out.println("通过Map.keySet遍历key和value:");
  for (String key : map.keySet()) {
   System.out.println("key= "+ key + " and value= " + map.get(key));
  }
  
  //第二种
  System.out.println("通过Map.entrySet使用iterator遍历key和value:");
  Iterator> it = map.entrySet().iterator();
  while (it.hasNext()) {
   Map.Entry entry = it.next();
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }
  
  //第三种:推荐,尤其是容量大时
  System.out.println("通过Map.entrySet遍历key和value");
  for (Map.Entry entry : map.entrySet()) {
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }

  //第四种
  System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
  for (String v : map.values()) {
   System.out.println("value= " + v);
  }
 }

得到txt的编码

InputStream in = new FileInputStream("E:\\Zexcel\\aa.txt");
InputStreamReader ir = new InputStreamReader(in);
System.out.println(ir.getEncoding()); 

线程异步调用

@Test
public void innerTransAsynThreadLargeTest() {

    new Thread(new Runnable() {

        @Override
        public void run() {

            for (int i = 0; i < 100; i++) {
                System.out.println("bbbbb");
            }

        }
    }).start();

    new Thread(new Runnable() {

        @Override
        public void run() {

            for (int i = 0; i < 100; i++) {
                System.out.println("aaaa");
            }

        }
    }).start();

    try {
        Thread.sleep(60000L);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

注意如果不给休眠时间,线程没有执行完,线程内的东西不会打印,

Joyce /
Published under (CC) BY-NC-SA in categories java  tagged with date  string  random  thread  regex  implements