论坛首页 Web前端技术论坛

常用JAVA工具类

浏览 2976 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-11-26  
获取访问者ip

             /**
             * 获取访问者ip
             */
             public static String getUserIp(HttpServletRequest request) {
                        String ip = request.getHeader( "x-forwarded-for");
                         if ((ip == null) || (ip.length() == 0)
                                                || "unknown".equalsIgnoreCase(ip)) {
                                    ip = request.getHeader( "Proxy-Client-IP");
                        }
                         if ((ip == null) || (ip.length() == 0)
                                                || "unknown".equalsIgnoreCase(ip)) {
                                    ip = request.getHeader( "WL-Proxy-Client-IP");
                        }
                         if ((ip == null) || (ip.length() == 0)
                                                || "unknown".equalsIgnoreCase(ip)) {
                                    ip = request.getRemoteAddr();
                        }
                         return ip;
            }

获取用户浏览器类型

             /**
             * 获取用户浏览器类型
             */
             public static String getUserBrowerType(HttpServletRequest request) {
                         // String userAgent = request.getHeader("USER-AGENT").toLowerCase();
                         // return userAgent.split(";")[1];
                         return request.getHeader( "USER-AGENT");
            }


获取时间差

             /**
             * 获取相差n月的月份的第一天或最后一天,格式:yyyyMMdd
             *
             * @param diff
             *            月份差值
             * @param type
             *            ('first':该月第一天,"last":该月最后一天)
             * @return yyyyMMdd
             */
             public static String getDayByDiff( int diff, String type) {
                        Calendar c = Calendar. getInstance();
                        c.add(Calendar. MONTH, diff);
                        Calendar calendar = new GregorianCalendar(c.get(Calendar. YEAR), c
                                                .get(Calendar. MONTH), c.get(Calendar.DAY_OF_MONTH ));
                        String day = "";
                         if ( "first".equals(type)) {
                                    day = "0"
                                                            + String.valueOf(calendar
                                                                                    .getActualMinimum(Calendar. DAY_OF_MONTH));
                        } else {
                                    day = String. valueOf(calendar
                                                            .getActualMaximum(Calendar. DAY_OF_MONTH));
                        }
                         return new SimpleDateFormat( "yyyy-MM").format(calendar.getTime()) + "-"
                                                + day;
            }


             /**
             * 获取 -n或+n个年的年份yyyy
             *
             * @param diff
             * @return
             */
             public static String getYearByDiff( int diff) {
                        Calendar c = Calendar. getInstance();
                        c.add(Calendar. YEAR, diff);
                         return new SimpleDateFormat( "yyyy").format(c.getTime());
            }

             /**
             * 获取 -n或+n个月的日期,格式:yyyy -MM
             *
             * @param diff
             *            月份差值
             * @return yyyy- MM
             */
             public static String getMonthByDiff( int diff) {
                        Calendar calendar = Calendar. getInstance();
                        calendar.add(Calendar. MONTH, diff);
                         return new SimpleDateFormat( "yyyy-MM").format(calendar.getTime());
            }

获取前两个月的时间

             /**
             * 获取前两个月的时间
             *
             * @param upMonthTime
             *            (格式:2011 -09)
             * @return
             */
             public static String getUpThreeTime(String upMonthTime) {
                         int queryYear = Integer. parseInt(upMonthTime.substring(0, 4));
                         int queryMonth = Integer. parseInt(upMonthTime.substring(5, 7));

                        String upQueryDateString = null;
                         if ( "01".equals(upMonthTime.substring(5, 7))) {
                                    queryYear = queryYear - 1;
                                    queryMonth = 11;
                                    upQueryDateString = Integer. toString(queryYear) + "-"
                                                            + Integer.toString(queryMonth);
                        } else if ( "02".equals(upMonthTime.subSequence(5, 7))) {
                                    queryYear = queryYear - 1;
                                    queryMonth = 12;
                                    upQueryDateString = Integer. toString(queryYear) + "-"
                                                            + Integer.toString(queryMonth);
                        } else if (queryMonth > 2 && queryMonth < 12) {
                                    queryMonth = queryMonth - 2;
                                    upQueryDateString = Integer. toString(queryYear) + "-0"
                                                            + Integer.toString(queryMonth);
                        } else {
                                    queryMonth = queryMonth - 2;
                                    upQueryDateString = Integer. toString(queryYear) + "-"
                                                            + Integer.toString(queryMonth);
                        }
                         return upQueryDateString;
            }


