面积单位的转化,把亩以及公顷转换成平方米
private final static Pattern PATTERN_PRICE = Pattern
.compile("(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))");
// 全部转换为平方米
public float formatArea(String value) {
Matcher matcher = PATTERN_PRICE.matcher(value);
if (matcher.find()) {
if (value.contains("亩") && !value.contains("平方米")){
return String.format("%.2f",Float.parseFloat(matcher.group()) * 666.67);
} else if (value.contains("公顷") && !value.contains("平方米")) {
return String.format("%.2f",Float.parseFloat(matcher.group()) * 10000);
} else if ((value.contains("亩") && value.contains("平方米"))||
(value.contains("公顷") && value.contains("平方米"))
) {
return formatMaxNumber(value);
} else {
return matcher.group();
}
} else {
grantarea.setUseValue("0.0");
}
return grantarea;
}
protected String formatMaxNumber(String value) {
List<Float> fl = new ArrayList<Float>();
if (value != null) {
Matcher valueMatcher = intPattern.matcher(value);
while (valueMatcher.find()) {
String strRjl = valueMatcher.group();
float rst = Float.valueOf(strRjl);
fl.add(rst);
}
if (fl.size() == 1) {
return fl.get(0).toString();
} else if (fl.size() == 2) {
if (fl.get(0) > fl.get(1)) {
return fl.get(0).toString();
} else {
return fl.get(1).toString();
}
}
}
return null;
}
将中文日期转换为日期
public static void main(String[] args) throws IOException {
testConvertZwrq();
}
private static void testConvertZwrq() {
String[] list = new String[] { "二○○九年四月三十日", "○九年四月三十日", "二○○九年十二月三十一日", "二零零九年十二月三十一日" };
for (String s : list) {
Date d = convertCnDate(s);
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(d));
}
}
public static Date convertCnDate(String cprq) {
int yearPos = cprq.indexOf("年");
int monthPos = cprq.indexOf("月");
String cnYear = cprq.substring(0, yearPos);
String cnMonth = cprq.substring(yearPos + 1, monthPos);
String cnDay = cprq.substring(monthPos + 1, cprq.length() - 1);
String year = ConvertCnYear(cnYear);
String month = ConvertCnDateNumber(cnMonth);
String day = ConvertCnDateNumber(cnDay);
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, Integer.parseInt(year));
c.set(Calendar.MONTH, Integer.parseInt(month)-1);
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
return c.getTime();
}
private static String ConvertCnYear(String cnYear) {
if(cnYear.length() == 2)
return "20" + ConvertCnNumberChar(cnYear);
else
return ConvertCnNumberChar(cnYear);
}
private static String ConvertCnDateNumber(String cnNumber) {
if (cnNumber.length() == 1) {
if(cnNumber.equals("十"))
return "10";
else
return ConvertCnNumberChar(cnNumber);
} else if (cnNumber.length() == 2) {
if (cnNumber.startsWith("十")) {
return "1" + ConvertCnNumberChar(cnNumber.substring(1, 2));
} else if (cnNumber.endsWith("十")) {
return ConvertCnNumberChar(cnNumber.substring(0, 1)) + "0";
} else {
return ConvertCnNumberChar(cnNumber);
}
} else if (cnNumber.length() == 3) {
return ConvertCnNumberChar(cnNumber.substring(0, 1) + cnNumber.substring(2, 3));
}
return null;
}
private static String ConvertCnNumberChar(String cnNumberStr) {
String ALL_CN_NUMBER = "○零一二三四五六七八九";
String ALL_NUMBER = "00123456789";
StringBuffer buf = new StringBuffer();
for (int i = 0; i < cnNumberStr.length(); i++) {
char c = cnNumberStr.charAt(i);
int index = ALL_CN_NUMBER.indexOf(c);
if (index != -1) {
buf.append(ALL_NUMBER.charAt(index));
} else {
buf.append(cnNumberStr.charAt(i));
}
}
return buf.toString();
} |