用Java将一个XX年XX月XX天变成一个6位的字符串。

如:12年转换后120000;12年1天 转换后是 120001,12年1月12天转换后是120112,
12年3月 转换后 是120300. 3天转换后就是 000003 。没有年前面就是00,没有月中间两位就是00 麻烦帮我写一个方法用于这样的转换。

    import com.jfinal.kit.StrKit;

public class DataTransfer {
public String getxq(String xqms) {
int indexof1 = 0;
int indexof2 = 0;
String n = "";// 年
String y = "";// 月
String r = "";// 天
if (StrKit.isBlank(xqms)) {
// 判断传入的参数是否为空
return "000000";
} else {
if (xqms.contains("年") && xqms.contains("月") && xqms.contains("天")) {
// 传入的字符串同时包含了"年月天"
indexof1 = xqms.indexOf("年");
n = xqms.substring(0, indexof1);
n = zfc(n);
indexof1 = xqms.indexOf("月");
indexof2 = xqms.indexOf("年") + 1;
y = xqms.substring(indexof2, indexof1);
y = zfc(y);
indexof1 = xqms.indexOf("天");
indexof2 = xqms.indexOf("月") + 1;
r = xqms.substring(indexof2, indexof1);
r = zfc(r);
return n + y + r;
} else if (!xqms.contains("年") && xqms.contains("月")
&& xqms.contains("天")) {
// 传入的字符串包含了"月天",但不包含"年"
indexof2 = xqms.indexOf("月");
y = xqms.substring(0, indexof2);
y = zfc(y);
indexof1 = xqms.indexOf("天");
indexof2 = indexof2 + 1;
r = xqms.substring(indexof2, indexof1);
r = zfc(r);
return "00" + y + r;
} else if (xqms.contains("年") && xqms.contains("月")
&& !xqms.contains("天")) {
// 传入的字符串包含了"年月",但不包含"天"
indexof1 = xqms.indexOf("年");
n = xqms.substring(0, indexof1);
n = zfc(n);
indexof1 = xqms.indexOf("月");
indexof2 = xqms.indexOf("年") + 1;
y = xqms.substring(indexof2, indexof1);
y = zfc(y);
return n + y + "00";
} else if (xqms.contains("年") && !xqms.contains("月")
&& xqms.contains("天")) {
// 传入的字符串包含了"年日",但不包含"月"
indexof1 = xqms.indexOf("年");
n = xqms.substring(0, indexof1);
n = zfc(n);
indexof1 = xqms.indexOf("天");
indexof2 = xqms.indexOf("年") + 1;
r = xqms.substring(indexof2, indexof1);
r = zfc(r);
return n + "00" + r;
} else if (!xqms.contains("年") && !xqms.contains("月")
&& xqms.contains("天")) {
// 传入的字符串只包含"天"
indexof1 = xqms.indexOf("天");
r = xqms.substring(0, indexof1);
r = zfc(r);
return "0000" + r;
} else {
return "000000";
}
}

}

/**
 * 用于判断传入参数的位数,没有2位,用"0"来补齐2位。
 * 
 * @param xx
 * @return
 * @author Penstaro
 */
public String zfc(String xx) {
    if (xx.length() == 2) {
        return xx;
    } else if (xx.length() == 1) {
        String a = "0" + xx;
        return a;
    } else if (xx.length() == 0) {
        return "00";
    } else {
        return "00";
    }
}

}

你应该告诉大家需要使用哪种语言

 import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTransfer {
    public static String dataTranfer(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        return sdf.format(date);
    }
    public static void main(String[] args) {
        System.out.println(dataTranfer(new Date()));
    }
}

参考SimpleDateFormat的使用

