一个分享个人学习、开发经验的Blog,http://www.joyphper.net

将util.Date转换为大写(农历)时间

posted @ 2012-09-11 12:32 | 阅读:3097 | 评论:0 | 分类: Java , 代码分享

将util.Date转换为大写(农历)时间

 

package base.datetime;

import java.util.Calendar;
import java.util.Date;

/***************************************************************************************************
 * <b>function:</b> 日期工具类:将util.Date日期转换成大写日期格式
 * @project web
 * @package base.datetime
 * @fileName DateUtils.java
 * @createDate 2010-5-27 上午10:24:47
 */
public class DateUtils {
	// 日期转化为大小写
	public static String dataToUpper(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		int year = ca.get(Calendar.YEAR);
		int month = ca.get(Calendar.MONTH) + 1;
		int day = ca.get(Calendar.DAY_OF_MONTH);
		return numToUpper(year) + "年" + monthToUppder(month) + "月" + dayToUppder(day) + "日";
	}

	/***
	 * <b>function:</b> 将数字转化为大写
	 * @createDate 2010-5-27 上午10:28:12
	 * @param num 数字
	 * @return 转换后的大写数字
	 */
	public static String numToUpper(int num) {
		// String u[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
		//String u[] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
		String u[] = {"○", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
		char[] str = String.valueOf(num).toCharArray();
		String rstr = "";
		for (int i = 0; i < str.length; i++) {
			rstr = rstr + u[Integer.parseInt(str[i] + "")];
		}
		return rstr;
	}

	/***
	 * <b>function:</b> 月转化为大写
	 * @createDate 2010-5-27 上午10:41:42
	 * @param month 月份
	 * @return 返回转换后大写月份
	 */
	public static String monthToUppder(int month) {
		if (month < 10) {
			return numToUpper(month);
		} else if (month == 10) {
			return "十";
		} else {
			return "十" + numToUpper(month - 10);
		}
	}

	/***
	 * <b>function:</b> 日转化为大写
	 * @createDate 2010-5-27 上午10:43:32
	 * @param day 日期
	 * @return 转换大写的日期格式
	 */
	public static String dayToUppder(int day) {
		if (day < 20) {
			return monthToUppder(day);
		} else {
			char[] str = String.valueOf(day).toCharArray();
			if (str[1] == '0') {
				return numToUpper(Integer.parseInt(str[0] + "")) + "十";
			} else {
				return numToUpper(Integer.parseInt(str[0] + "")) + "十" + numToUpper(Integer.parseInt(str[1] + ""));
			}
		}
	}
	
	public static void main(String[] args) {
		
		System.out.println(DateUtils.dataToUpper(new Date()));
	}
}

 

TAG: java , 农历 , 时间

共有0条评论 发表评论>>

点击换一张验证码