`
风沙星辰
  • 浏览: 54715 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

Java常用工具类(字符编码,时间等)

    博客分类:
  • java
阅读更多


/**
     * 将UTF编码的字符串转化为GB2312编码的字符串,主要用来处理中文显示乱码的问题
     *
     * @param UTF
     *            通过UTF编码的字符串
     * @return 通过GB2312编码的字符串
     */
    public static String GB2312FromUTF(String UTF) {
        if (UTF == null) {
            return "";
        } else {
            try {
                return new String(UTF.getBytes("UTF-8"), "GB2312");
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 将GB2312编码的字符串转化为UTF-8编码的字符串,主要用来处理中文显示乱码的问题
     *
     * @param GB2312
     *            通过GB2312编码的字符串
     * @return 通过UTF-8编码的字符串
     */
    public static String UTFFromGB2312(String GB2312) {
        if (GB2312 == null) {
            return "";
        } else {
            try {
                return new String(GB2312.getBytes("GB2312"), "UTF-8");
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public static String GBKFromISO8859_1(String ISO8859_1) {
        if (ISO8859_1 == null) {
            return "";
        } else {
            try {
                return new String(ISO8859_1.getBytes("ISO8859_1"), "GBK");
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public static String GBKFromUTF(String UTF) {
        if (UTF == null) {
            return "";
        } else {
            try {
                return new String(UTF.getBytes("UTF-8"), "GBK");
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 将ISO8859_1编码的字符串转化为UTF-8编码的字符串,主要用来处理中文显示乱码的问题
     *
     * @param ISO8859_1str
     *            通过ISO8859_1编码的字符串
     * @return 通过UTF-8编码的字符串
     */
    public static String UTFFromISO8859_1(String ISO8859_1str) {
        return ISO8859_1str;
    }

    public static String UTFFromGBK(String GBK) {
        if (GBK == null) {
            return "";
        } else {
            try {
                return new String(GBK.getBytes("GBK"), "UTF-8");
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 将UTF-8编码的字符串转化为ISO8859_1编码的字符串,主要用来处理中文显示乱码的问题
     *
     * @param UTF
     *            通过UTF编码的字符串
     * @return 通过ISO8859_1编码的字符串
     */
    public static String ISO8859_1FromUTF(String UTFstr) {
        if (UTFstr == null) {
            return "";
        } else {
            try {
                return new String(UTFstr.getBytes("UTF-8"), "ISO8859_1");
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 将GB2312编码的字符串转化为ISO8859_1编码的字符串
     *
     * @param GBstr
     *            GB2312编码的字符串
     * @return ISO8859_1编码的字符串
     */
    public static String ISO8859_1String(String GBstr) {
        if (GBstr == null) {
            return "";
        } else {
            try {
                return new String(GBstr.getBytes("GB2312"), "ISO8859_1");
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 将GB2312编码的字符串转化为ISO8859_1编码的字符串
     *
     * @param GBstr
     *            GB2312编码的字符串
     * @return ISO8859_1编码的字符串
     */
    public String ISO8859_1FromGB2312(String GBstr) {
        if (GBstr == null) {
            return "";
        } else {
            try {
                return new String(GBstr.getBytes("GB2312"), "ISO8859_1");
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    public static String ISO8859_1FromGBK(String GBK) {
        if (GBK == null) {
            return "";
        } else {
            try {
                return new String(GBK.getBytes("GBK"), "ISO8859_1");
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 去除字符串两端空格。
     *
     * @param str
     *            需要处理的字符串
     * @return 去掉了两端空格的字符串,如果str 为 null 则返回 ""
     */
    public static String trim(String str) {
        if (str != null) {
            return str.trim();
        } else {
            return "";
        }
    }

    // static public String mm_dd_yyyy = "MM-dd-yyyy HH:mm:ss";
    /**
     * 获得当前年份
     *
     * @return 当前年份,格式如:2003
     */
    public static int getCurrentYear() {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
        return Integer.parseInt(sdf.format(new java.util.Date()));
    }

    /**
     * 获得当前月份
     *
     * @return 当前月份
     */
    public static int getCurrentMonth() {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("M");
        return Integer.parseInt(sdf.format(new java.util.Date()));
    }

    /**
     * 获得当前天
     *
     * @return 当前天
     */
    public static int getCurrentDay() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DATE);
    }

    public static String getCurrentDateTime() {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                "yyyy-MM-dd H:mm");
        return sdf.format(new Date());
    }

    /**
     * 获得形如 19770608 格式的当前年月日
     *
     * @return 当前年月日
     */
    public static String getSimpleCurrentDate() {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                "yyyyMMdd HH:mm:ss");
        return sdf.format(new java.util.Date());
    }

    /**
     * 返回两个日期相差天数
     *
     * @param d1
     *            日期
     * @param d2
     *            日期
     * @return 天数
     */
    public int diffDate(Date d1, Date d2) {
        if ((d1 == null) || (d2 == null))
            return 0;

        Calendar cal = Calendar.getInstance();

        // from Locale, has nothing to do with your input date format
        int zoneoffset = cal.get(Calendar.ZONE_OFFSET);
        int dstoffset = cal.get(Calendar.DST_OFFSET);

        // getTime() return absolute GMT time
        // compensate with the offsets
        long dl1 = d1.getTime() + zoneoffset + dstoffset;
        long dl2 = d2.getTime() + zoneoffset + dstoffset;

        int intDaysFirst = (int) (dl1 / (60 * 60 * 1000 * 24)); //60*60*1000
        int intDaysSecond = (int) (dl2 / (60 * 60 * 1000 * 24));

        return intDaysFirst > intDaysSecond ? intDaysFirst - intDaysSecond
                : intDaysSecond - intDaysFirst;
    }

    /**
     * 将给定的时间转换为格式是8位的字符串
     *
     * @param date
     *            给定的时间
     * @return 格式化后的字符串形式的时间
     */
    public String get8BitDate(java.util.Date date) {
        if (date == null) {
            return "";
        }
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                "yyyyMMdd");
        return sdf.format(date);
    }

    public String to_date(String strdate, String df) {
        if (strdate == null) {
            return "";
        }
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat(
                "M/d/yyyy H:m:s");
        Date d = null;
        try {
            d = sdf1.parse(strdate);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return sdf.format(d);
    }

    public static String get8BitString(String strDate) {
        if (strDate == null) {
            return "";
        }
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                "yyyy-MM-dd");
        java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat(
                "yyyyMMdd");
        Date d = null;
        try {
            d = sdf.parse(strDate);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return sdf2.format(d);
    }

    public static String get8ByteTo10Byte(String strDate) {
        if (strDate == null) {
            return "";
        }
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                "yyyyMMdd");
        java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat(
                "yyyy-MM-dd");
        Date d = null;
        try {
            d = sdf.parse(strDate);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return sdf2.format(d);
    }

    public static String getStandedDateTime(String strDate) {
        if (strDate == null) {
            return "";
        }
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                "yyyy-MM-dd");
        java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat(
                "yyyy-MM-dd");
        Date d = null;
        try {
            d = sdf.parse(strDate);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return sdf2.format(d);
    }

    public static String getMonthDay(java.util.Date date) {
        if (date == null) {
            return "";
        }
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("M月d日");
        return sdf.format(date);
    }

    public static String getHourMinute(java.util.Date date) {
        if (date == null) {
            return "";
        }
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("H:mm");
        return sdf.format(date);
    }

    /**
     * 判断字符串是否符合日期格式
     *
     * @param str
     *            字符串时间
     * @return
     */
    public static boolean isDate(String strDate) {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                "yyyy-MM-dd");
        sdf.setLenient(false);
        try {
            sdf.parse(strDate);
            return true;
        } catch (ParseException ex) {
            return false;
        }
    }

    /**
     * 判断是否是数字
     *
     * @param str
     * @return
     */
    public static boolean isNumber(String strNumber) {
        boolean bolResult = false;
        try {
            Double.parseDouble(strNumber);
            bolResult = true;
        } catch (NumberFormatException ex) {
            bolResult = false;
        }
        return bolResult;
    }

    public String dateadd(Date strDate, int a) {
        String str = "";

        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                "yyyy-MM-dd");
        String strDate1 = sdf.format(strDate);

        int year = Integer.parseInt(strDate1.substring(0, 4));
        int month = Integer.parseInt(strDate1.substring(5, 7));
        int day = Integer.parseInt(strDate1.substring(8, 10));
        int md = getdayformonth(month);
        int i = (day + a) / md;
        int j = (day + a) % md;
        if (j == 0) {
            i = i - 1;
            j = md;
        }
        String strmon = "";
        String strday = "";
        String mondiff = "";
        if (i < 2) {
            if (Integer.toString(j).length() == 1) {
                strday = "0" + Integer.toString(j);
            } else {
                strday = Integer.toString(j);
            }
            if ((month + i) > 12) {
                int yeardiff = (month + i) / 12;
                int monthmod = (month + i) % 12;
                mondiff = Integer.toString(monthmod);
                if (Integer.toString(monthmod).length() == 1) {
                    mondiff = "0" + Integer.toString(monthmod);
                }
                str = Integer.toString(year + yeardiff) + "-" + mondiff + "-"
                        + strday;
            } else {
                strmon = Integer.toString(month + i);
                if (Integer.toString(month + i).length() == 1) {
                    strmon = "0" + Integer.toString(month + i);
                }

                str = Integer.toString(year) + "-" + strmon + "-" + strday;

            }
        } else {
            //主要判断假如天数,月份溢出的处理,
        }
        return str;
    }

    public int getdayformonth(int month) {
        int a = 0;
        switch (month) {
        case 1:
            a = 31;
            break;
        case 2:
            a = 28;
            break;
        case 3:
            a = 31;
            break;
        case 4:
            a = 30;
            break;
        case 5:
            a = 31;
            break;
        case 6:
            a = 30;
            break;
        case 7:
            a = 31;
            break;
        case 8:
            a = 31;
            break;
        case 9:
            a = 30;
            break;
        case 10:
            a = 31;
            break;
        case 11:
            a = 30;
            break;
        case 12:
            a = 31;
            break;
        default:

        }
        return a;
    }

    public String addOneDay(String strDate) //YYYY-MM-DD
    {
        int[] standardDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        int[] leapyearDays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        int y = Integer.parseInt(strDate.substring(0, 4));
        int m = Integer.parseInt(strDate.substring(4, 6));
        int d = Integer.parseInt(strDate.substring(6,) + 1;
        int maxDateCount = 0;

        System.out.println(y);
        System.out.println(m);
        System.out.println(d);

        if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
            maxDateCount = leapyearDays[m - 1];
        } else {
            maxDateCount = standardDays[m - 1];
        }

        if (d > maxDateCount) {
            d = 1;
            m++;
        }

        if (m > 12) {
            m = 1;
            y++;
        }
        java.text.DecimalFormat yf = new java.text.DecimalFormat("0000");
        java.text.DecimalFormat mdf = new java.text.DecimalFormat("00");
        return yf.format(y) + mdf.format(m) + mdf.format(d);
    }

    public static String subOneDay(String strDate) {
        //YYYY-MM-DD
        int[] standardDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        int[] leapyearDays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        int y = Integer.parseInt(strDate.substring(0, 4));
        int m = Integer.parseInt(strDate.substring(4, 6));
        int d = Integer.parseInt(strDate.substring(6,) - 1;
        int maxDateCount = 0;

        System.out.println(y);
        System.out.println(m);
        System.out.println(d);

        if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
            maxDateCount = leapyearDays[m - 1];
        } else {
            maxDateCount = standardDays[m - 1];
        }

        if (d > maxDateCount) {
            d = 1;
            m++;
        }

        if (m > 12) {
            m = 1;
            y++;
        }
        java.text.DecimalFormat yf = new java.text.DecimalFormat("0000");
        java.text.DecimalFormat mdf = new java.text.DecimalFormat("00");
        return yf.format(y) + mdf.format(m) + mdf.format(d);
    }

    public static void main(String[] argv) {
        System.out.println(Tools.getMonthDay(new java.util.Date()));
        System.out.println(Tools.getHourMinute(new java.util.Date()));
     
    }
}
分享到:
评论

相关推荐

    Java常用工具类大全

    提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密...

    Java自动识别文件字符编码工具类.rar

    Java自动识别文件字符编码工具类 参考博客 https://blog.csdn.net/superbeyone/article/details/103036914 使用方式: String encode = EncodingDetect.getFileEncode(geoJsonFile); log.info("系统检测到文件[ {}...

    Java常用工具类大全,工作5年精心整理(最新版)

    提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密...

    java常用工具类

    文件工具类,Http请求工具类,图片处理工具类。...mail工具类,Map工具类,MD5编码工具类,数字工具类,随机数工具类,反射工具类,字符串处理工具类,URL工具类,XML工具类,常用的数据验证工具类

    java基础类库开发包,工作5年精心整理_Java常用工具类源码

    提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密...

    Java 所有字符串转UTF-8 万能工具类-GetEncode.java

    不需要关心接受的字符串编码是UTF_8还是GBK,还是ios-8859-1,自动转换为utf-8编码格式,无需判断字符串原有编码,用法://处理编码String newStr = GetEncode.transcode(oldStr);

    字符编码工具类CharacterEncodingFilter.java

    字符编码工具类CharacterEncodingFilter.java 字符编码工具类CharacterEncodingFilter.java 字符编码工具类CharacterEncodingFilter.java

    Java开发常用Util工具类

    字符串工具类/数据类型转换类/集合工具类/数组工具类/Properties文件操作类/常用流操作工具类/编码工具类/Json工具类/日期工具类/下载文件工具类/解压ZIP工具类/文件编码转码

    Java常用工具类大全.7z

    提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密...

    java常用工具大全

    提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密...

    一些java常用的工具类整合

    一些java常用的工具整合,包括 字符编码 ,获取服务器路径 ,获取类路径,数据库读取,支持自定义datatable类的数据库读取等类。

    java基础工具类iceroot

    java基础工具类iceroot iceroot是一个java基础工具类.封装了很多有用的方法. 该类库无任何第三方依赖. 涵盖了 字符串操作 时间转化 读取配置文件 等方面. 基础工具类对于java代码的编写是非常必要的,然 而很多...

    javaweb项目常用工具包

    Base64工具类-字符编码工具类-数据类型转换-日期工具类-Escape中文转码工具类-fastjson工具类-文件工具类-Http工具类-http请求工具类-用于模拟HTTP请求中GET/POST方式 -图片处理工具类-Ip工具类-mail工具类-Map工具...

    JAVA 转换字符编码工具

    NULL 博文链接:https://sammyfun.iteye.com/blog/1662240

    java工具类地址

    java常用的工具类收集;包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。

    java各种工具类,多年精华

    提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密...

    Java 工具类 包含一些常用的方法

    JavaUtil类中其中包含的方法有: ...5.将文件转换为指定字符编码集的字符串 6.获取指定类的随机实例(传入Class模板) 7.根据指定包名搜索文件 如需使用如上方法,需要将JavaUtil复制到项目中任意位置即可

    java utils 工具类

    java 工具类,包含上传下载工具类,数据库工具类,验证码工具类,分页工具类,字符编码过滤工具类,字符串工具类 提醒:我已打包,在/WEB-INF/lib/itbofeng-utils.jar中,可以看到源码。 测试下载文件时修改servlet...

    JAVA字符串操作类CTool.java字符转换类.rar

    JAVA字符串操作类CTool.java字符转换类,此类中收集Java编程中WEB开发常用到的一些工具。为避免生成此类的实例,构造方法被申明为private类型的。封装的功能:字符串从GBK编码转换为Unicode编码、对字符串进行md5...

    字符编码过滤器

    java过滤器实现统一字符编码 封装好的工具类,可以直接使用

Global site tag (gtag.js) - Google Analytics