不好意思呀,我刚才没读懂问题,我又尝试了正则表达式的方法,这样可以满足要求吗


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataTransfer {
    public static String dataTransfer(String data){
        String year = "00";
        String month = "00";
        String day = "00";
        Pattern p = Pattern.compile("\\d+");
        Matcher m = p.matcher(data);
        while(m.find()){
            char c = data.charAt(m.end());
            if('年'==c){
                year = m.group();
                if(m.group().length()==1)
                    year = "0" + year;
            }
            if('月'==c){
                month = m.group();
                if(m.group().length()==1)
                    month = "0" + month;
            }
            if('日'==c){
                day = m.group();
                if(m.group().length()==1)
                    day = "0" + day;
            }
        }
        return year+month+day;
    }
    public static void main(String[] args) {
        System.out.println(dataTransfer("1月20日"));
    }
}

我写了一个,并测试了你的数据,我又加了几个测试数据

    public static void main(String[] args) {
        // 12年转换后120000;12年1天 转换后是 120001;12年1月12天转换后是120112;
        // 12年3月 转换后是120300;3天转换后就是 000003
        System.out.println(format("12年"));
        System.out.println(format("12年1天 "));
        System.out.println(format("12年1月12天"));
        System.out.println(format("12年3月"));
        System.out.println(format("3天"));

        System.out.println(format("12年11月12天"));
        System.out.println(format("11月12天"));
        System.out.println(format("1月12天"));
        System.out.println(format("1月2天"));
    }

    private static char[] FIELDS = {'年', '月', '天'};

    public static String format(String date) {
        char[] s = {'0', '0', '0', '0', '0', '0'};
        int index0 = 0, size = 0;
        int index;
        for (char ch : FIELDS) {
            index = date.indexOf(ch);
            if (index != -1) {
                String sub = date.substring(index0, index);
                if (sub.length() == 1) {
                    s[size + 1] = sub.charAt(0);
                } else {
                    s[size] = sub.charAt(0);
                    s[size + 1] = sub.charAt(1);
                }
                index0 = index+1;
            }
            size += 2;
        }
        return new String(s);
    }

直接indexOf计算汉字的索引。subString截取字符串。需要什么格式,拼接字符串就行了。

public class Test {

public static void main(String[] args) {
    System.out.println(getxq("12年"));
    System.out.println(getxq("12年1天"));
    System.out.println(getxq("12年1月12天"));
    System.out.println(getxq("12年3月"));
    System.out.println(getxq("3天"));

}

public static String getxq(String time){
    if(time==null){
        return null;
    }
    String n=null;
    String y=null;
    String r=null;
    //是否包含年
    boolean hasN=time.indexOf("年")!=-1;
    //是否包含月
    boolean hasY=time.indexOf("月")!=-1;
    //是否包含日
    boolean hasR=time.indexOf("天")!=-1;
    if(hasN){
        n=time.substring(0,time.indexOf("年"));
    }else{
        n="00";
    }
    if(hasY){
        if(hasN){
            y=time.substring(time.indexOf("年")+1, time.indexOf("月"));
        }else{
            y=time.substring(0,time.indexOf("月"));
        }

    }else{
        y="00";
    }
    if(hasR){
        if(hasN){
            if(hasY){
                r=time.substring(time.indexOf("月")+1, time.indexOf("天"));
            }else{
                r=time.substring(time.indexOf("年")+1, time.indexOf("天"));
            }
        }else{
            if(hasY){
                r=time.substring(time.indexOf("月")+1, time.indexOf("天"));
            }else{
                r=time.substring(0, time.indexOf("天"));
            }
        }
    }else{
        r="00";
    }
    return addZero(n)+addZero(y)+addZero(r);
}
/**
 * 补零
 * @param value
 * @return
 */
public static String addZero(String value){
    if(value==null){
        return "00";
    }
    if(value.length()<2){
        return "0"+value;
    }else{
        return value;
    }
}

}

        //设置当前日期2016-07-22,自己可以设定
        int year = 2016;
        int month = 7;
        int day = 22;

        //实例化一个日历对象,并设定时间
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR,year);
        cal.set(Calendar.MONTH,month-1);
        cal.set(Calendar.DATE,day);

        //把日历时间转换成Date型
        long b = cal.getTimeInMillis();
        Date date = new Date(b);

        //格式化时间yyMMdd
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        String s = sdf.format(date);
        System.out.println(s);