你可以用一份用于C語(yǔ)言的數(shù)據(jù)得到,Java和C語(yǔ)言有相似處,數(shù)據(jù)也是可以通用的。
專(zhuān)注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)宜陽(yáng)免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
我這里有一個(gè)數(shù)據(jù)提取工具,可以提取范圍從公元1600年到公元6400年之間的四千八百年之間的任意年份區(qū)間的數(shù)據(jù),非常好用,算法也有源代碼。
//農(nóng)歷月份信息。一年用3個(gè)字節(jié)表示
//+-------------------------------------------------------------------------+
//|?第23位?|????????第22-17位???????|??第16-13位?|?????????第12-0位?????????|
//|--------+------------------------+------------+--------------------------|
//|??保留??|?農(nóng)歷正月初一的年內(nèi)序數(shù)?|????閏月????|?一個(gè)比特對(duì)應(yīng)一個(gè)月份大小?|
//+-------------------------------------------------------------------------+
//月份大小數(shù)據(jù)是月份小的在低位,月份大的在高位,即正月在最低位。
//以1900年為例,3個(gè)字節(jié)的數(shù)據(jù)展開(kāi)成二進(jìn)制位:
//??0???????011110????????1000?????????????????????1?0?1?1?0?1?1?0?1?0?0?1?0
//保留??1月31日(春節(jié))??閏八月???從左往右依次十二月,十一月……閏八月、八月、七月……正月的天數(shù)
//農(nóng)歷月份對(duì)應(yīng)的位為0,表示這個(gè)月為29天(小月),為1表示有30天(大月)。
0x58,0x0B,0xA5,0x42,0x0B,0x49,0x2C,0x5A,0x93,0x52,0x0A,0x95,0x3D,0x35,0x2D,?//1600-1604
0x60,0x05,0x56,0x4A,0x0A,0xB5,0x36,0xD5,0xAA,0x5C,0x05,0xD2,0x44,0x0D,0xA5,?//1605-1609
0x30,0x7D,0x4A,0x56,0x0D,0x4A,0x41,0x6A,0x95,0x62,0x0A,0x97,0x4E,0x05,0x56,?//1610-1614
0x39,0x0A,0xB5,0x5E,0x0A,0xD9,0x48,0x06,0xD2,0x32,0x8E,0xA5,0x58,0x0F,0x25,?//1615-1619
0x44,0x06,0x4A,0x2A,0x4C,0x97,0x50,0x0A,0xAB,0x3D,0x55,0x5A,0x62,0x05,0x6A,?//1620-1624
0x4A,0x0B,0x69,0x36,0xD7,0x52,0x5C,0x07,0x92,0x46,0x0B,0x25,0x2E,0x96,0x4B,?//1625-1629
……
你用用看吧,應(yīng)該能用的。
/*
題目:輸出任意年份任意月份的日歷表(公元后)
思路:
1.已知1年1月1日是星期日,1?%?7?=?1?對(duì)應(yīng)的是星期日,2?%?7?=?2?對(duì)應(yīng)的是星期一,以此類(lèi)推;
2.計(jì)算當(dāng)年以前所有天數(shù)+當(dāng)年當(dāng)月1號(hào)之前所有天數(shù);
a.年份分平年閏年,平年365天,閏年366天;
b.閏年的判斷方法year?%?400?==?0?||?(year?%?100?!=?0??year?%?4?==?0)若為真,則為閏年否則為平年;
c.定義平年/閏年數(shù)組,包含各月天數(shù);
d.遍歷數(shù)組求和,計(jì)算當(dāng)年當(dāng)月前總天數(shù);
e.當(dāng)年以前所有天數(shù)+當(dāng)年當(dāng)月前總天數(shù)+1即為1年1月1日到當(dāng)年當(dāng)月1日的總天數(shù);
3.總天數(shù)對(duì)7取模,根據(jù)結(jié)果判斷當(dāng)月1號(hào)是星期幾,輸出空白區(qū)域;
4.輸出當(dāng)月日歷表,逢星期六換行
*/
import?java.util.Scanner;
class?FindMonthList?{
public?static?void?main(String[]?args){
Scanner?sc?=?new?Scanner(System.in);
System.out.println("請(qǐng)輸入年份:");
int?year?=?sc.nextInt();????????????//年份
if?(year??1)?{????????????????????????//判斷非法輸入年份
System.out.println("輸入錯(cuò)誤!");
return;
}
System.out.println("請(qǐng)輸入月份:");
int?month?=?sc.nextInt();????????????//月份
if?(month??1?||?month??12)?{????????//判斷非法輸入月份
System.out.println("輸入錯(cuò)誤!");
return;
}
//輸出表頭
System.out.println("-------"?+?year?+?"?年?"?+?month?+?"?月?"?+?"-------");
System.out.println();
System.out.println("日??一??二??三??四??五??六");
//計(jì)算當(dāng)前年份以前所有天數(shù)beforeYearTotalDay;每4年一個(gè)閏年,閏年366天,平年365天
int?beforeYearTotalDay?=?((year?-?1)?/?4?*?366)?+?(year-1?-?((year?-?1)?/?4))?*?365;
int[]?arrLeapYear?=?{0,31,29,31,30,31,30,31,31,30,31,30,31};????//閏年各月天數(shù)????int數(shù)組
int[]?arrNormalYear?=?{0,31,28,31,30,31,30,31,31,30,31,30,31};????//平年各月天數(shù)????int數(shù)組
int?beforeMonthTotalDay?=?0;????????????????????????????????????//定義本年當(dāng)月之前月份的總天數(shù)
if?(year?%?400?==?0?||?(year?%?100?!=?0??year?%?4?==?0))?{????//判斷當(dāng)前年份是否是閏年
for?(int?i?=?0?;?i??month?;?i?++?)?{????//for循環(huán)計(jì)算當(dāng)月之前總天數(shù)
//計(jì)算當(dāng)前月份之前的所有天數(shù)
beforeMonthTotalDay?=?beforeMonthTotalDay?+?arrLeapYear[i];
}
//判斷當(dāng)月1日是星期幾
int?totalDay?=?beforeYearTotalDay?+?beforeMonthTotalDay?+?1;
int?week?=?totalDay?%?7;//已知1年1月1日是星期日,即模7得1對(duì)應(yīng)的是星期日
for?(int?i?=?0?;?i??(week?-?1?+?7)?%?7?;?i?++)?{????//如果寫(xiě)成i??(week-1)會(huì)出現(xiàn)i-1的情況
System.out.print("????");//輸出開(kāi)頭空白
}
for?(int?i?=?1?;i?=?arrLeapYear[month]?;i?++?)?{????//for循環(huán)輸出各月天數(shù)
System.out.print(i?+?"??");
if?(i??10?)?{????????//小于10的數(shù)補(bǔ)一個(gè)空格,以便打印整齊
System.out.print("?");
}
if?(i?%?7?==?((7-(week?-?1))?%?7?)?||?i?==?arrLeapYear[month])?{//每逢星期六/尾數(shù)換行
System.out.println();
}
}
}?else?{????????//不是閏年就是平年
for?(int?i?=?0?;?i??month?;?i?++?)?{????//for循環(huán)計(jì)算出當(dāng)月之前月份總天數(shù)
beforeMonthTotalDay?=?beforeMonthTotalDay?+?arrNormalYear[i];
}
//判斷當(dāng)月1日是星期幾
int?totalDay?=?beforeYearTotalDay?+?beforeMonthTotalDay?+?1;
int?week?=?totalDay?%?7;//已知1年1月1日是星期日,即模7得1對(duì)應(yīng)的是星期日
for?(int?i?=?0?;?i??(week?-?1?+?7)?%?7?;?i?++)?{????//如果寫(xiě)成i??(week-1)會(huì)出現(xiàn)i-1的情況
System.out.print("????");//輸出開(kāi)頭空白
}
for?(int?i?=?1?;i?=?arrNormalYear[month]?;i?++?)?{//for循環(huán)輸出各月天數(shù)
System.out.print(i?+?"??");
if?(i??10?)?{????????????//小于10的數(shù)補(bǔ)一個(gè)空格,以便打印整齊
System.out.print("?");
}
if?(i?%?7?==?((7-(week?-?1))?%?7?)?||?i?==?arrNormalYear[month])?{//每逢星期六/尾數(shù)換行
System.out.println();
}
}
}
}
}
顯示效果:
/**
* @(#)AidyCalender.java
*
*
* @author
* @version 1.00 2008/7/19
*/
import java.awt.*;
import java.awt.event.*;
import java.lang.StringBuffer;
import javax.swing.*;
import java.util.*;
import javax.swing.Timer;
import javax.swing.border.*;
public class AidyCalender extends JFrame implements ActionListener,ItemListener{
Date date = new Date();
private GregorianCalendar gregorianCalendar = new GregorianCalendar();
//定義中英文字符數(shù)組存儲(chǔ)星期信息,用于轉(zhuǎn)換顯示
private String[] stringWeekEn = new String[] { "SUN", "MON", "TUE", "WED",
"THU", "FRI", "SAT" };
private String[] stringWeekCn = new String[] { "星期日", "星期一", "星期二", "星期三",
"星期四", "星期五", "星期六" };
//定義存儲(chǔ)月份的信息數(shù)組,用于轉(zhuǎn)換顯示方示
private String[] stringMonthEn = new String[] { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };
private String[] stringMonthCn = {"一月","二月","三月","四月","五月","六月",
"七月","八月","九月","十月","十一月","十二月"};
private String[] sysNowTime = new String[6];//sysNowTime 用于存儲(chǔ)系統(tǒng)時(shí)間的變量
private String[] sysRunTime = new String[6];
private JLabel []labelWeek = new JLabel[7];
private JLabel []labelDay = new JLabel[42];
private JLabel labelTime = new JLabel();
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JPanel panel3 = new JPanel();
private JComboBox combo1 = new JComboBox();
private JComboBox combo2 = new JComboBox();
private JButton buttonToday = new JButton();
private Border border = BorderFactory.createRaisedBevelBorder();
private Border border1 = BorderFactory.createLineBorder(Color.cyan,3);
public AidyCalender(String title) {
super(title);
for (int y = 1900; y 2101; y++) {
combo1.addItem(" " + new Integer(y).toString()+"年");
}
for (int m = 0;m12;m++){
combo2.addItem(" "+stringMonthCn[m]);
}
buttonToday.setText("今 天");
setLayout(new FlowLayout());
add(panel1);
add(panel2);
add(panel3);
panel1.setLayout(new GridLayout(1,3,10,0));
panel1.add(combo1);
combo1.addItemListener(this);
panel1.add(combo2);
combo2.addItemListener(this);
panel1.add(buttonToday);
buttonToday.addActionListener(this);
labelTime.setFont(new Font("宋體",Font.PLAIN,16));
labelTime.setForeground(Color.MAGENTA);
panel1.add(labelTime);
Timer time = new Timer(1000,new TimerListener());
time.addActionListener(new TimerListener());
//time.setRepeats(true);
time.start();
//labelTime.addAncestorListener(new TimerListener());
panel2.setLayout(new GridLayout(7,7,0,10));
panel2.setBackground(Color.white);
for(int i=0;i7;i++){
labelWeek[i] = new JLabel();
labelWeek[i].setHorizontalAlignment(0);
if(i==0||i==6){
labelWeek[i].setBackground(Color.blue);
labelWeek[i].setForeground(Color.RED);
labelWeek[i].setFont(new Font("黑體",Font.BOLD,14));
}
else{
labelWeek[i].setForeground(Color.BLACK);
labelWeek[i].setFont(new Font("新宋體",Font.PLAIN,14));
}
labelWeek[i].setText(stringWeekCn[i]);
panel2.add(labelWeek[i]);
}
for(int i= 0;i42;i++){
labelDay[i] = new JLabel();
labelDay[i].setHorizontalAlignment(0);
labelDay[i].setText("");
panel2.add(labelDay[i]);
}
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setSize(300,300);
setBounds(250, 200, 400, 360);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getSysDate();
setNowDate();
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==buttonToday){
setNowDate();
}
}
public void itemStateChanged(ItemEvent aa){
setChangeDate();
}
public int turnWeek(String week){
int i;
for(i=0;i7;i++)
if(week.equalsIgnoreCase(stringWeekEn[i]))
break;
return i;
}
public int turnMonth(String month){
/**
*int turnMonth(String month)
*@month 系統(tǒng)日期中的月,諸如Jan\Feb
*@return int
*返回一個(gè)整數(shù)值,用于尋找stringMonthCn[]數(shù)組中對(duì)應(yīng)的中文月份
*/
int i;
for(i=0;i12;i++)
if(month.equalsIgnoreCase(stringMonthEn[i]))
break;
return i;
}
/**
*setNowDate()
*設(shè)置當(dāng)前系統(tǒng)日期
*/
public void setNowDate(){
setSysDate(getNowYear(),getNowMonth());
getSysRunDate();
setDateNull();
combo1.setSelectedIndex(getShowYear() - 1900);
combo2.setSelectedIndex(getShowMonth());
setDays(getMonthDays(getNowYear(),getNowMonth()),getInitWeek(sysRunTime[0]),getNowDay());
//labelTime.setText(sysNowTime[3]);
//labelTime.setHorizontalAlignment(0);
}
/**Integer getShowYear()
*獲取組合框中應(yīng)該顯示的年份
*/
public void setSysDate(int year,int month){
gregorianCalendar.set(year,month,1);
}
public void setDateNull(){
for(int i=0;i42;i++){
labelDay[i].setText("");
}
}
public void setChangeDate(){
setSysDate(getComboYear(),getComboMonth());
getSysRunDate();
setDateNull();
setDays(getMonthDays(getComboYear(),getComboMonth()),getInitWeek(sysRunTime[0]),-1);
}
public int getMonthDays(int year, int month) {
/**
*返回所選年月的天數(shù),因?yàn)閿?shù)組中的數(shù)值從0開(kāi)始,所以3\5\8\10分別代表4\6\9\11幾個(gè)小月.
*而1代表2月,經(jīng)過(guò)是否為閏年判斷,選擇返回28或29天.
*其余月份為大月,返回31天.
**/
switch (month) {
case 3:
case 5:
case 8:
case 10:
return 30;//小月返回30天
case 1:
if (gregorianCalendar.isLeapYear(year)) {
//isLeapYear(year)確定當(dāng)前紀(jì)元中的指定年份是否為閏年。
return 29;
} else {
return 28;
}//閏年的二月返回29天,平年返回28天
default:
return 31;
//大月返回31天
}
}
/**
*int getComboYear()
*獲取組合框中的年份
*/
public void getSysDate(){
date = gregorianCalendar.getTime();
sysNowTime = (date.toString()).split(" ");
}
public void getSysRunDate(){
date = gregorianCalendar.getTime();
sysRunTime = (date.toString()).split(" ");
}
public int getComboYear(){
return combo1.getSelectedIndex()+1900;
}
/**
*int getComboMonth()
*獲取月組合框中的整數(shù)值,
*/
public int getComboMonth(){
return combo2.getSelectedIndex();
}
public int getInitWeek(String initWeek){
/**
*getWeekNow(String initWeek)
*@para nowWeek 系統(tǒng)日期中的星期
*返回當(dāng)月中的1號(hào)是從星期幾開(kāi)始
*/
int nowWeek = 0 ;
for(int i = 0;i7;i++){
if(initWeek.equalsIgnoreCase(stringWeekEn[i])){
nowWeek = i;
break;
}
}
return nowWeek;
}
public int getNowYear(){
return Integer.parseInt(sysNowTime[5]);
}
public int getNowMonth(){
int nowMonth=0;
for(int i=0;i12;i++){
if(sysNowTime[1].equalsIgnoreCase(stringMonthEn[i]));
nowMonth=i;
break;
}
return nowMonth;
}
public int getNowDay(){
return Integer.parseInt(sysNowTime[2]);
}
public Integer getShowYear(){
return Integer.parseInt(sysNowTime[5]);
}
public Integer getShowMonth(){
/**
*Integer getShowMonth()
*獲取在組給框中顯示的中文格式月份:如七月\八月等
*/
return turnMonth(sysNowTime[1]);
}
public void setDays(int monthDays,int initWeek,int day){
/**
*void setDays(int monthDays,int initWeek,int day)
*@para monthDays 本月天數(shù)
*@para initWeek 初始星期
*@para day 今天日
*設(shè)置月歷
*/
setDateNull();
for(int i=initWeek;iinitWeek+monthDays+1;i++){
if((i-initWeek+1)==day){
labelDay[i].setBorder(border1);
labelDay[i].setForeground(Color.BLUE);
labelDay[i].setFont(new Font("黑體",Font.BOLD,20));
}else if((i%7==0)||(i%7==6))
labelDay[i].setForeground(Color.RED);
else{
labelDay[i].setForeground(Color.BLACK);
}
labelDay[i].setText(String.valueOf(i-initWeek+1));
}
for(int i=initWeek+monthDays;i42;i++)
labelDay[i].setText("");
}
class TimerListener implements ActionListener{
//AdapterDemo var=new AdapterDemo("萬(wàn)年歷程序--Aidy");
public void actionPerformed(ActionEvent e) {
GregorianCalendar g = new GregorianCalendar();
String clock = new String((g.getTime().toString().split(" "))[3]);
labelTime.setText(clock);
}
}
public static void main(String args[])
{
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
throw new RuntimeException(e);
}
AidyCalender var=new AidyCalender("萬(wàn)年歷程序--Aidy");
}
}
本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:
import java.text.SimpleDateFormat; import java.util.Calendar; public class TestDate { public static final String[] weeks = { "日", "一", "二", "三", "四", "五", "六" }; public static void main(String[] args) { Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR,2011);//2011年 c.set(Calendar.MONTH,0);//java中Calendar類(lèi),月從0開(kāi)始, 0代表一月 c.set(Calendar.DATE,1);//1號(hào) int day = c.get(Calendar.DAY_OF_WEEK);//獲致是本周的第幾天地, 1代表星期天...7代表星期六 System.out.println(new SimpleDateFormat( "yyyy-MM-dd ").format(c.getTime())); System.out.println("星期" + weeks[day-1]); } } 把以上測(cè)試代碼寫(xiě)作一個(gè)方法 方法的參數(shù)名為年月日, 即可。當(dāng)然Calendar 還有很多功能,比如一周的第幾天,一年的第幾個(gè)月……