java將日期修改修改為漢字

來源:互聯網
上載者:User

 

如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<sdate.length();i++)  {   int k = Integer.parseInt(Character.toString(sdate.charAt(i)));   switch (k)   {   case 0:    newsdate[i]="〇";     break;   case 1:    newsdate[i]="一";    break;   case 2:    newsdate[i]="二";    break;   case 3:    newsdate[i]="三";    break;   case 4:    newsdate[i]="四";    break;   case 5:    newsdate[i]="五";    break;   case 6:    newsdate[i]="六";    break;   case 7:    newsdate[i]="七";    break;   case 8:    newsdate[i]="八";    break;   case 9:    newsdate[i]="九";    break;   }  }  //加入年月日  List<String> s1 = new ArrayList<String>();  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();  } }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.