日期工具

时间戳格式的日期与8位格式转换,日期格式数据与字符串形式的转换。

public final class DateUtils {

    //时间戳转化为8位格式
    public static String stampToDate(String stamp){
        String date = "";
        if(StringUtil.isNotNull(stamp)){
            Date date1 = null;
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                ////parse:把String型的字符串转换成特定格式的date类型
                date1 = dateFormat.parse(stamp);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            date = format.format(date1);
        }
        return date;
    }

    //8位格式转化为时间戳
    public static String dateToStamp(String date){
        String stamp = "";
        if(StringUtil.isNotNull(date)){
            Date date1 = null;
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            try {
                ////parse:把String型的字符串转换成特定格式的date类型
                date1 = dateFormat.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = format.format(date1);
        }
        return stamp;
    }

    //性别转换,男女转F,M
    public static String toOnline(String gender){
        String genderJudge = "";
        if(gender!=null){
            if (("M").equals(gender)) {
                genderJudge = "1";
            } else if (("F").equals(gender)) {
                genderJudge = "0";
            } else {
                genderJudge = "";
            }
        }else{
            genderJudge = "";
        }
        return genderJudge;
    }

    //性别转换,男女转1,2
    public static String toOffline(String gender){
        String genderJudge = "";
        if (gender.equals("1")) {
            genderJudge = "M";
        } else if (gender.equals("0")) {
            genderJudge = "F";
        } else {
            genderJudge = "";
        }
        return genderJudge;
    }
}

 

posted @ 2017-09-04 11:03  话说当初  阅读(159)  评论(0编辑  收藏  举报