java 急

java 急,第1张

万年历代码:

/

@(#)AidyCalenderjava

@author

@version 100 2008/7/19

/

import javaawt;

import javaawtevent;

import javalangStringBuffer;

import javaxswing;

import javautil;

import javaxswingTimer;

import javaxswingborder;

public class AidyCalender extends JFrame implements ActionListener,ItemListener{

Date date = new Date();

private GregorianCalendar gregorianCalendar = new GregorianCalendar();

//定义中英文字符数组存储星期信息,用于转换显示

private String[] stringWeekEn = new String[] { "SUN", "MON", "TUE", "WED",

"THU", "FRI", "SAT" };

private String[] stringWeekCn = new String[] { "星期日", "星期一", "星期二", "星期三",

"星期四", "星期五", "星期六" };

//定义存储月份的信息数组,用于转换显示方示

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 用于存储系统时间的变量

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 = BorderFactorycreateRaisedBevelBorder();

private Border border1 = BorderFactorycreateLineBorder(Colorcyan,3);

public AidyCalender(String title) {

super(title);

for (int y = 1900; y < 2101; y++) {

combo1addItem(" " + new Integer(y)toString()+"年");

}

for (int m = 0;m<12;m++){

combo2addItem(" "+stringMonthCn[m]);

}

buttonTodaysetText("今 天");

setLayout(new FlowLayout());

add(panel1);

add(panel2);

add(panel3);

panel1setLayout(new GridLayout(1,3,10,0));

panel1add(combo1);

combo1addItemListener(this);

panel1add(combo2);

combo2addItemListener(this);

panel1add(buttonToday);

buttonTodayaddActionListener(this);

labelTimesetFont(new Font("宋体",FontPLAIN,16));

labelTimesetForeground(ColorMAGENTA);

panel1add(labelTime);

Timer time = new Timer(1000,new TimerListener());

timeaddActionListener(new TimerListener());

//timesetRepeats(true);

timestart();

//labelTimeaddAncestorListener(new TimerListener());

panel2setLayout(new GridLayout(7,7,0,10));

panel2setBackground(Colorwhite);

for(int i=0;i<7;i++){

labelWeek[i] = new JLabel();

labelWeek[i]setHorizontalAlignment(0);

if(i==0||i==6){

labelWeek[i]setBackground(Colorblue);

labelWeek[i]setForeground(ColorRED);

labelWeek[i]setFont(new Font("黑体",FontBOLD,14));

}

else{

labelWeek[i]setForeground(ColorBLACK);

labelWeek[i]setFont(new Font("新宋体",FontPLAIN,14));

}

labelWeek[i]setText(stringWeekCn[i]);

panel2add(labelWeek[i]);

}

for(int i= 0;i<42;i++){

labelDay[i] = new JLabel();

labelDay[i]setHorizontalAlignment(0);

labelDay[i]setText("");

panel2add(labelDay[i]);

}

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

Systemexit(0);

}

});

setSize(300,300);

setBounds(250, 200, 400, 360);

setVisible(true);

setResizable(false);

setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

getSysDate();

setNowDate();

}

public void actionPerformed(ActionEvent ae){

if(aegetSource()==buttonToday){

setNowDate();

}

}

public void itemStateChanged(ItemEvent aa){

setChangeDate();

}

public int turnWeek(String week){

int i;

for(i=0;i<7;i++)

if(weekequalsIgnoreCase(stringWeekEn[i]))

break;

return i;

}

public int turnMonth(String month){

/

int turnMonth(String month)

@month 系统日期中的月,诸如Jan\Feb

@return int

返回一个整数值,用于寻找stringMonthCn[]数组中对应的中文月份

/

int i;

for(i=0;i<12;i++)

if(monthequalsIgnoreCase(stringMonthEn[i]))

break;

return i;

}

/

setNowDate()

设置当前系统日期

/

public void setNowDate(){

setSysDate(getNowYear(),getNowMonth());

getSysRunDate();

setDateNull();

combo1setSelectedIndex(getShowYear() - 1900);

combo2setSelectedIndex(getShowMonth());

setDays(getMonthDays(getNowYear(),getNowMonth()),getInitWeek(sysRunTime[0]),getNowDay());

//labelTimesetText(sysNowTime[3]);

//labelTimesetHorizontalAlignment(0);

}

