博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字符串相关工具类
阅读量:4313 次
发布时间:2019-06-06

本文共 2701 字,大约阅读时间需要 9 分钟。

package com.taiping.ecms.car.utils;import java.util.Calendar;import java.util.Date;/** * 字符串相关工具类 * @author Alex * */public class StringUtils {        /**     * 格式化字符串第一个字符为大写     * @param field     * @return     */    public static String upperFirstString(String field){        if(field.length() <= 0 || field == null){            return field;        }        String firstLetter = field.substring(0, 1).toUpperCase();            String formatString = firstLetter + field.substring(1);                         return formatString;    }        /**     * 功能:在判定已经是正确的身份证号码之后,查找出此人性别     * @param idCard-身份证号码     * @return 1-男或者 2-女     */    public static String getSex(String idCard) {        String sex = "";        if (idCard.length() == 15) {            sex = idCard.substring(idCard.length() - 3, idCard.length());        } else {            sex = idCard.substring(idCard.length() - 4, idCard.length() - 1);        }        int sexNum = Integer.parseInt(sex) % 2;        if (sexNum == 0) {            return "2";        }        return "1";    }     /**     * 根据出生日期获取年龄     * @param birthDay     * @return     * @throws Exception     */    public  static int getAge(Date birthDay) throws Exception {         Calendar cal = Calendar.getInstance();         if (cal.before(birthDay)) {             throw new IllegalArgumentException(                 "The birthDay is before Now.It's unbelievable!");         }         int yearNow = cal.get(Calendar.YEAR);         int monthNow = cal.get(Calendar.MONTH);         int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);         cal.setTime(birthDay);           int yearBirth = cal.get(Calendar.YEAR);         int monthBirth = cal.get(Calendar.MONTH);         int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);           int age = yearNow - yearBirth;           if (monthNow <= monthBirth) {             if (monthNow == monthBirth) {                 if (dayOfMonthNow < dayOfMonthBirth) age--;             }else{                 age--;             }         }         return age;     }    /**     * 功能:在判定已经是正确的身份证号码之后,查找出此人出生日期     * @param idCard-身份证号码     * @return 出生日期 XXXX MM-DD     */      public static String getBirthday(String idCard) {        String Ain = "";        if (idCard.length() == 18) {            Ain = idCard.substring(0, 17);        } else if (idCard.length() == 15) {            Ain = idCard.substring(0, 6) + "19" + idCard.substring(6, 15);        }        String strYear = Ain.substring(6, 10);// 年份        String strMonth = Ain.substring(10, 12);// 月份        String strDay = Ain.substring(12, 14);// 日期        return strYear + "-" + strMonth + "-" + strDay;    }         }

 

转载于:https://www.cnblogs.com/sinosoft/p/11171261.html

你可能感兴趣的文章
Centos7安装tomcat8
查看>>
MySQL基本命令和常用数据库对象
查看>>
poj 1222 EXTENDED LIGHTS OUT(位运算+枚举)
查看>>
秘密:之所以不搞军事同盟,俄罗斯
查看>>
&#181;C/OS-II版本升级指南
查看>>
hibernate中持久化对象的生命周期(三态:自由态,持久态,游离态 之间的转换)...
查看>>
postgres出现Server doesn't listen错误解决办法
查看>>
linux shell学习--awk练习
查看>>
敏捷开发一千零一问系列之十二:敏捷实施的步骤?
查看>>
TCP三次握手机制中的seq和ack
查看>>
java内部类的定义原则
查看>>
2017年11月26日 C#流&&窗体对话框
查看>>
endl与\n的区别
查看>>
进程和线程概念及原理
查看>>
Dubbo超时重试机制带来的数据重复问题
查看>>
注解配置里的几个注解
查看>>
使ie678支持css3伪类选择器的插件
查看>>
题解报告:hdu 1212 Big Number(大数取模+同余定理)
查看>>
POJ 3624 Charm Bracelet
查看>>
ZOJ 2314 Reactor Cooling
查看>>