Java常用代码片段汇总

来源:互联网 发布:linux创建hadoop用户 编辑:程序博客网 时间:2024/05/16 10:24

1.如何求两个整数的百分比

[java] view plaincopy
  1. int currentindex = 55;  
  2. int totalcount = 66;  
  3. NumberFormat nf = NumberFormat.getPercentInstance();  
  4. final String persent = nf.format(((float)currentindex/(float)totalcount));  


2.从一个链接中获取域名

[java] view plaincopy
  1. String url = "http://blog.csdn.net/zuolongsnail";  
  2. Pattern p = Pattern.compile(  
  3.         "(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)",  
  4.         Pattern.CASE_INSENSITIVE);  
  5. Matcher matcher = p.matcher(url);  
  6. matcher.find();  
  7. System.out.println(matcher.group());  


3.显示年月日时分秒星期

[java] view plaincopy
  1. Date date = new Date();  
  2. SimpleDateFormat dateformat = new SimpleDateFormat(  
  3.         "yyyy年MM月dd日 HH时mm分ss秒 E");  
  4. String dateStr = dateformat.format(date);  
  5. System.out.println(dateStr);  

4.显示年月日时分秒毫秒

[java] view plaincopy
  1. Date date = new Date();  
  2. SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS");  
  3. String dateStr = sdf.format(date);  
  4. System.out.println(dateStr); 
0 0
原创粉丝点击