注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

放飞自由

年轻人,如果三年的时间里,没有任何想法,他这一生,就基本这个样子,没有多大改变。

 
 
 

日志

 
 

6 Useful Java Snippets 6段有用的Java代码  

2013-07-25 20:53:23|  分类: JAVA后台 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
1. Converting Strings to ints and ints to Strings (int 转String String 转int)
Here’s a handy little string converter that you can use to turn strings to ints and vice versa!
int y = 10;
String x = String.valueOf(y); // int to string
int z = Integer.parseInt(x); // string to int

2. Getting the Current Method Name 获取当前方法名称
Sometimes you need to find out the name of the method you’re currently using. Here’s a handy way to do so!
String name = Thread.currentThread().getStackTrace()[1].getMethodName();
3. Convert A String To Date (String 转 Date)
Quite often you’ll need to convert a string date into a Date variable. Here’s a quick way of doing so with SimpleDateFormat! - See more at: 

String dateString = "July 1st, 2009";
Date dateVar = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(string);
System.out.println(dateVar);
4. Take A Screen Shot (Java截屏代码)
Here’s how to take a screen shot in Java- useful if you want to pop a screenshot button in somewhere! - See more at: 
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.Toolkit;

public void takeScreenshot(String file) throws Exception {
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rect = new Rectangle(size);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(rect);
ImageIO.write(image, "png", new File(fileName));
}
5. Get A Directory Listing 获取文件目录集合
Sometimes you need to get a directory listing, and you’re not entirely sure how to get one. Here’s a way to get a directory listing in Java! - See more at: 
File directory = new File("directoryName");
String[] subdirs = dir.list();

for (int x = 0; x < subdirs.length; x++) {
String filename = subdirs[x];
}
6. Generic Static Initializer 生成静态初始化方法
Sometimes you need a generic static initializer, but you don’t want to write one yourself. Here’s a generic snippet to help you build up faster!
public class MyClass {
public static String[] strings;
public static int index = 100;

static {
strings = new String[index];

for (int x = 0; x < index - 1; x++) {
strings[x] = null;
}
}
}
FROM: http://www.learncomputer.com/java-snippets/
  评论这张
 
阅读(98)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017