输入年份得到当年春节的公历

这个Java程序实现了一个工具类`Calendar`,能够将公历年份转换为对应的春节农历日期,帮助用户计算不同年份春节的具体日期。程序包含了农历和公历之间的转换方法,如`sCalendarSolarToLundar`和`sCalendarLundarToSolar`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.fangdi.workflow.definition.webService;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test1 {

 public static void main(String[] args) {
  // 调用农历日期转换阳历日期方法
  String str = new Calendar().sCalendarLundarToSolar(2015, 1, 1);
  System.out.println(nDaysAfterOneDateString(str,1));
 }

 public static  String   nDaysAfterOneDateString(String basicDate,int n)   { 
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date tmpDate = null; 
        try   { 
            tmpDate = df.parse(basicDate); 
        } 
        catch(Exception e){ 
        } 
        long nDay=(tmpDate.getTime()/(24*60*60*1000)+1-n)*(24*60*60*1000); 
        tmpDate.setTime(nDay); 
        return   df.format(tmpDate); 
    }
 
 
  static class Calendar {
  private final int[] iLunarMonthDaysTable = {
    0x4ae0,
    0xa570,
    0x5268,
    0xd260,
    0xd950,
    0x6aa8,
    0x56a0,
    0x9ad0,
    0x4ae8,
    0x4ae0, //  1910
    0xa4d8,
    0xa4d0,
    0xd250,
    0xd548,
    0xb550,
    0x56a0,
    0x96d0,
    0x95b0,
    0x49b8,
    0x49b0, //  1920
    0xa4b0,
    0xb258,
    0x6a50,
    0x6d40,
    0xada8,
    0x2b60,
    0x9570,
    0x4978,
    0x4970,
    0x64b0, //  1930
    0xd4a0,
    0xea50,
    0x6d48,
    0x5ad0,
    0x2b60,
    0x9370,
    0x92e0,
    0xc968,
    0xc950,
    0xd4a0, //  1940
    0xda50,
    0xb550,
    0x56a0,
    0xaad8,
    0x25d0,
    0x92d0,
    0xc958,
    0xa950,
    0xb4a8,
    0x6ca0, //  1950
    0xb550,
    0x55a8,
    0x4da0,
    0xa5b0,
    0x52b8,
    0x52b0,
    0xa950,
    0xe950,
    0x6aa0,
    0xad50, //  1960
    0xab50,
    0x4b60,
    0xa570,
    0xa570,
    0x5260,
    0xe930,
    0xd950,
    0x5aa8,
    0x56a0,
    0x96d0, //  1970
    0x4ae8, 0x4ad0,
    0xa4d0,
    0xd268,
    0xd250,
    0xd528,
    0xb540,
    0xb6a0,
    0x96d0,
    0x95b0, //  1980
    0x49b0, 0xa4b8, 0xa4b0,
    0xb258,
    0x6a50,
    0x6d40,
    0xada0,
    0xab60,
    0x9370,
    0x4978, //  1990
    0x4970, 0x64b0, 0x6a50, 0xea50,
    0x6b28,
    0x5ac0,
    0xab60,
    0x9368,
    0x92e0,
    0xc960, //  2000
    0xd4a8, 0xd4a0, 0xda50, 0x5aa8, 0x56a0,
    0xaad8,
    0x25d0,
    0x92d0,
    0xc958,
    0xa950, //  2010
    0xb4a0, 0xb550, 0xb550, 0x55a8, 0x4ba0, 0xa5b0,
    0x52b8,
    0x52b0,
    0xa930,
    0x74a8, //  2020
    0x6aa0, 0xad50, 0x4da8, 0x4b60, 0x9570, 0xa4e0, 0xd260,
    0xe930,
    0xd530,
    0x5aa0, //  2030
    0x6b50, 0x96d0, 0x4ae8, 0x4ad0, 0xa4d0, 0xd258, 0xd250, 0xd520,
    0xdaa0,
 &nb

### C语言 实现根据输入年份和月份计算并输出该月的天数 为了实现这一功能,可以按照以下逻辑构建程序: 定义一个函数用于判断给定年份是否为闰年。对于公历纪元中的年份来说,如果能被4整除但不能被100整除,则是普通闰年;若能被400整除则是世纪闰年[^1]。 创建数组存储每个月固定的最大日数(不考虑二月),并通过条件语句处理二月的情况。当遇到2月时,依据之前编写的闰年检测函数来决定返回28还是29天。 最后,在`main()`函数里接收用户输入年份与月份参数,并调用上述方法获取对应的总天数后打印出来。 下面是完整的C语言源码示例: ```c #include <stdio.h> // 判断是否为闰年的辅助函数 int isLeapYear(int year){ return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } // 获取指定月份最大天数的函数 int getMaxDayOfMonth(int month, int year){ switch(month){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: return isLeapYear(year)? 29 : 28; default: printf("Invalid Month\n"); return -1; } } int main(){ int year, month; // 提示用户输入数据 printf("请输入年份:"); scanf("%d", &year); printf("请输入月份(1-12): "); scanf("%d", &month); // 调用getMaxDayOfMonth()得到结果并输出 int days = getMaxDayOfMonth(month, year); if(days >= 0) printf("%d年%d月共有:%d天.\n", year, month, days); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值