Java常用代码片段汇总
2014-10-07 01:04
435 查看
1.如何求两个整数的百分比
[java]
view plaincopy
int currentindex = 55;
int totalcount = 66;
NumberFormat nf = NumberFormat.getPercentInstance();
final String persent = nf.format(((float)currentindex/(float)totalcount));
2.从一个链接中获取域名
[java]
view plaincopy
String url = "http://blog.csdn.net/zuolongsnail";
Pattern p = Pattern.compile(
"(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)",
Pattern.CASE_INSENSITIVE);
Matcher matcher = p.matcher(url);
matcher.find();
System.out.println(matcher.group());
3.显示年月日时分秒星期
[java]
view plaincopy
Date date = new Date();
SimpleDateFormat dateformat = new SimpleDateFormat(
"yyyy年MM月dd日 HH时mm分ss秒 E");
String dateStr = dateformat.format(date);
System.out.println(dateStr);
4.显示年月日时分秒毫秒
[java]
view plaincopy
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS");
String dateStr = sdf.format(date);
System.out.println(dateStr);
[java]
view plaincopy
int currentindex = 55;
int totalcount = 66;
NumberFormat nf = NumberFormat.getPercentInstance();
final String persent = nf.format(((float)currentindex/(float)totalcount));
2.从一个链接中获取域名
[java]
view plaincopy
String url = "http://blog.csdn.net/zuolongsnail";
Pattern p = Pattern.compile(
"(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)",
Pattern.CASE_INSENSITIVE);
Matcher matcher = p.matcher(url);
matcher.find();
System.out.println(matcher.group());
3.显示年月日时分秒星期
[java]
view plaincopy
Date date = new Date();
SimpleDateFormat dateformat = new SimpleDateFormat(
"yyyy年MM月dd日 HH时mm分ss秒 E");
String dateStr = dateformat.format(date);
System.out.println(dateStr);
4.显示年月日时分秒毫秒
[java]
view plaincopy
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS");
String dateStr = sdf.format(date);
System.out.println(dateStr);