截取、合并数组

             /**
             * 截取子数组
             *
             * @param array
             * @param startIndexInclusive
             * @param endIndexExclusive
             * @return
             */
             public static byte[] subarray( byte[] array, int startIndexInclusive,
                                     int endIndexExclusive) {
                         if (array == null) {
                                     return null;
                        }
                         if (startIndexInclusive < 0) {
                                    startIndexInclusive = 0;
                        }
                         if (endIndexExclusive > array. length) {
                                    endIndexExclusive = array. length;
                        }
                         int newSize = endIndexExclusive - startIndexInclusive;
                         if (newSize <= 0) {
                                     return new byte[0];
                        }

                         byte[] subarray = new byte[newSize];
                        System. arraycopy(array, startIndexInclusive, subarray, 0, newSize);
                         return subarray;
            }

             /**
             * 合并byte数组
             *
             * @param array1
             * @param array2
             * @return
             */
             public static byte[] addAll( byte[] array1, byte... array2) {
                         if (array1 == null) {
                                     return null;
                        } else if (array2 == null) {
                                     return null;
                        }
                         byte[] joinedArray = new byte[array1. length + array2. length];
                        System. arraycopy(array1, 0, joinedArray, 0, array1. length);
                        System. arraycopy(array2, 0, joinedArray, array1.length, array2.length);
                         return joinedArray;
            }



时间格式

             /**
             * 根据指定的日期格式生成日期时间串
             *
             * @param format
             *            日期格式,如yyyyMMdd
             * @return dateTimeString
             */
             public static String formatDatetime(Date date, String format) {
                        DateFormat df = new SimpleDateFormat(format);
                         return df.format(date);
            }

             /**
             * 将给定的日期格式化为"yyyy -MM -dd HH:mm:ss"
             *
             * @param date
             *            日期
             * @return 格式为"yyyy- MM- dd HH:mm:ss"的String类型的时间串
             */
             public static String formatDatetime(Date date) {
                         return formatDatetime(date, "yyyy-MM-dd HH:mm:ss");
            }

             /**
             * 根据指定的格式返回当前日期时间
             *
             * @param format
             *            指定的日期格式
             * @return String类型的时间
             */
             public static String formatDatetime(String format) {
                         return formatDatetime(new Date(), format);
            }

             /**
             * 返回当前标准的日期时间
             *
             * @return 格式为"yyyy- MM- dd HH:mm:ss"的当前时间
             */
             public static String formatDatetime() {
                         return formatDatetime(new Date(), "yyyy-MM-dd HH:mm:ss");
            }

转码

             /**
             * 编码转换ISO8859 -1 -> UTF -8
             *
             * @param source
             * @return
             * @throws UnsupportedEncodingException
             */
             public static String isoToUtf8(String source)
                                     throws UnsupportedEncodingException {
                         return null == source ? "" : new String(source.getBytes( "ISO8859-1"),
                                                 "utf-8");
            }

             /**
             * 编码转换GB2312 -> ISO8859 -1
             *
             * @param source
             * @return
             * @throws UnsupportedEncodingException
             */
             public static String gb2312ToISO(String source)
                                     throws UnsupportedEncodingException {
                         return null == source ? "" : new String(source.getBytes( "gb2312"),
                                                 "ISO8859-1");
            }

             /**
             * 编码转换ISO8859 -1 -> GB2312
             *
             * @param source
             * @return
             * @throws UnsupportedEncodingException
             */
             public static String isoToGb2312(String source)
                                     throws UnsupportedEncodingException {
                         return null == source ? "" : new String(source.getBytes( "ISO8859-1"),
                                                 "gb2312");
            }

论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics