收藏列表
- 全部 [32]
- 默认 [20]
- jquery [1]
- sql [1]
- download [1]
- insert [1]
- html标签 [1]
- 数据访问 [1]
- dateutil [1]
- servlet+common.io [1]
- httpstatu [1]
- ajax+jsp异步请求 [1]
- regkey [1]
- getpath [1]
标题 | 标签 | 来源 | |
日期转换工具类 | dateutil | ||
package com.appsafe.core.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Date Utility Class This is used to convert Strings to Dates and Timestamps * * <p> * <a href="DateUtil.java.html"><i>View Source</i></a> * </p> * * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a> Modified by * <a href="mailto:dan@getrolling.com">Dan Kibler </a> to correct time * pattern. Minutes should be mm not MM (MM is month). * @version $Revision: 1.5 $ $Date: 2004/10/05 07:20:13 $ */ public class DateUtil { // ~ Static fields/initializers // ============================================= private static Log log = LogFactory.getLog(DateUtil.class); private static String datePattern = "MM/dd/yyyy"; private static String timePatternHHmm = "HH:mm"; private static String timePatternHHmmss = "HH:mm:ss"; // private static String datepa = "yyyy-MM-dd"; private static String datePatternyyyyMMdd = "yyyyMMdd"; private static String datePatternyyMMdd = "yyMMdd"; private static String datePatternyy_MM_dd = "yyyy-MM-dd"; private static String datePattern_date_time = "yyyy-MM-dd HH:mm:ss"; private static String datePatternyyMMddhhmmss = "yyyyMMddHHmmss"; // ~ Methods // ================================================================ /** * Return default datePattern (MM/dd/yyyy) * * @return a string representing the date pattern on the UI */ public static String getDatePattern() { return datePattern; } /** * This method attempts to convert an Oracle-formatted date in the form * dd-MMM-yyyy to mm/dd/yyyy. * * @param aDate * date from database as a string * @return formatted string for the ui */ public static final String getDate(Date aDate) { SimpleDateFormat df = null; String returnValue = ""; if (aDate != null) { df = new SimpleDateFormat(datePattern); returnValue = df.format(aDate); } return (returnValue); } public static final String getNowDate() { SimpleDateFormat df = null; String returnValue = ""; Date aDate = new Date(); if (aDate != null) { df = new SimpleDateFormat(datePatternyy_MM_dd); returnValue = df.format(aDate); } return (returnValue); } /** * 取月份 */ public static final String getMonth(String time) { SimpleDateFormat df = null; String returnValue = ""; try { Date aDate = convertStringToDate(time); if (aDate != null) { df = new SimpleDateFormat("yyyy年MM月"); returnValue = df.format(aDate); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return (returnValue); } /** * This method attempts to convert an Oracle-formatted date in the form * yyyyMMdd * * @return */ public static final String getNowyyyyMMddDate() { SimpleDateFormat df = null; String returnValue = ""; Date aDate = new Date(); if (aDate != null) { df = new SimpleDateFormat(datePatternyyyyMMdd); returnValue = df.format(aDate); } return (returnValue); } /** * This method attempts to convert an Oracle-formatted date in the form * yyyy-MM-dd HH:mm:ss * * @return */ public static final String getNowDateTime() { SimpleDateFormat df = null; String returnValue = ""; Date aDate = new Date(); if (aDate != null) { df = new SimpleDateFormat(datePattern_date_time); returnValue = df.format(aDate); } return (returnValue); } /** * yyyyMMdd * * @param date * @return */ public static final String formatyyyyMMdd(String date) { SimpleDateFormat df = null; String returnValue = ""; if (date != null) { df = new SimpleDateFormat(datePatternyyyyMMdd); try { returnValue = df.format(convertStringToDate(date)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return (returnValue); } public static final String formatyyMMdd(String date) { SimpleDateFormat df = null; String returnValue = ""; if (date != null) { df = new SimpleDateFormat(datePatternyyMMdd); try { returnValue = df.format(convertStringToDate(date)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return (returnValue); } public static final String formatyyyy_MM_dd(String date) { SimpleDateFormat df = null; String returnValue = ""; if (date != null) { df = new SimpleDateFormat(datePatternyy_MM_dd); try { returnValue = df.format(convertStringToDate(date)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return (returnValue); } /** * This method generates a string representation of a date/time in the * format you specify on input * * @param aMask * the date pattern the string is in * @param strDate * a string representation of a date * @return a converted Date object * @see java.text.SimpleDateFormat * @throws ParseException */ public static final Date convertStringToDate(String aMask, String strDate) throws ParseException { SimpleDateFormat df = null; Date date = null; df = new SimpleDateFormat(aMask); if (log.isDebugEnabled()) { log.debug("converting '" + strDate + "' to date with mask '" + aMask + "'"); } try { date = df.parse(strDate); } catch (ParseException pe) { // log.error("ParseException: " + pe); throw new ParseException(pe.getMessage(), pe.getErrorOffset()); } return (date); } /** * This method returns the current date time in the format: HH:MM a * * @param theTime * the current time * @return the current date/time */ public static String getTimeNowHHmm(Date theTime) { return getDateTime(timePatternHHmm, theTime); } /** * This method returns the current date time in the format: HH:MM:ss a * * @param theTime * the current time * @return the current date/time */ public static String getTimeNowHHmmss(Date theTime) { return getDateTime(timePatternHHmmss, theTime); } public static String getTimeNowHHmmss() { return getDateTime(timePatternHHmmss, new Date()); } /** * This method returns the current date in the format: MM/dd/yyyy * * @return the current date * @throws ParseException */ public static Calendar getToday() throws ParseException { Date today = new Date(); SimpleDateFormat df = new SimpleDateFormat(datePattern); // This seems like quite a hack (date -> string -> date), // but it works ;-) String todayAsString = df.format(today); Calendar cal = new GregorianCalendar(); cal.setTime(convertStringToDate(todayAsString)); return cal; } /** * This method generates a string representation of a date's date/time in * the format you specify on input * * @param aMask * the date pattern the string is in * @param aDate * a date object * @return a formatted string representation of the date * * @see java.text.SimpleDateFormat */ public static final String getDateTime(String aMask, Date aDate) { SimpleDateFormat df = null; String returnValue = ""; if (aDate == null) { log.error("aDate is null!"); } else { df = new SimpleDateFormat(aMask); returnValue = df.format(aDate); } return (returnValue); } /** * This method generates a string representation of a date based on the * System Property 'dateFormat' in the format you specify on input * * @param aDate * A date to convert * @return a string representation of the date */ public static final String convertDateToStringDatepa(Date aDate) { return getDateTime(datePatternyy_MM_dd, aDate); } public static final String convertDateToStringByYYMMDD(Date aDate) { return getDateTime(datePatternyyyyMMdd, aDate); } /** * This method generates a string representation of a date based on the * System Property 'dateFormat' in the format you specify on input * * @param aDate * A date to convert * @return a string representation of the date */ public static final String convertDateToString(Date aDate) { return getDateTime(datePatternyy_MM_dd, aDate); } /** * This method generates a string representation of a date based on the * System Property 'dateFormat' in the format you specify on input * * @param aDate * A date to convert * @return a string representation of the date */ public static final String convertDateToStringymdhhmmss(Date aDate) { return getDateTime(datePattern_date_time, aDate); } /** * This method converts a String to a date using the datePattern * * @param strDate * the date to convert (in format MM/dd/yyyy) * @return a date object * * @throws ParseException */ public static Date convertStringToDate(String strDate) throws ParseException { Date aDate = null; try { if (log.isDebugEnabled()) { log.debug("converting date with pattern: " + datePatternyy_MM_dd); } aDate = convertStringToDate(datePatternyy_MM_dd, strDate); } catch (ParseException pe) { log.error("Could not convert '" + strDate + "' to a date, throwing exception"); pe.printStackTrace(); throw new ParseException(pe.getMessage(), pe.getErrorOffset()); } return aDate; } /** * add by lion * This method converts a String to a date using the datePattern * * @param strDate * the date to convert (in format datePattern_date_time) * @return a date object * * @throws ParseException */ public static Date convertyyMMddhhmmssToDate(String strDate) throws ParseException { Date aDate = null; try { if (log.isDebugEnabled()) { log.debug("converting date with pattern: " + datePattern_date_time); } aDate = convertStringToDate(datePattern_date_time, strDate); } catch (ParseException pe) { log.error("Could not convert '" + strDate + "' to a date, throwing exception"); pe.printStackTrace(); throw new ParseException(pe.getMessage(), pe.getErrorOffset()); } return aDate; } /** * This method converts a String to a date using the datePattern * * @param strDate * the date to convert (in format MM/dd/yyyy HH:mm:ss) * @return a date object * * @throws ParseException */ public static Date convertStringToDateymdhhmmss(String strDate) throws ParseException { Date aDate = null; try { if (log.isDebugEnabled()) { log.debug("converting date with pattern: " + datePattern_date_time); } aDate = convertStringToDate(datePattern_date_time, strDate); } catch (ParseException pe) { log.error("Could not convert '" + strDate + "' to a date, throwing exception"); pe.printStackTrace(); throw new ParseException(pe.getMessage(), pe.getErrorOffset()); } return aDate; } /** * 时间减法 */ public static String timeReduce(String date, int reduceNumber) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); String t = ""; try { Date dt = sdf.parse(date); t = sdf.format(new Date(dt.getTime() - reduceNumber * 24 * 60 * 60 * 1000)); // System.out.println(t); } catch (ParseException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return t; } /** * 将长时间格式字符串转换为字符串 yyyy-MM-dd HH:mm:ss * * @return */ public static String longToStrng(long time) { // long型转换成的字符串 Date date = new Date(time); SimpleDateFormat formatter = new SimpleDateFormat(datePattern_date_time); String dateString = formatter.format(date); // System.out.println("TIME:::" + dateString); return dateString; } /** * 将长时间格式字符串转换为字符串 yyyyMMddHHmmss * * @return */ public static String longToyyyyMMddHHmmss(long time) { // long型转换成的字符串 Date date = new Date(time); SimpleDateFormat formatter = new SimpleDateFormat( datePatternyyMMddhhmmss); String dateString = formatter.format(date); // System.out.println("TIME:::" + dateString); return dateString; } /** * 时间区间所包含的日期. * * @param startDate * @param endDate */ public String[] formatDate(String startDate, String endDate) { String[] date = null; if (startDate.equals(endDate)) {// 同一天. date = new String[] { endDate }; } else if (startDate.substring(5, 7).equals(endDate.substring(5, 7))) {// 不同天,同一月. int a = Integer.parseInt(endDate.substring(8)) - Integer.parseInt(startDate.substring(8)); date = new String[a + 1]; for (int i = 0; i <= a; i++) { date[i] = startDate.substring(0, 8) + (Integer.parseInt(startDate.substring(8)) + i); } } else {// 不同天,不同月. String d[] = endDate.toString().split("-"); int yy = Integer.parseInt(d[0]); int mm = Integer.parseInt(d[1]); int dd = Integer.parseInt(d[2]); mm = mm - 1; if (mm == 2) { dd = 28; } else if (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12) { dd = 31; } else { dd = 30; } // 判断闰年 boolean r = yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0; if (r && mm == 2) dd++; String temp = String.valueOf(yy) + "-" + String.valueOf(mm < 10 ? "0" + mm : mm) + "-" + String.valueOf(dd); int a = Integer.parseInt(temp.substring(8)) - Integer.parseInt(startDate.substring(8)); int b = Integer.parseInt(endDate.substring(8)) - 1; date = new String[a + b + 2]; for (int i = 0; i <= a; i++) { date[i] = startDate.substring(0, 8) + (Integer.parseInt(startDate.substring(8)) + i); } for (int i = 0; i <= b; i++) { date[a + i + 1] = endDate.substring(0, 8) + ((i + 1) < 10 ? "0" + (i + 1) : (i + 1)); } } for (int i = 0; i < date.length; i++) { System.out.println(date[i]); } return date; } /** * 获得两个日期之间相差的天数 * * @param time1:开始时间 * @param time2:结束时间 * @return:两个日期之间相差的天数 */ public static long getQuot(String time1, String time2) { long quot = 0; SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd"); try { Date date1 = ft.parse(time1); Date date2 = ft.parse(time2); quot = date2.getTime() - date1.getTime(); quot = quot / 1000 / 60 / 60 / 24; } catch (ParseException e) { e.printStackTrace(); } return quot; } } |
|||
文件上传 | servlet+common.io | http://www.quanxi.com | |
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("=================文件上传来了================="); boolean isMultipart = ServletFileUpload.isMultipartContent(request); // 若果是的话 if (isMultipart) { /** 为该请求创建一个DiskFileItemFactory对象,通过它来解析请求。执行解析后,所有的表单项目都保存在一个List中。* */ try { DiskFileItemFactory factory = new DiskFileItemFactory(); File tempPathFile = new File("g:/file"); File per = new File("g:/file"); factory.setSizeThreshold(4048); // 设置缓冲区大小,这里是4kb factory.setRepository(tempPathFile);// 设置缓冲区目录 ServletFileUpload upload = new ServletFileUpload(factory); upload.setHeaderEncoding("UTF-8");// 解决文件乱码问题 upload.setSizeMax(40480000);// 设置最大文件尺寸 List<FileItem> items = upload.parseRequest(request); // 检查是否符合上传的类型 Iterator<FileItem> itr = items.iterator();// 所有的表单项 // 保存文件 while (itr.hasNext()) { FileItem item = (FileItem) itr.next();// 循环获得每个表单项 if (!item.isFormField()) {// 如果是文件类型 String name = item.getName();// 获得文件名 包括路径啊 if (name != null&&!"".equals(name)) { int index=name.indexOf("."); name=name.substring(index); name=UUID.randomUUID().toString()+name; System.out.println("====name:"+name); File savedFile = new File(per, name); item.write(savedFile); } } } } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("the enctype must be multipart/form-data"); } System.out.println("============文件上传成功=================="); } |