/Integer getShowYear()

获取组合框中应该显示的年份

/

public void setSysDate(int year,int month){

gregorianCalendarset(year,month,1);

}

public void setDateNull(){

for(int i=0;i<42;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) {

/

返回所选年月的天数,因为数组中的数值从0开始,所以3\5\8\10分别代表4\6\9\11几个小月

而1代表2月,经过是否为闰年判断,选择返回28或29天

其余月份为大月,返回31天

/

switch (month) {

case 3:

case 5:

case 8:

case 10:

return 30;//小月返回30天

case 1:

if (gregorianCalendarisLeapYear(year)) {

//isLeapYear(year)确定当前纪元中的指定年份是否为闰年。

return 29;

} else {

return 28;

}//闰年的二月返回29天,平年返回28天

default:

return 31;

//大月返回31天

}

}

/

int getComboYear()

获取组合框中的年份

/

public void getSysDate(){

date = gregorianCalendargetTime();

sysNowTime = (datetoString())split(" ");

}

public void getSysRunDate(){

date = gregorianCalendargetTime();

sysRunTime = (datetoString())split(" ");

}

public int getComboYear(){

return combo1getSelectedIndex()+1900;

}

/

int getComboMonth()

获取月组合框中的整数值,

/

public int getComboMonth(){

return combo2getSelectedIndex();

}

public int getInitWeek(String initWeek){

/

getWeekNow(String initWeek)

@para nowWeek 系统日期中的星期

返回当月中的1号是从星期几开始

/

int nowWeek = 0 ;

for(int i = 0;i<7;i++){

if(initWeekequalsIgnoreCase(stringWeekEn[i])){

nowWeek = i;

break;

}

}

return nowWeek;

}

public int getNowYear(){

return IntegerparseInt(sysNowTime[5]);

}

public int getNowMonth(){

int nowMonth=0;

for(int i=0;i<12;i++){

if(sysNowTime[1]equalsIgnoreCase(stringMonthEn[i]));

nowMonth=i;

break;

}

return nowMonth;

}

public int getNowDay(){

return IntegerparseInt(sysNowTime[2]);

}

public Integer getShowYear(){

return IntegerparseInt(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 本月天数

@para initWeek 初始星期

@para day 今天日

设置月历

/

setDateNull();

for(int i=initWeek;i<initWeek+monthDays+1;i++){

if((i-initWeek+1)==day){

labelDay[i]setBorder(border1);

labelDay[i]setForeground(ColorBLUE);

labelDay[i]setFont(new Font("黑体",FontBOLD,20));

}else if((i%7==0)||(i%7==6))

labelDay[i]setForeground(ColorRED);

else{

labelDay[i]setForeground(ColorBLACK);

}

labelDay[i]setText(StringvalueOf(i-initWeek+1));

}

for(int i=initWeek+monthDays;i<42;i++)

labelDay[i]setText("");

}

class TimerListener implements ActionListener{

//AdapterDemo var=new AdapterDemo("万年历程序--Aidy");

public void actionPerformed(ActionEvent e) {

GregorianCalendar g = new GregorianCalendar();

String clock = new String((ggetTime()toString()split(" "))[3]);

labelTimesetText(clock);

}

}

public static void main(String args[])

{

try{

UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName());

}catch(Exception e){

throw new RuntimeException(e);

}

AidyCalender var=new AidyCalender("万年历程序--Aidy");

}

}

记事本代码这里已经贴不上了。你百度HI我,我给你。

import javatext;

import javautil;

public class Test36 {

public static void main(String[] args) {

try {

SimpleDateFormat farmatter = new SimpleDateFormat("yyyy-MM-dd");

Date d1, d2;

int year, month;

String temp;

Scanner scan = new Scanner(Systemin);

Systemoutprint("请输入一个年月值\n年份:");

year = scannextInt();

Systemoutprint("月份:");

month = scannextInt();

MyCalendarlistCalendar(year, month);

Systemoutprint("请输入一个日期(yyyy-MM-dd):");

temp = scannext();

d1 = farmatterparse(temp);

Systemoutprintln(d1toLocaleString() + " 是 " + MyCalendargetDayOfWeek(d1));

Systemoutprint("请输入两个日期(yyyy-MM-dd)\n");

Systemoutprint("日期1 : ");

temp = scannext();

d1 = farmatterparse(temp);

Systemoutprint("日期2 : ");

temp = scannext();

d2 = farmatterparse(temp);

int diff = MyCalendardateDiff(d1, d2);

Systemoutprintf("%s 和 %s 之间相隔 %d 天\n", d1toLocaleString(), d2toLocaleString(), diff);

}

catch (Exception e) {

eprintStackTrace();

}

}

}

