java将日期修改修改为汉字

 

如2012-10-10转换为二〇一二年十月十日

写的比较繁琐,有没有高手帮忙改进下

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TimeUtil
{
 public String chtime(Date date)
 {
  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  String sdate = sdf.format(date);
  String newsdate[] = new String[8];
  //全部转化为汉字
  for(int i=0;i s1 = new ArrayList();
  for(int i=0;i<8;i++)
  {
   if(i<4)
   {
    s1.add(newsdate[i]);
   }
   else if(i==4)
   {
    s1.add("年");
    s1.add(newsdate[i]);
   }     
   else if(i==5)
   {
    s1.add(newsdate[i]);
   }
   else if(i==6)
   {
    s1.add("月");
    s1.add(newsdate[i]);
   }
   else if(i==7)
   {
    s1.add(newsdate[i]);
    s1.add("日");
   }
   
  }
  
  String newstr="";
  for(String s:s1)
  {
   newstr+=s;
  }
  /*
   * 截取月份、日期
   */
  int i = newstr.indexOf("年");
  int j = newstr.indexOf("月");
  String month = newstr.substring(i+1, j);
  String day = newstr.substring(j+1, newstr.length()-1);
  /*
   * 处理月份
   */
  String str1 = month.substring(0,1);
  String str2 = month.substring(1);
  String newmonth="";
  if("〇".equals(str1))
  {
   newmonth = str2;
  }
  else if("一".equals(str1)&&"〇".equals(str2))
  {
   newmonth = "十";
  }
  else if("一".equals(str1)&&!"〇".equals(str2))
  {
   newmonth = "十"+str2;
  }
  
  /*
   * 处理日期
   */
  String st1 = day.substring(0,1);
  String st2 = day.substring(1);
  String newday = "";
  if("〇".equals(st1))
  {
   newday = st2;
  }
  else if("一".equals(st1)&&"〇".equals(st2))
  {
   newday = "十";
  }
  else if("一".equals(st1)&&!"〇".equals(st2))
  {
   newday = "十"+st2;
  }
  else if("二".equals(st1)&&"〇".equals(st2))
  {
   newday = st1+"十";
  }
  else if("二".equals(st1)&&!"〇".equals(st2))
  {
   newday = st1+"十"+st2;
  }
  else if("三".equals(st1)&&"〇".equals(st2))
  {
   newday = st1+"十";
  }
  else if("三".equals(st1)&&!"〇".equals(st2))
  {
   newday = st1+"十"+st2;
  }
  String newstring = newstr.substring(0, i)+"年"+newmonth+"月"+newday+"日";
  return newstring;
 }
 
 
 public static void main(String[] args)
 {
  TimeUtil t = new TimeUtil();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  try
  {
   Date d = sdf.parse("20121010");
   System.out.println(t.chtime(d));
  }
  catch (ParseException e)
  {
   e.printStackTrace();
  }
 }
}


 

你可能感兴趣的:(java,string,date,import,class)