用java编写一个2013全年日历
3个回答
展开全部
下面是一个带界面的java日历。可以指定任意年月。默认为2013年1月
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import javax.swing.JOptionPane;
class CalendarBean
{
String day[];
int year=2011,month=0;
public void setYear(int year)
{ this.year=year;
}
public int getYear()
{ return year;
}
public void setMonth(int month)
{ this.month=month;
}
public int getMonth()
{ return month;
}
public String[] getCalendar()
{ String a[]=new String[42];
Calendar 日历=Calendar.getInstance();
日历.set(year,month-1,1);
int 星期几=日历.get(Calendar.DAY_OF_WEEK)-1;
int day=0;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{ day=31;
}
if(month==4||month==6||month==9||month==11)
{ day=30;
}
if(month==2)
{ if(((year%4==0)&&(year%100!=0))||(year%400==0))
{ day=29;
}
else
{ day=28;
}
}
for(int i=星期几,n=1;i<星期几+day;i++)
{
a[i]=String.valueOf(n) ;
n++;
}
return a;
}
}
class CalendarFrame extends Frame implements ActionListener
{
Label labelDay[]=new Label[42];
Button titleName[]=new Button[7];
String name[]={"日","一","二","三", "四","五","六"};
TextField text1,text2;Button nextMonth,previousMonth,Enter;Label lab1,lab2,lab3;
int year=2013,month=1;
CalendarBean calendar;
Label showMessage=new Label("",Label.CENTER);
public CalendarFrame()
{ Panel pCenter=new Panel();
pCenter.setLayout(new GridLayout(7,7));
for(int i=0;i<7;i++)
{ titleName[i]=new Button(name[i]);
pCenter.add(titleName[i]);
}
for(int i=0;i<42;i++)
{
labelDay[i]=new Label("",Label.CENTER);
pCenter.add(labelDay[i]);
}
calendar=new CalendarBean();
calendar.setYear(year);
calendar.setMonth(month);
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++)
{ labelDay[i].setText(day[i]);
}
lab1=new Label("请输入日期");
lab2=new Label("年份");
lab3=new Label("月份");
Enter=new Button("确定");
text1=new TextField(10);
text2=new TextField(5);
nextMonth=new Button("下月");
previousMonth=new Button("上月");
Enter.addActionListener(this);
nextMonth.addActionListener(this);
previousMonth.addActionListener(this);
Panel pNorth=new Panel(),
pSouth=new Panel();
pNorth.add( lab1);
pNorth.add(lab2);
pNorth.add( text1);
pNorth.add(lab3);
pNorth.add(text2);
pNorth.add(Enter);
pNorth.add(previousMonth);
pNorth.add(nextMonth);
pSouth.add(showMessage);
showMessage.setText("日历:"+calendar.getYear()+"年"+ calendar.getMonth()+"月" );
ScrollPane scrollPane=new ScrollPane();
scrollPane.add(pCenter);
add(scrollPane,BorderLayout.CENTER);
add(pNorth ,BorderLayout.NORTH);
add(pSouth ,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==nextMonth)
{ month=month+1;
if(month>12)
month=1;
calendar.setMonth(month);
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++)
{ labelDay[i].setText(day[i]);
}
}
else if(e.getSource()==previousMonth)
{ month=month-1;
if(month<1)
month=12;
calendar.setMonth(month);
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++)
{ labelDay[i].setText(day[i]);
}
}else {
String yea=text1.getText();
String mon=text2.getText();
try{
year=Integer.parseInt(yea);
month=Integer.parseInt(mon);
if(month>12||month<1||year<1){
JOptionPane.showMessageDialog(null, "请输入正确月份或月份");
return;
}
else{
calendar.setYear(year);
calendar.setMonth(month);
}
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++)
{
labelDay[i].setText(day[i]);
}
}catch(NumberFormatException ee){
JOptionPane.showMessageDialog(null, "请输入正确的年份及月份");
}
}
showMessage.setText("日历:"+calendar.getYear()+"年"+calendar.getMonth()+"月" );
}
}
public class CalendarMainClass
{ public static void main(String args[])
{ CalendarFrame frame=new CalendarFrame();
frame.setTitle("日历");
frame.setBounds(300,200,500,300);
frame.setVisible(true);
frame.validate();
frame.addWindowListener(new java.awt.event.WindowAdapter()
{ public void windowClosing(java.awt.event.WindowEvent e)
{ System.exit(0);
}
}
);
}
}
有问题就追问。满意请采纳。
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import javax.swing.JOptionPane;
class CalendarBean
{
String day[];
int year=2011,month=0;
public void setYear(int year)
{ this.year=year;
}
public int getYear()
{ return year;
}
public void setMonth(int month)
{ this.month=month;
}
public int getMonth()
{ return month;
}
public String[] getCalendar()
{ String a[]=new String[42];
Calendar 日历=Calendar.getInstance();
日历.set(year,month-1,1);
int 星期几=日历.get(Calendar.DAY_OF_WEEK)-1;
int day=0;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{ day=31;
}
if(month==4||month==6||month==9||month==11)
{ day=30;
}
if(month==2)
{ if(((year%4==0)&&(year%100!=0))||(year%400==0))
{ day=29;
}
else
{ day=28;
}
}
for(int i=星期几,n=1;i<星期几+day;i++)
{
a[i]=String.valueOf(n) ;
n++;
}
return a;
}
}
class CalendarFrame extends Frame implements ActionListener
{
Label labelDay[]=new Label[42];
Button titleName[]=new Button[7];
String name[]={"日","一","二","三", "四","五","六"};
TextField text1,text2;Button nextMonth,previousMonth,Enter;Label lab1,lab2,lab3;
int year=2013,month=1;
CalendarBean calendar;
Label showMessage=new Label("",Label.CENTER);
public CalendarFrame()
{ Panel pCenter=new Panel();
pCenter.setLayout(new GridLayout(7,7));
for(int i=0;i<7;i++)
{ titleName[i]=new Button(name[i]);
pCenter.add(titleName[i]);
}
for(int i=0;i<42;i++)
{
labelDay[i]=new Label("",Label.CENTER);
pCenter.add(labelDay[i]);
}
calendar=new CalendarBean();
calendar.setYear(year);
calendar.setMonth(month);
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++)
{ labelDay[i].setText(day[i]);
}
lab1=new Label("请输入日期");
lab2=new Label("年份");
lab3=new Label("月份");
Enter=new Button("确定");
text1=new TextField(10);
text2=new TextField(5);
nextMonth=new Button("下月");
previousMonth=new Button("上月");
Enter.addActionListener(this);
nextMonth.addActionListener(this);
previousMonth.addActionListener(this);
Panel pNorth=new Panel(),
pSouth=new Panel();
pNorth.add( lab1);
pNorth.add(lab2);
pNorth.add( text1);
pNorth.add(lab3);
pNorth.add(text2);
pNorth.add(Enter);
pNorth.add(previousMonth);
pNorth.add(nextMonth);
pSouth.add(showMessage);
showMessage.setText("日历:"+calendar.getYear()+"年"+ calendar.getMonth()+"月" );
ScrollPane scrollPane=new ScrollPane();
scrollPane.add(pCenter);
add(scrollPane,BorderLayout.CENTER);
add(pNorth ,BorderLayout.NORTH);
add(pSouth ,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==nextMonth)
{ month=month+1;
if(month>12)
month=1;
calendar.setMonth(month);
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++)
{ labelDay[i].setText(day[i]);
}
}
else if(e.getSource()==previousMonth)
{ month=month-1;
if(month<1)
month=12;
calendar.setMonth(month);
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++)
{ labelDay[i].setText(day[i]);
}
}else {
String yea=text1.getText();
String mon=text2.getText();
try{
year=Integer.parseInt(yea);
month=Integer.parseInt(mon);
if(month>12||month<1||year<1){
JOptionPane.showMessageDialog(null, "请输入正确月份或月份");
return;
}
else{
calendar.setYear(year);
calendar.setMonth(month);
}
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++)
{
labelDay[i].setText(day[i]);
}
}catch(NumberFormatException ee){
JOptionPane.showMessageDialog(null, "请输入正确的年份及月份");
}
}
showMessage.setText("日历:"+calendar.getYear()+"年"+calendar.getMonth()+"月" );
}
}
public class CalendarMainClass
{ public static void main(String args[])
{ CalendarFrame frame=new CalendarFrame();
frame.setTitle("日历");
frame.setBounds(300,200,500,300);
frame.setVisible(true);
frame.validate();
frame.addWindowListener(new java.awt.event.WindowAdapter()
{ public void windowClosing(java.awt.event.WindowEvent e)
{ System.exit(0);
}
}
);
}
}
有问题就追问。满意请采纳。
追问
谢谢你的答案,我先将你的采纳为最佳答案,但是如果我还有问题可以向你请教吧?
博思aippt
2024-07-20 广告
2024-07-20 广告
作为深圳市博思云创科技有限公司的工作人员,对于Word文档生成PPT的操作,我们有以下建议:1. 使用另存为功能:在Word中编辑完文档后,点击文件->另存为,选择PowerPoint演示文稿(*.pptx)格式,即可将文档内容转换为PPT...
点击进入详情页
本回答由博思aippt提供
展开全部
看看我这个,可以一个时间段
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class $ {
private static int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public static void main(String... _) {
long l1 = System.currentTimeMillis();
List data = rili(1000, 1, 10000, 12);
long l2 = System.currentTimeMillis();
for (int i = 0; i < data.size(); i++) {
System.out.println(data.get(i));
}
System.out.println((l2 - l1) + "MS");
}
private static List rili(int startYear, int startMonth, int endYear, int endMonth) {
if (startYear > endYear || (startYear == endYear && startMonth > endMonth)) {
return null;
}
List data = new ArrayList();
for (int ii = startYear; ii <= endYear; ii++) {
int startM = startMonth - 1;
int endM = endMonth - 1;
if (startYear < endYear) {
if (ii == endYear) {
endM = 11;
} else {
startM = 0;
}
}
for (int i = startM; i <= endM; i++) {
data.add("-----------------------" + ii + "年" + (i + 1) + "月-----------------------");
data.add("日\t一\t二\t三\t四\t五\t六");
int day = days(ii, i);
StringBuffer buf = new StringBuffer();
int idx = 1;
while (idx <= day) {
int len = 0;
if (idx == 1) {
Calendar c = Calendar.getInstance();
c.set(ii, i, idx);
int xingqi = c.get(Calendar.DAY_OF_WEEK);
len = getLen(xingqi);
buf.append(addBlank(len));
}
buf.append(idx++);
for (int k = len + 1; k < 7; k++) {
if (idx > day) {
break;
}
buf.append("\t" + (idx++));
}
buf.append("\n");
}
data.add(buf);
}
}
return data;
}
private static StringBuffer addBlank(int len) {
StringBuffer buf = new StringBuffer();
for (int index = 0; index < len; index++) {
buf.append("\t");
}
return buf;
}
private static int getLen(int xingqi) {
return xingqi == Calendar.SUNDAY ? 0 : xingqi - 1;
}
public static int days(int year, int month) {
if (month != 1) {
return DAYS[month];
}
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return 29;
}
return DAYS[month];
}
}
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class $ {
private static int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public static void main(String... _) {
long l1 = System.currentTimeMillis();
List data = rili(1000, 1, 10000, 12);
long l2 = System.currentTimeMillis();
for (int i = 0; i < data.size(); i++) {
System.out.println(data.get(i));
}
System.out.println((l2 - l1) + "MS");
}
private static List rili(int startYear, int startMonth, int endYear, int endMonth) {
if (startYear > endYear || (startYear == endYear && startMonth > endMonth)) {
return null;
}
List data = new ArrayList();
for (int ii = startYear; ii <= endYear; ii++) {
int startM = startMonth - 1;
int endM = endMonth - 1;
if (startYear < endYear) {
if (ii == endYear) {
endM = 11;
} else {
startM = 0;
}
}
for (int i = startM; i <= endM; i++) {
data.add("-----------------------" + ii + "年" + (i + 1) + "月-----------------------");
data.add("日\t一\t二\t三\t四\t五\t六");
int day = days(ii, i);
StringBuffer buf = new StringBuffer();
int idx = 1;
while (idx <= day) {
int len = 0;
if (idx == 1) {
Calendar c = Calendar.getInstance();
c.set(ii, i, idx);
int xingqi = c.get(Calendar.DAY_OF_WEEK);
len = getLen(xingqi);
buf.append(addBlank(len));
}
buf.append(idx++);
for (int k = len + 1; k < 7; k++) {
if (idx > day) {
break;
}
buf.append("\t" + (idx++));
}
buf.append("\n");
}
data.add(buf);
}
}
return data;
}
private static StringBuffer addBlank(int len) {
StringBuffer buf = new StringBuffer();
for (int index = 0; index < len; index++) {
buf.append("\t");
}
return buf;
}
private static int getLen(int xingqi) {
return xingqi == Calendar.SUNDAY ? 0 : xingqi - 1;
}
public static int days(int year, int month) {
if (month != 1) {
return DAYS[month];
}
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return 29;
}
return DAYS[month];
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如下.年可以指定.
---------------------------------------------------------------------------------
import java.util.Calendar;
import java.util.Scanner;
public class TestCalendar {
static public void main(String 参数[]) {
Calendar c = Calendar.getInstance();
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DAY_OF_MONTH, 1);
while (c.get(Calendar.YEAR) == year) {
int wday = c.get(Calendar.DAY_OF_WEEK);
int mday = c.get(Calendar.DAY_OF_MONTH);
if (mday == 1) {
System.out.println("\n日\t一\t二\t三\t四\t五\t六\t第"
+ (c.get(Calendar.MONTH) + 1) + "月");
System.out
.println("---------------------------------------------------");
for (int i = 0; i < wday - 1; i++)
System.out.print(" \t");
}
System.out.print(mday + "\t");
if (wday == 7)
System.out.println();
c.add(Calendar.DAY_OF_YEAR, 1);
}
}
}
---------------------------------------------------------------------------------
import java.util.Calendar;
import java.util.Scanner;
public class TestCalendar {
static public void main(String 参数[]) {
Calendar c = Calendar.getInstance();
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DAY_OF_MONTH, 1);
while (c.get(Calendar.YEAR) == year) {
int wday = c.get(Calendar.DAY_OF_WEEK);
int mday = c.get(Calendar.DAY_OF_MONTH);
if (mday == 1) {
System.out.println("\n日\t一\t二\t三\t四\t五\t六\t第"
+ (c.get(Calendar.MONTH) + 1) + "月");
System.out
.println("---------------------------------------------------");
for (int i = 0; i < wday - 1; i++)
System.out.print(" \t");
}
System.out.print(mday + "\t");
if (wday == 7)
System.out.println();
c.add(Calendar.DAY_OF_YEAR, 1);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询