[java] view plain copypackage com.util; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * 功能更強大的格式化工具類 */ public class FormatUtils { private static SimpleDateFormat second = new SimpleDateFormat( "yy-MM-dd hh:mm:ss"); private static SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd"); private static SimpleDateFormat detailDay = new SimpleDateFormat("yyyy年MM月dd日"); private static SimpleDateFormat fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); private static SimpleDateFormat tempTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static SimpleDateFormat ExcelDate = new SimpleDateFormat("yyyy/MM/dd"); /** * 格式化excel中的時間 * @param date * @return */ public static String formatDateForExcelDate(Date date) { return excelDate.format(date); } /** * 將日期格式化作為文件名 * @param date * @return */ public static String formatDateForFileName(Date date) { return fileName.format(date); } /** * 格式化日期(精確到秒) * * @param date * @return */ public static String formatDateSecond(Date date) { return second.format(date); } /** * 格式化日期(精確到秒) * * @param date * @return */ public static String tempDateSecond(Date date) { return tempTime.format(date); } public static Date tempDateSecond(String str) { try { return tempTime.parse(str); } catch (ParseException e) { e.printStackTrace(); } return new Date(); } /** * 格式化日期(精確到天) * * @param date * @return */ public static String formatDateDay(Date date) { return day.format(date); } /** * 格式化日期(精確到天) * * @param date * @return */ public static String formatDateDetailDay(Date date) { return detailDay.format(date); } /** * 將double類型的數字保留兩位小數(四舍五入) * * @param number * @return */ public static String formatNumber(double number) { DecimalFormat df = new DecimalFormat(); df.applyPattern("#0.00"); return df.format(number); } /** * 將字符串轉換成日期 * * @param date * @return * @throws Exception */ public static Date formateDate(String date) throws Exception { return day.parse(date); } /** * 將字符日期轉換成Date * @param date * @return * @throws Exception */ public static Date parseStringToDate(String date) throws Exception { return day.parse(date); } public static String formatDoubleNumber(double number) { DecimalFormat df = new DecimalFormat("#"); return df.format(number); }
文件上傳工具類 UploadUtil.java
[java] view plain copypackage com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Calendar; /** * 文件上傳工具類 * */ public class UploadUtil { private static final int BUFFER_SIZE = 16 * 1024; //保存圖片 public static synchronized void copy(File src, File newFile) { try { InputStream is = null; OutputStream os = null; try { is = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE); os = new BufferedOutputStream(new FileOutputStream(newFile), BUFFER_SIZE); byte[] buffer = new byte[BUFFER_SIZE]; while (is.read(buffer) > 0) { os.write(buffer); } } finally { if (null != is) { is.close(); } if (null != os) { os.close(); } } } catch (Exception e) { e.printStackTrace(); } } /** * 返回 年號+月號+天+時+分+秒+隨機碼 * @return */ @SuppressWarnings("static-access") public static synchronized String getTime() { Calendar calendar = Calendar.getInstance(); String year = calendar.get(calendar.YEAR) + ""; String month = (calendar.get(calendar.MONTH) + 1) + ""; String day = calendar.get(calendar.DAY_OF_MONTH) + ""; String hour = calendar.get(calendar.HOUR_OF_DAY) + ""; String minute = calendar.get(calendar.MINUTE) + ""; String second = calendar.get(calendar.SECOND) + ""; String milliSecond = calendar.get(calendar.MILLISECOND) + ""; int r = (int)(Math.random()*100000); String random = String.valueOf(r); return year + month + day + hour + minute + second + milliSecond + random+"a"; } }新聞熱點
疑難解答