java 这么获取农历_用JAVA查询中国农历年

用来查询农历年的代码,可能比较粗糙,希望不要太打击偶,哈哈。

一,Console.java[用来获取控制台的输入]

/*

#(@)Console.java 20:40:25 2/2/2006

控制台消息输入/输出。

*/

import java.io.*;

import java.text.SimpleDateFormat;

import java.util.Calendar;

public final class Console {

public Console() {}

private static SimpleDateFormat style = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

/**

* 根据提示返回用户的输入

*

* @param s String 控制台输入提示

* @return String 返回用户的控制台输入

*/

public String read(String m) {

String s = null;

System.out.println(m);

try {

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

s = in.readLine();

} catch (IOException ex) {

printError(this.getClass().getName(), "read(String s)", ex.getMessage());

}

return s;

}

/**

* 根据提示返回用户的输入

*

* @param s String 控制台输入提示

* @return String 返回用户的控制台输入

*/

public int readInt(String s) {

int x;

try {

x = Integer.parseInt(this.read(s));

} catch (NumberFormatException ex) {

x = 0;

printError(this.getClass().getName(), "readInt(String s)", ex.getMessage());

}

return x;

}

/**

* 根据提示返回用户的输入

*

* @param s String 控制台输入提示

* @return String 返回用户的控制台输入

*/

public Double readDouble(String s) {

double x;

try {

x = Double.parseDouble(this.read(s));

} catch (NumberFormatException ex) {

x = 0;

printError(this.getClass().getName(), "readInt(String s)", ex.getMessage());

}

return x;

}

/**

* 打印错误消息

*

* @param c String 类名

* @param m String 方法名

* @param e String 错误信息

*/

public void printError(String c, String m, String e) {

System.out.println(style.format(Calendar.getInstance().getTime()) + ":调用"+ c + "." + m + "方法时发生错误:" + e + "。");

}

/**

* main(String[] args)方法

*/

public static void main(String[] args) {

Console c = new Console();

System.out.println(c.readDouble("请输入Double类型的数字……"));

}

}

二,Chinese.java[计算农历年的代码]

/*

#(@)Chinese.java 21:56:25 2/2/2006

中国农历。干支纪年始于公元四年,即第一个甲子年,故甲、子位于数组中的第四位。

*/

import java.text.SimpleDateFormat;

import java.util.Calendar;

public final class Chinese {

public Chinese() {}

private Console c = new Console();

/*天干数组*/

private String[] s = {"庚", "辛", "壬", "癸", "甲", "乙", "丙", "丁", "戊", "己"};

/*地支数组*/

private String[] t = {"申", "酉", "戌", "亥", "子", "丑", "寅", "卯", "辰", "巳", "午", "未"};

/*十二生肖*/

private String[] g = {"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};

private String again = null;

/**

* 根据用户的输入确定继续查询或退出运行

*

*/

public void chinese() {

if (this.chineseera()) {

String s = c.read("提示:按回车键退出,重新查询请输入新的年数后按回车键。");

if (s != null && !s.trim().equals("")) {

again = s;

this.chinese();

} else {

System.exit(0);

}

}

}

/**

* 根据用户的输入查询农历年和生肖属相,如果没有用户输入,则按当前年度开始查询

*

*/

private boolean chineseera() {

boolean b = false;

String year = null;

if (again == null) {

year = c.read("请输入准备查询农历年的年数,完成后按回车键。");

if (year == null || year.equals("")) {

SimpleDateFormat style = new SimpleDateFormat("yyyy");

year = style.format(Calendar.getInstance().getTime());

}

} else {

year = again;

}

int i = this.last(year);

if (i != -1) {

int y = this.cathay(year);

System.out.println(year + "年是农历"+ s[i] + t[y] +"年,十二生肖中属"+ g[y] +"。");

b = true;

}

return b;

}

/**

* 取得字符串的最后一位字符并转换成数字型变量

* 取得天干时的算法,主要是用最后一位数字对应天干数组中的数位字符

*

* @param s String 用户输入的年数

* @return int 返回转换后的最后一位数字,如果发生错误,则返回-1

*/

private int last(String s) {

int y = 0;

if (s != null) {

try {

s = s.substring(s.length()-1, s.length());

y = Integer.parseInt(s);

} catch (NumberFormatException ex) {

y = -1;

c.printError(this.getClass().getName(), "last(String s)", ex.getMessage());

}

}

return y;

}

/**

* 取得字符串除以十二以后的余数

* 取得地支时的算法,主要是用除以十二以后的余数对应地支数组中的数位字符

* 十二生肖的算法也与此相同

*

* @param s String 用户输入的年数

* @return int 返回除以十二以后的余数

*/

private int cathay(String s) {

int y = 0;

try {

y = Integer.parseInt(s);

y %= 12;

} catch (NumberFormatException ex) {

c.printError(this.getClass().getName(), "cathay(String s)", ex.getMessage());

}

return y;

}

/**

* main(String[] args)方法

*/

public static void main(String[] args) {

Chinese cathay = new Chinese();

cathay.chinese();

}

}

LunarCalendar返回农历(阴历)日期的JAR包 根据指定日期计算对应农历日期(这个计算方法是网上找的,最初的作者是谁已经无法考证了,感谢网络资源吧!),本人封装成好用的JAR包后发不出来,供大家免费下载! toString()方法输出阴历日期(例如:癸巳七月廿) getFullInfo()方法输出包括生肖在内的阴历日期(例如:癸巳七月廿,生肖:蛇) 构建方法包括以下四种: public LunarCalendar(String year, String month, String date) public LunarCalendar(JComboBox jcYear, JComboBox jcMonth, JComboBox jcDate) public LunarCalendar(int year, int month, int date) public LunarCalendar(Calendar cal)) 使用前两种构建方法时,若文本内容不为数字,getErrorMessage会返回错误信息 方法摘要 java.lang.String getErrorMessage() 返回String类型的错误信息 java.lang.String getFullInfo() 返回String类型的详细阴历信息(例如:癸巳七月廿,生肖:蛇) java.lang.String getLunarAnimal() 返回String类型的生肖(例如:蛇) java.lang.String getLunarDate() 返回String类型的阴历日期(例如:廿) java.lang.String getLunarMonth() 返回String类型的阴历月份(例如:七) java.lang.String getLunarYear() 返回String类型的阴历份(天干地支,例如:癸巳) java.lang.String toString() 返回String类型的阴历日期(例如:癸巳七月廿) JAR包名称:LunarCalendar version 1.0 8/26/2013 作者:Roy, Liu royliu90@live.cn
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值