原文:分享非常有用的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. Ext JS学习第五天 Ext_window组件(一)

    此文来记录学习笔记 •第一个组件:Ext.window.Window.对于组件,也就是Ext最吸引开发者的地方,那么我们要真正的使用Ext的组件,首先必须学会阅读API文档. –xtype:组件的别名 ...

  2. NSOperationQueue和GCD的区别

    使用NSOperationQueue用来管理子类化的NSOperation对象,控制其线程并发数目.GCD和NSOperation都可以实现对线程的管理,区别是 NSOperation和NSOpera ...

  3. Eclipse安装反编译插件JD(Java Decompiler)

    JD安装说明:*****Eclipse 插件安装*****1. 在网上搜索并下载jdeclipse_update_site.zip2. Eclipse -> Install New Softwa ...

  4. 函数重载不仅仅是看其参数,还要看是否有const修饰

    比如QString有两个函数,可以堂而皇之的存在,原因就在于有了const修饰以后,编译器不把两个函数当作同一个函数名了: QChar * data() const QChar * data() co ...

  5. Delphi 中 COM 实现研究手记(一)

    前言 前些日子用 Delphi 写了一个 Windows 外壳扩展程序,大家知道 Windows 外壳扩展实际上就是 COM 的一种应用 -- Shell COM,虽然整个程序写得还算比较顺利,但写完 ...

  6. 用C语言写一个程序,得出当前系统的整形数字长(16位,32位,64位)等,不能使用sizeof()

    #include <iostream>#include <cmath>using namespace std; int main(){ int num = -1; unsign ...

  7. Codeforces Round #262 (Div. 2) 460C. Present(二分)

    题目链接:http://codeforces.com/problemset/problem/460/C C. Present time limit per test 2 seconds memory ...

  8. Linux 多线程通信

    摘自资料(linux 与Windows不同) 线程间无需特别的手段进行通信,由于线程间能够共享数据结构,也就是一个全局变量能够被两个线程同一时候使用.只是要注意的是线程间须要做好同步,一般用mutex ...

  9. 免费edu邮箱申请注冊地址

    几个国外.edu邮箱注冊地址: 注冊地址:http://mail.alumni.fandm.edu/reg/reg_pangia.asp   @alumni.fandm.edu 注冊地址: http: ...

  10. MDK常见错误详解集合

    错误代码及错误信息 错误释义 error 1: Out of memory 内存溢出 error 2: Identifier expected 缺标识符 error 3: Unknown identi ...