class MyCalendar{

//1) 用户输入一个年份和月份,程序输出这个月的月历,每星期一行,星期天开始,星期六结束。

public static void listCalendar(int year, int month){

if(month<1 || month>12){

Systemoutprintln(month + " 是非法的月份值,它的有效范围是 1~12");

return;

}

Systemoutprintln(" " + year + " - " + month);

Systemoutprintln(" ---------------------------");

Systemoutprintf("%4s%4s%4s%4s%4s%4s%4s\n", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

try{

Calendar c = CalendargetInstance();

cset(year, month-1, 1);

int num = getDays(year, month); //本月天数

int i;

for(i=0; i<(cget(CalendarDAY_OF_WEEK) - 1) 4; i++){

Systemoutprint(" ");

}

for(i=0; i<num; i++){

cset(year, month-1, i+1);

Systemoutprintf("%4s", i+1);

if(cget(CalendarDAY_OF_WEEK) == CalendarSATURDAY){

Systemoutprintln();

}

}

Systemoutprintln();

}

catch(Exception e){

eprintStackTrace();

}

}

//2) 用户输入一个日期,程序显示这个日期当天是星期几。

public static String getDayOfWeek(Date d){

String[] weeks = new String[]{"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};

Calendar c = CalendargetInstance();

csetTime(d);

return weeks[cget(CalendarDAY_OF_WEEK)-1];

}

//3) 用户输入两个日期,程序计算这两个日期相差的天数。

public static int dateDiff(Date d1, Date d2) throws Exception {

int i;

int[] days = new int[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

Calendar c1, c2;

int y1, y2;

int diff0 = 0, diff1 = 0, diff2 = 0;

c1 = CalendargetInstance();

c1setTime(d1);

y1 = c1get(CalendarYEAR);

c2 = CalendargetInstance();

c2setTime(d2);

y2 = c2get(CalendarYEAR);

//计算d1日期到d1所在年份第一天相隔的天数

days[1] = isLeapYear(y1) 29 : 28;

for(i=1; i<=c1get(CalendarMONTH); i++){

diff1 += days[i-1];

}

//计算d2日期到d2所在年份第一天相隔的天数

days[1] = isLeapYear(y2) 29 : 28;

for(i=1; i<=c2get(CalendarMONTH); i++){

diff2 += days[i-1];

}

//计算d1、d2两个日期的年份相隔的天数

if(y1 < y2){

for(i=y1; i<y2; i++){

diff0 += isLeapYear(i) 366 : 365;

}

}

else if(y1 > y2){

for(i=y2; i<y1; i++){

diff0 += isLeapYear(i) 366 : 365;

}

diff0 = -1;

}

return diff0 + diff2 - diff1 + c2get(CalendarDATE) - c1get(CalendarDATE);

}

//判断一个年份是否是闰年

public static boolean isLeapYear(int year){

return ((year%400==0) || (year%4==0 && year%100!=0)) true : false;

}

//返回某月的天数

public static int getDays(int year, int month){

if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){

return 31;

}

else if(month == 4 || month == 6 || month == 9 || month == 11){

return 30;

}

else{

if(isLeapYear(year) == true){

return 29;

}

else{

return 28;

}

}

}

}

运行测试:

请输入一个年月值

年份:2011

月份:6

2011 - 6

---------------------------

Sun Mon Tue Wed Thu Fri Sat

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30

请输入一个日期(yyyy-MM-dd):2011-6-15

2011-6-15 0:00:00 是 星期三

请输入两个日期(yyyy-MM-dd)

日期1 : 2009-3-10

日期2 : 2011-6-15

2009-3-10 0:00:00 和 2011-6-15 0:00:00 之间相隔 827 天

以上就是关于java 急全部的内容,包括:java 急、JAVA设计实现日历计算程序、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9858379.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存