你既然有处理2006-3-2这种情况﹐那就把2006年3月2日轉成2006-3-2這樣就行了啊 方法很簡單 String date = "2006年3月2日"; date = date.replaceAll("\\D+","-"); date = date.replaceAll("\\D+$","");
int index_year = str.indexOf("年"); int index_month = str.indexOf("月"); int index_day = str.indexOf("日"); String str_year = str.substring(0, index_year); String str_month = str.substring(index_year+1, index_month); String str_day = str.substring(index_month+1, index_day);把str_year,str_month,str_day按照你的数字转换,再用把“年”,“月”,“日”拼凑字符串不就行了么
package com.zet.regx;import java.security.InvalidParameterException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern;public class Replacer { public final static int DIGIT_TO_CHINESE = 0; public final static char[] CCHARS = (new String("零一二三四五六七八九")) .toCharArray(); public final static String REGEX0 = "\\d{4}年\\d{1,2}月\\d{1,2}日"; public static String replaceDate(String s) { char[] cs = s.toCharArray(); replace(cs); return new String(cs); } public static void replace(char[] orig) { for (int i = 0; i < orig.length; i++) { if (isReplaceTarget(orig[i])) { orig[i] = charAdapter(orig[i], DIGIT_TO_CHINESE); } } } public static boolean isReplaceTarget(char c) { if (c >= '0' && c <= '9') return true; return false; } public static char charAdapter(char c, int type) { if (type == DIGIT_TO_CHINESE) { return CCHARS[((int) (c - '0'))]; } throw new InvalidParameterException(""); } public static String replaceDateString(String s) { if (s == null) throw new NullPointerException(); String[] remainder = s.split(REGEX0); Pattern p = Pattern.compile(REGEX0); Matcher m = p.matcher(s); List neededReplaceString = new ArrayList(); while (m.find()) { neededReplaceString.add(m.group()); } return replace(remainder, neededReplaceString, s); } public static String replace(String[] remainder, List neededReplaceString, String s) { if (neededReplaceString.size() == 0) return s; StringBuffer res = new StringBuffer(); if (remainder.length == 0) { for (int i = 0; i < neededReplaceString.size(); i++) { String temp = (String) neededReplaceString.get(i); res.append(replaceDate(temp)); } return res.toString(); } return combine(remainder, neededReplaceString, s, res); } public static String combine(String[] remainder, List neededReplaceString, String s, StringBuffer res) { int remainderFirstPos = s.indexOf(remainder[0]); int replaceFirstPos = s.indexOf((String) neededReplaceString.get(0)); if (remainderFirstPos < replaceFirstPos) { for (int i = 0; i < remainder.length; i++) { res.append(remainder[i]); if (i < neededReplaceString.size()) { String temp = (String) neededReplaceString.get(i); res.append(replaceDate(temp)); } } } else { for (int i = 0; i < neededReplaceString.size(); i++) { String temp = (String) neededReplaceString.get(i); res.append(replaceDate(temp)); if (i < remainder.length) { res.append(remainder[i]); } } } return res.toString(); } public static void main(String[] args) { String s = "希望将'2006年3月2日'这样的字符串转换为'二千零六年三月二日'在网上找了一个例子," + "可以处理2006-3-2这种情况,可对2006年3月2日就不行了帮忙一下,多谢"; System.out.println(s); s = replaceDateString(s); System.out.println(s); }}
如果网上找到了例子,那么就把你的格式转化为网上例子能接受的格式 DateFormat df1 = new SimpleDateFormat("yyyy年M月d日"); DateFormat df2 = new SimpleDateFormat("yyyy-M-d"); System.out.println(df2.format(df1.parse("2006年3月2日")));//输出2006-3-2楼主也可以修改一下网上的例子,应该不难的吧
多谢各位了,这几天一直没上来看...今天上来一看,这么多伙计帮忙,感动啊...多谢大家了! 这个问题已经搞定了。从数据库中取出的日期格式为:20060301010101,转换前8位,中间加上年月日。如下: package commons;public class DateTools {
private static String getChinese(int digital){ switch(digital){ case 0: return "零"; case 1: return "一"; case 2: return "二"; case 3: return "三"; case 4: return "四"; case 5: return "五"; case 6: return "六"; case 7: return "七"; case 8: return "八"; case 9: return "九"; } return "零"; }
方法很簡單
String date = "2006年3月2日";
date = date.replaceAll("\\D+","-");
date = date.replaceAll("\\D+$","");
int index_month = str.indexOf("月");
int index_day = str.indexOf("日");
String str_year = str.substring(0, index_year);
String str_month = str.substring(index_year+1, index_month);
String str_day = str.substring(index_month+1, index_day);把str_year,str_month,str_day按照你的数字转换,再用把“年”,“月”,“日”拼凑字符串不就行了么
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Replacer { public final static int DIGIT_TO_CHINESE = 0; public final static char[] CCHARS = (new String("零一二三四五六七八九"))
.toCharArray(); public final static String REGEX0 = "\\d{4}年\\d{1,2}月\\d{1,2}日"; public static String replaceDate(String s) {
char[] cs = s.toCharArray();
replace(cs);
return new String(cs);
} public static void replace(char[] orig) {
for (int i = 0; i < orig.length; i++) {
if (isReplaceTarget(orig[i])) {
orig[i] = charAdapter(orig[i], DIGIT_TO_CHINESE);
}
}
} public static boolean isReplaceTarget(char c) {
if (c >= '0' && c <= '9')
return true;
return false;
} public static char charAdapter(char c, int type) {
if (type == DIGIT_TO_CHINESE) {
return CCHARS[((int) (c - '0'))];
}
throw new InvalidParameterException("");
} public static String replaceDateString(String s) {
if (s == null)
throw new NullPointerException();
String[] remainder = s.split(REGEX0);
Pattern p = Pattern.compile(REGEX0);
Matcher m = p.matcher(s);
List neededReplaceString = new ArrayList();
while (m.find()) {
neededReplaceString.add(m.group());
}
return replace(remainder, neededReplaceString, s);
} public static String replace(String[] remainder, List neededReplaceString,
String s) {
if (neededReplaceString.size() == 0)
return s;
StringBuffer res = new StringBuffer();
if (remainder.length == 0) {
for (int i = 0; i < neededReplaceString.size(); i++) {
String temp = (String) neededReplaceString.get(i);
res.append(replaceDate(temp));
}
return res.toString();
}
return combine(remainder, neededReplaceString, s, res);
} public static String combine(String[] remainder, List neededReplaceString,
String s, StringBuffer res) {
int remainderFirstPos = s.indexOf(remainder[0]);
int replaceFirstPos = s.indexOf((String) neededReplaceString.get(0));
if (remainderFirstPos < replaceFirstPos) {
for (int i = 0; i < remainder.length; i++) {
res.append(remainder[i]);
if (i < neededReplaceString.size()) {
String temp = (String) neededReplaceString.get(i);
res.append(replaceDate(temp));
}
} } else {
for (int i = 0; i < neededReplaceString.size(); i++) {
String temp = (String) neededReplaceString.get(i);
res.append(replaceDate(temp));
if (i < remainder.length) {
res.append(remainder[i]);
}
}
}
return res.toString();
} public static void main(String[] args) {
String s = "希望将'2006年3月2日'这样的字符串转换为'二千零六年三月二日'在网上找了一个例子,"
+ "可以处理2006-3-2这种情况,可对2006年3月2日就不行了帮忙一下,多谢";
System.out.println(s);
s = replaceDateString(s);
System.out.println(s); }}
DateFormat df1 = new SimpleDateFormat("yyyy年M月d日");
DateFormat df2 = new SimpleDateFormat("yyyy-M-d");
System.out.println(df2.format(df1.parse("2006年3月2日")));//输出2006-3-2楼主也可以修改一下网上的例子,应该不难的吧
这个问题已经搞定了。从数据库中取出的日期格式为:20060301010101,转换前8位,中间加上年月日。如下:
package commons;public class DateTools {
public static String date2chinese(String date){
//日期格式为-> 20060101101520
StringBuffer sb=new StringBuffer();
sb.append( getChinese(Integer.parseInt( date.substring( 0,1))));
sb.append( getChinese(Integer.parseInt( date.substring( 1,2))));
sb.append( getChinese(Integer.parseInt( date.substring( 2,3))));
sb.append( getChinese(Integer.parseInt( date.substring( 3,4))));
sb.append( "年");
String month1=date.substring( 4,5);
if(! "0".equals( month1)){
sb.append("十");
}
String month2=date.substring( 5,6);
if(! "0".equals( month2)){
sb.append( getChinese(Integer.parseInt( month2)));
}
sb.append( "月");
String day1=date.substring( 6,7);
if("0".equals( day1)){
//nothing
}else if("1".equals( day1)){
sb.append("十");
}else{
sb.append( getChinese(Integer.parseInt(day1)));
sb.append("十");
}
String day2=date.substring( 7,8);
if(! "0".equals( day2)){
sb.append( getChinese(Integer.parseInt( day2)));
}
sb.append( "日");
return sb.toString() ;
}
private static String getChinese(int digital){
switch(digital){
case 0:
return "零";
case 1:
return "一";
case 2:
return "二";
case 3:
return "三";
case 4:
return "四";
case 5:
return "五";
case 6:
return "六";
case 7:
return "七";
case 8:
return "八";
case 9:
return "九";
}
return "零";
}
public static void main(String[] args){
//System.out.println(date2chinese("20060101"));
//System.out.println(date2chinese("20061010"));
//System.out.println(date2chinese("20061231"));
//System.out.println(date2chinese("20061123"));
//System.out.println(date2chinese("20061021"));
//System.out.println(date2chinese("20061030"));
//System.out.println(date2chinese("20000101"));
//System.out.println(date2chinese("21230901"));
}}很多情况没考虑,可是够我用了。