用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();
    }
}

转载于:https://www.cnblogs.com/civvy/archive/2007/09/11/889589.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值