當前位置:編程學習大全網 - 編程語言 - 在已知陽歷(20120204)的情況下,如何算出節氣(java編程實現)!

在已知陽歷(20120204)的情況下,如何算出節氣(java編程實現)!

給妳代碼,從網上萬年歷中抽出來的。

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class Test {

long[] sTermInfo = new long[] { 0, 21208, 42467, 63836, 85337, 107014,

128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989,

308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224,

483532, 504758 };

String[] solarTerm = new String[] { "小寒", "大寒", "立春", "雨水", "驚蟄", "春分",

"清明", "谷雨", "立夏", "小滿", "芒種", "夏至", "小暑", "大暑", "立秋", "處暑", "白露",

"秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };

public static void main(String[] args) throws Exception {

Test t = new Test();

String strDate = "20120204";

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

Calendar clendar = Calendar.getInstance();

clendar.setTime(sdf.parse(strDate));

int year = clendar.get(Calendar.YEAR);

int month = clendar.get(Calendar.MONTH);

int day = clendar.get(Calendar.DATE);

// 節氣

int tmp1 = t.sTerm(year, month * 2);

int tmp2 = t.sTerm(year, month * 2 + 1);

String solarTerms = null;

if (day == tmp1) {

solarTerms = t.solarTerm[month * 2];

} else if (day == tmp2) {

solarTerms = t.solarTerm[month * 2 + 1];

} else {

solarTerms = "";

}

System.out.println(strDate + "的節氣是:" + solarTerms);

System.out.println("所有節氣的日期是:");

for (month = 0; month < 12; month++) {

clendar.set(Calendar.MONDAY, month);

tmp1 = t.sTerm(year, month * 2);

tmp2 = t.sTerm(year, month * 2 + 1);

clendar.set(Calendar.DATE, tmp1);

System.out.println(t.solarTerm[month * 2] + ":"

+ sdf.format(clendar.getTime()));

clendar.set(Calendar.DATE, tmp2);

System.out.println(t.solarTerm[month * 2 + 1] + ":"

+ sdf.format(clendar.getTime()));

}

}

public int sTerm(int y, int n) throws Exception {

if (y == 2009 && n == 2) {

sTermInfo[n] = 43467;

}

Calendar calendar = Calendar.getInstance();

calendar.set(1900, 0, 6, 2, 5);

Date _1900 = calendar.getTime();

long millis = (long) ((31556925974.7 * (y - 1900) + sTermInfo[n] * 60000) + _1900

.getTime());

calendar.setTimeInMillis(millis);

return calendar.get(Calendar.DATE);

}

}

  • 上一篇:明星編程技巧
  • 下一篇:英語周報高壹外研綜合第37期的完形填空,語法填空和短文改錯的文章翻譯
  • copyright 2024編程學習大全網