java日期转换,转载自http://www.cnblogs.com/android-html5/archive/2012/05/12/2533652.html
package com.ttgbook.conver;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Conver {
//把日期转为字符串
public static String ConverToString(Date date)
{
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(date);
}
//把字符串转为日期
public static Date ConverToDate(String strDate) throws Exception
{
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.parse(strDate);
}
}
java中的转义字符
java 运算符优先级
java 壓縮
import java.util.*
public static void zipFiles(List<byte[]> inputFileBytes, String zipfile) {
byte[] buf = new byte[1024];
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipfile));
for (int j = 0; j < inputFileBytes.size(); j++) {
byte[] bytes = inputFileBytes.get(j);
InputStream in = new ByteArrayInputStream(bytes);
out.putNextEntry(new ZipEntry("文件" + j + ".doc"));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
System.out.println("压缩完成.");
} catch (Exception e) {
e.printStackTrace();
}
}
import java.util.*
public static void zipFiles(File[] srcfile, String zipfile) {
byte[] buf = new byte[1024];
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipfile));
for (int i = 0; i < srcfile.length; i++) {
InputStream in = new FileInputStream(srcfile[i]);
out.putNextEntry(new ZipEntry(srcfile[i].getName()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
System.out.println("压缩完成.");
} catch (Exception e) {
e.printStackTrace();
}
}
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
public static void zipFiles(List<byte[]> inputFileBytes, String zipfile) {
try {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
zipfile));
for (int j = 0; j < inputFileBytes.size(); j++) {
ZipEntry ze = new ZipEntry("文件" + (j + 1) + ".doc");
zos.putNextEntry(ze);
InputStream in = new ByteArrayInputStream(inputFileBytes.get(j));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
java下载图片
public void exportReceipt() throws IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
String baseUrl = getRequest().getSession().getServletContext()
.getRealPath("/");
FileInputStream in;
in = new FileInputStream(baseUrl + "tempImg/receipt.png");
HttpServletResponse response = (HttpServletResponse) facesContext
.getExternalContext().getResponse();
response.setContentType("png");
response.setHeader(
"Content-Disposition",
"attachment;"
+ "filename="
+ java.net.URLEncoder.encode(
getMessage("student.receipt", null) + ".png",
"UTF-8"));
try {
IOUtils.copy(in, response.getOutputStream());
} finally {
IOUtils.closeQuietly(in);
}
response.getOutputStream().flush();
facesContext.responseComplete();
}
java list快速初始化
List<String> summaryScoreMessages2 = Arrays.asList("comment", "notes",
"prize");
private static void fun2() {
String url1 = Test.class.getResource("").toString();
String url2 = Test.class.getResource("/").toString();
System.out.println(url1.replace(url2, ""));
}
java获取src目录下的文件
private String getCurrentClassUrl() {
String url1 = this.getClass().getResource("").toString();
String url2 = this.getClass().getResource("/").toString();
return url1.replace(url2, "");
}
private byte[] getIcon(String iconFileName) throws IOException {
String templatePath = getCurrentClassUrl() + "/" + iconFileName;
InputStream in = this.getClass().getClassLoader()
.getResourceAsStream(templatePath);
if (in != null) {
in.close();
}
return inputStreamToBytes(in);
}