问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

求Java 日历的小程序的源代码

发布网友 发布时间:2022-04-29 04:23

我来回答

4个回答

热心网友 时间:2023-10-11 07:38

也不知道你具体需求是什么,以前改过一个日历程序,一共四个java类,放在同一个包里。经测试可以运行。
//Start.java
import java.awt.*;
import javax.swing.*;
class Start{
public static void main(String [] args){
DateFrame frame=new DateFrame();
frame.setLocationRelativeTo(frame);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

//DateInfo.java

import java.util.*;
public class DateInfo{
private int mYear, mMonth;
private int mDayOfMonth, mFristWeek;
public DateInfo(int year, int month) throws DateException{
mYear = year;
if (month < 0 || month > 12){
throw (new DateException());
}
mMonth = month;

mDayOfMonth = getDayOfMonth(mYear, mMonth);
mFristWeek = getFristWeek(mYear, mMonth);
}

private int getDayOfMonth(int year, int month){
int[][] ary = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
return (ary[isLeapYear(year)][month]);
}
private int isLeapYear(int year){
if (year % 4 == 0 && year % 100 != 0 ||year % 400 == 0){
return (1);
}
else{
return (0);
}
}

private int getFristWeek(int year, int month){
java.util.Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, 1);
return (cal.get(Calendar.DAY_OF_WEEK) - 1);
}

public String toString(){
String str;
str = "\t\t" + mYear + "年" + mMonth + "月\n";
str += "日\t一\t二\t三\t四\t五\t六\n";
int i;
for (i = 1; i <= mFristWeek; i++){
str += " \t";
}
for (int j = 1; j <= mDayOfMonth; j++, i++){
str +=j+"\t" ;
if (i % 7 == 0){
str += "\n";
}
}
return (str);
}
}

//DateFrame.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;
class DateFrame extends JFrame implements Runnable{
Calendar date=Calendar.getInstance();
String[] str={"1","2","3","4","5","6","7","8","9","10","11","12"};
JLabel lblYear=new JLabel("年 ");
JLabel lblMonth=new JLabel("月 ");
JLabel lblDate=new JLabel("现在的时间是:");
JLabel lblShowDate=new JLabel();
//javax.swing.JTextField trxt =new JTextField(10);
//trxt.setHorizontalAlignment(JTextField.RIGHT); //设置文本从右边输入
JComboBox cboMonth=new JComboBox(str);
JComboBox cboYear=new JComboBox();
JTextArea txaShow=new JTextArea();
JPanel pnlNorth=new JPanel();
JPanel pnlSOUTH=new JPanel();
JButton btnShow=new JButton("显示");
JButton btnClose=new JButton("关闭");
JScrollPane jsp=new JScrollPane(txaShow);
Container c=this.getContentPane();
public DateFrame(){
Thread thread=new Thread(this);
thread.start();
this.setTitle("玩玩日历拉!!!");
this.setSize(300,260);
for (int i = 1990; i<=2025; i++) {
cboYear.addItem(""+i);
}
cboYear.setSelectedItem(""+(date.get(Calendar.YEAR)));
cboMonth.setSelectedItem(""+(date.get(Calendar.MONTH)+1));
pnlNorth.add(cboYear);
txaShow.setTabSize(4); //设置tab键的距离
txaShow.setForeground(Color.GREEN);
pnlNorth.add(lblYear);
pnlNorth.add(cboMonth);
pnlNorth.add(lblMonth);
pnlNorth.add(lblDate);
pnlNorth.add(lblShowDate);
c.add(pnlNorth,BorderLayout.NORTH);
c.add(jsp);
pnlSOUTH.add(btnShow);
pnlSOUTH.add(btnClose);
c.add(pnlSOUTH,BorderLayout.SOUTH);
btnShow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int year=Integer.parseInt((String)cboYear.getSelectedItem());
int month=Integer.parseInt((String)cboMonth.getSelectedItem());
try {
DateInfo date=new DateInfo(year,month);
txaShow.setText(""+date);
}
catch (DateException ex) {
ex.printStackTrace();
}

}
});
btnClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
}
public void run(){
try {
while(true){
Thread.sleep(1000);
int hour=date.get(Calendar.HOUR);
int minute=date.get(Calendar.MINUTE);
int second=date.get(Calendar.SECOND);
String str=hour+":"+minute+":"+second;
lblShowDate.setText(str);

//this.repaint();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}

}

//DateException.java
public class DateException extends Exception{
public DateException(){
super("日期数据不合法.");
}
}

热心网友 时间:2023-10-11 07:38

好巧,今天正好网友问过这个问题,顺便发一下,嘿嘿

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalendarTest {
Calendar d;
int today,thisMonth,thisYear;
int month,year;

public CalendarTest(){
d = new GregorianCalendar(); //默认构造 用今日 初始化参数
today = d.get(Calendar.DAY_OF_MONTH); //当前日
thisMonth = month = d.get(Calendar.MONTH); //当前月
thisYear = year = d.get(Calendar.YEAR); //当前年
}

public void setCalendarMonth(int month){
this.month = month;
}

public void setCalendarYear(int year){
this.year = year;
}

public void printCalendar(){
d.set(Calendar.MONTH, month); //设置月
d.set(Calendar.YEAR, year); //设置年
d.set(Calendar.DAY_OF_MONTH, 1);//设置当前月的第一天

int weekday=d.get(Calendar.DAY_OF_WEEK);//DAY_OF_WEEK 是个从1到7的整数,不是从0开始的数字
int firstDayOfWeek=d.getFirstDayOfWeek();

int indent=0;

while(weekday!=firstDayOfWeek){
indent++;
d.add(Calendar.DAY_OF_MONTH, -1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}

//String [] weekdayNames=new DateFormatSymbols().getShortWeekdays();
String [] weekdayNames={"","日","一","二","三","四","五","六"};

do{
System.out.printf("%2s", weekdayNames[weekday]);
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}while(weekday != firstDayOfWeek);
System.out.println();

for(int i=0;i<indent;i++){
System.out.print(" ");
d.add(Calendar.DAY_OF_MONTH, 1);
}

d.set(Calendar.DAY_OF_MONTH, 1);
do{
int day=d.get(Calendar.DAY_OF_MONTH);
System.out.printf("%3d",day);

if (day == today && d.get(Calendar.MONTH) == thisMonth
&& d.get(Calendar.YEAR) == thisYear)//判断是不是今日
System.out.print("*");

d.add(Calendar.DAY_OF_MONTH, 1);

weekday=d.get(Calendar.DAY_OF_WEEK);
if(weekday==firstDayOfWeek)System.out.println();
}while(d.get(Calendar.MONTH)==month);
}

public static void main(String [] args){
CalendarTest ct = new CalendarTest();
//如果未指定年、月,那么打印出来的月历 就是当前月,*代表今天
//ct.setCalendarMonth(7); //指定月
//ct.setCalendarYear(2011); //指定年
ct.printCalendar();
}
}

热心网友 时间:2023-10-11 07:38

package mike.util.calendar;

import mike.util.Print;

/**
* This class is used for printing some year's calendar.
* You should have a foundation what day is the first day ?
* Like the 2000 year. the first day is saturday.
* @author mike
* @time 2011.7.27
*
*/
public class MyCalendar {

/**
* This method for getting the what day is the first day of year.
* The first day is sunday of 2000 year.
* @param y the year
* @return what day
*/
public static int getYearWeekDay(int y){
if(y < 1900 || y > 2100)
throw new IllegalArgumentException("The year is between 1900 and 2100");
int sum = 0;
if(y >= 2000){
for(int year = 2000; year < y; year++)
sum += getYearDays(year);
return (sum + 6) % 7;
}
else{
for(int year = y; year < 2000; year++)
sum += getYearDays(y);
return (-sum % 7 + 6) % 7;//sum = n*7 + 6-x
}

}

/**
* returns days of y.
*/
public static int getYearDays(int y) {
return isLeapYear(y)?366:365;
}

/**
* Judge whether in 2000 for a leap year.
*/
public static boolean isLeapYear(int y) {
/*if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
return true;
return false;*/
return (y % 4 == 0 && y % 100 != 0 || y % 400 == 0);
}
/**
* count some month' first day is what day.
*/
public static int getMonthWeekDay(int y, int m){
if( y < 1900 || y > 2100 || m < 1 || m >12)
throw new IllegalArgumentException("Illegal argument.");
int sum = 0;
for( int i = 1 ; i < m; i++){
sum += getMonthDays(y,i);
}
return (getYearWeekDay(y) + sum) % 7;
}

public static int getMonthDays(int y, int m) {
switch(m){
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(y)?29:28;
default:
return 0;
}
}

public static void printYear(int y){
for(int i = 1; i <= 12; i++)
printMonth(y,i);
}

private static void printMonth(int y, int m) {
Print.print(" ---------------- " + y + " - " + m + " --------------- ");
Print.print("sun" + "\t" + "mon" + "\t" + "tues" +" \t" + "wedsen" + "\t" +
"thurs" + "\t" + "fri" + "\t" + "satur");
int w = getMonthWeekDay(y,m);
if( w != 0)
for( int i = 0 ; i < w ; i++)
Print.printnb("\t");
for(int i = 1 ; i <= getMonthDays(y,m);i++){
Print.printnb(i + "\t");
w++;
w %= 7;
if( w == 0 && i <= getMonthDays(y,m))
Print.print();
}
Print.print();

}

public static void main(String [] args){
MyCalendar.printYear(2011);
}
}
自己写的。输出语句自己改为System.out.println();

热心网友 时间:2023-10-11 07:39

当在一段代码块定义一个变量时,Java就在栈中 为这个变量分配内存空间,当该变量退出该作用域后,Java会自动释放掉为该变量所分配的内存空间,该内存空间可以立即被另作他用。

Java内存分配中的堆

堆内存用来存放由new创建的对象和数组。 在堆中分配的内存,由Java虚拟机的自动垃圾回收器来管理。
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
表格列求和公式怎么设置 求和函数公式怎么输入 北京注册成立一个公司需要多少钱 北京公司都是什么 手机导航地图语音怎么下载 如何分别真金和仿金首饰 怎样区分真金和仿金首饰呢 小学生新年晚会主持人的串词!!(不要太多)急 大大后天就需要了!!!_百度... 周年晚会策划公司 奥格瑞玛传送门大厅在哪 奥格瑞玛传送门大厅怎么走 想用c语言写一个关于日历的小程序,但是不知道如何计算星期几的问题... 天然气灶灶的开关坏了,咋换? 用vb编写万年历小程序 空调正常制热突然停止,空调机里好像有流川的声音是咋回事 夏普硒鼓保质期怎么看 HP硒鼓有保质期么? 空调半夜异响没热风怎么回事? 打印机硒鼓放久了里面的粉会不会坏掉 打印机硒鼓保修期是多久 过期但没拆分的环保激光打印机硒鼓还能用吗 激光打印机的硒鼓(墨盒)有保质期吗? 激光打印机的硒鼓是否有保质期 用手机建档案填表怎么填不上呢? 打印机硒鼓没有保质期吗? 硒鼓有效期几年 别人发我手机的表格我怎么无法填写呢 老师给我发过来的,怎么在手机上填写啊,wps下载了还是不能填啊 学习消防安全知识的重要性? 为什么手机上填表有一栏选不中无法填? 在手机上填写表格,没有格子,写不上内容怎么办? 我正在写一个日历小程序,输入本月几天第一天星期几输出本月日历,问题... 微信分享名片没有看清楚推荐人就添加了,现在想知道推荐人是谁,怎么查微信分享名片没有看清楚推荐人就添_百度问一问 软件测试帮忙 糖溜地瓜怎么做? 糖溜地瓜怎么做。? 微信怎么查加过好友推荐人 急需修改系统日期的小程序或是批处理代码 谁可以给我提供一份Java小程序 例如 用Java编写的日历 留言薄 聊天小程序 等 一定要有源代码哦 糖溜地瓜的做法步骤图,糖溜地瓜怎么做好吃 java关于日历的小程序 荣耀8x换电池需要多少钱- 问一问 华为荣耀8x手机更换电池拆机教程,过程简单,你也可以试试哦 那我可以在维修店换华为荣耀8手机电池吗 荣耀8去实体店换一个电池要多少钱要多长时间,换电池手机数据还在吗? 淘宝企业店铺违规被关闭后,变更企业法人,能否继续开店 昨晚做梦在外婆家看见自己家老房子被火烧了…是什么意思 梦见火烧老房子只剩下土坯是啥意思 梦见老房子和土里被好多木材点燃火烧起来了好不好? 梦见以前住过的老房子被火烧?怎么回事呢? 梦见以前的老房子燃烧起来了~是吉是凶?