原文:分享非常有用的Java程序 (关键代码) (一)

 

分享一些非常有用的Java程序 (关键代码) ,希望对你有所帮助。

1.  得到当前方法的名字

String methodName = Thread.currentThread().getStackTrace()[1].getMethodName(); 

2. 转字符串到日期

java.util.Date = java.text.DateFormat.getDateInstance().parse(date String); 

或者是:

SimpleDateFormat format = new SimpleDateFormat( "dd.MM.yyyy" ); Date date = format.parse( myString ); 

3.使用JDBC链接Oracle

public class OracleJdbcTest   {       String driverClass = "oracle.jdbc.driver.OracleDriver";          Connection con;          public void init(FileInputStream fs) throws ClassNotFoundException, SQLException, FileNotFoundException, IOException       {           Properties props = new Properties();           props.load(fs);           String url = props.getProperty("db.url");           String userName = props.getProperty("db.user");           String password = props.getProperty("db.password");           Class.forName(driverClass);              con=DriverManager.getConnection(url, userName, password);       }          public void fetch() throws SQLException, IOException       {           PreparedStatement ps = con.prepareStatement("select SYSDATE from dual");           ResultSet rs = ps.executeQuery();              while (rs.next())           {               // do the thing you do           }           rs.close();           ps.close();       }          public static void main(String[] args)       {           OracleJdbcTest test = new OracleJdbcTest();           test.init();           test.fetch();       }   

4.把 Java util.Date 转成 sql.Date

java.util.Date utilDate = new java.util.Date();   java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); 

5.创建图片的缩略图

private void createThumbnail(String filename, int thumbWidth, int thumbHeight, int quality, String outFilename)           throws InterruptedException, FileNotFoundException, IOException       {           // load image from filename           Image image = Toolkit.getDefaultToolkit().getImage(filename);           MediaTracker mediaTracker = new MediaTracker(new Container());           mediaTracker.addImage(image, 0);           mediaTracker.waitForID(0);           // use this to test for errors at this point: System.out.println(mediaTracker.isErrorAny());              // determine thumbnail size from WIDTH and HEIGHT           double thumbRatio = (double)thumbWidth / (double)thumbHeight;           int imageWidth = image.getWidth(null);           int imageHeight = image.getHeight(null);           double imageRatio = (double)imageWidth / (double)imageHeight;           if (thumbRatio < imageRatio) {               thumbHeight = (int)(thumbWidth / imageRatio);           } else {               thumbWidth = (int)(thumbHeight * imageRatio);           }              // draw original image to thumbnail image object and           // scale it to the new size on-the-fly           BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);           Graphics2D graphics2D = thumbImage.createGraphics();           graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);           graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);              // save thumbnail image to outFilename           BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFilename));           JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);           JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);           quality = Math.max(0, Math.min(quality, 100));           param.setQuality((float)quality / 100.0f, false);           encoder.setJPEGEncodeParam(param);           encoder.encode(thumbImage);           out.close();   

6.使用iText JAR生成PDF

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class GeneratePDF {
public static void main(String[] args) {
try {
OutputStream file = new FileOutputStream(new File("C:\\Test.pdf"));
Document document = new Document();
PdfWriter.getInstance(document, file);
document.open();
document.add(new Paragraph("Hello Kiran"));
document.add(new Paragraph(new Date().toString()));
document.close();
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}


后续还将继续分享给大家一些有用的代码片段,敬请关注。--Hurry

版权声明:本文为博主原创文章,未经博主允许不得转载。

分享非常有用的Java程序 (关键代码) (一)的更多相关文章

  1. 分享非常有用的Java程序(关键代码)(七)---抓屏程序

    原文:分享非常有用的Java程序(关键代码)(七)---抓屏程序 import java.awt.Dimension; import java.awt.Rectangle; import java.a ...

  2. 分享非常有用的Java程序 (关键代码)(六)---解析/读取XML 文件(重要)

    原文:分享非常有用的Java程序 (关键代码)(六)---解析/读取XML 文件(重要) XML文件 <?xml version="1.0"?> <student ...

  3. 分享非常有用的Java程序 (关键代码)(五)---把 Array 转换成 Map

    原文:分享非常有用的Java程序 (关键代码)(五)---把 Array 转换成 Map import java.util.Map; import org.apache.commons.lang.Ar ...

  4. 分享非常有用的Java程序 (关键代码)(四)---动态改变数组的大小

    原文:分享非常有用的Java程序 (关键代码)(四)---动态改变数组的大小 /** * Reallocates an array with a new size, and copies the co ...

  5. 分享非常有用的Java程序 (关键代码) (二)---列出文件和目录

    原文:分享非常有用的Java程序 (关键代码) (二)---列出文件和目录 File dir = new File("directoryName"); String[] child ...

  6. 分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件

    原文:分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件 import java.util.zip.*; import java.io.*; public class Zip ...

  7. 分享非常有用的Java程序(关键代码)(八)---Java InputStream读取网络响应Response数据的方法!(重要)

    原文:分享非常有用的Java程序(关键代码)(八)---Java InputStream读取网络响应Response数据的方法!(重要) Java InputStream读取数据问题 ======== ...

  8. 20个非常有用的Java程序片段

    下面是20个非常有用的Java程序片段,希望能对你有用. 1. 字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric strin ...

  9. 整理:20个非常有用的Java程序片段

    下面是20个非常有用的Java程序片段,希望能对你有用. 1. 字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric strin ...

随机推荐

  1. IntelliJ IDEA 14 注册码生成器

    IntelliJ IDEA 14 注册码生成器 文件为Java代码 自己编译运行里面的程序输入名称然后就生成注册码了工具:http://yun.baidu.com/s/1cZKsA部分工具生成的注册码 ...

  2. git 无法添加文件夹下文件

    最近做项目时,发现无法提交某个子文件夹下的文件. google后发现可能是该子文件夹下有.git文件夹导致无法上传. 删除子文件夹下.git后,依然无法提交子文件夹下的文件. 继续google, 尝试 ...

  3. SQL数据库语句练习题目

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  4. 再谈协方差矩阵之主成分分析PCA

    上次那篇文章在理论层次介绍了下协方差矩阵,没准很多人觉得这东西用处不大,其实协方差矩阵在好多学科里都有很重要的作用,比如多维的正态分布,再比如今天我们今天的主角——主成分分析(Principal Co ...

  5. Android的回调模拟

    想要彻底理解安卓中用的回调,最好的办法是自己写一个类似的实现安卓中回调功能的实现方法. 我自己写了一个可以实现setOnClickListener回调的工程: 具体目录: 工程源码的具体地址:http ...

  6. Android 开发笔记 “SQLite”

    SQLite 入门与分析 1.SQLite介绍 自几十年前出现的商业应用程序以来,数据库就成为软件应用程序的主要组成部分.正与数据库管理系统非常关键一样,它们也变得非常庞大,并占用了相当多的系统资源, ...

  7. 服务器忘记mysql密码怎么修改?

    找到配置文件my.ini  ,然后将其打开,可以选择用记事本打开 打开后,搜索mysqld关键字 找到后,在mysqld下面添加skip-grant-tables,保存退出. PS:若提示不让保存时, ...

  8. (zz)Linux下Gcc生成和使用静态库和动态库详解

    http://blog.chinaunix.net/uid-23592843-id-223539.html

  9. Code(容斥,好题)

    Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi ...

  10. uboot编译: uboot编译配置和编译过程

    jz2440: 韦东山Linux视频第1期_裸板_UBoot_文件系统_驱动初步\第09课第2节 u-boot分析之Makefile结构分析.WMV <嵌入式linux完全开发手册> 15 ...