大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.io.*;
创新互联建站专注于牧野网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供牧野营销型网站建设,牧野网站制作、牧野网页设计、牧野网站官网定制、微信小程序定制开发服务,打造牧野网络公司原创品牌,更为您提供牧野网站排名全网营销落地服务。
class putout{
public void putout(int f,int x,int y){
int i;
int a[]= new int[40];
System.out.println(" 日 一 二 三 四 五 六 "+" "+f+"月");
for (i=0;ix;i++)
{System.out.print(" "); }
for(i=x;ix+y;i++)
a[i]=i-x+1;
for(i=x;ix+y;i++)
{
if ((i%7==0)(i0))
System.out.print("\n");
if (a[i]10)
System.out.print(" "+a[i]);
else System.out.print(" "+a[i]);
}
System.out.println("\n");
}
}
class st{
public static void main(String args[])throws IOException{
putout p=new putout();
int year,mouth,y=1,t,i;
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
System.out.print("请输入一个年份:");
String s=in.readLine();
year=Integer.parseInt(s);
if((year%4==0 year%100!=0)||(year%400==0))
mouth=1;
else
mouth=0;
y=year;
for(i=1;iyear;i++)
{if((i%4==0 i%100!=0)||(i%400==0))
y++;}
y=y%7;
for(i=1;i13;i++){
switch(i){
case 1: {p.putout(1,y,31);y=(y+31)%7;break;}
case 2: {p.putout(2,y,28+mouth);y=(y+28+mouth)%7;break;}
case 3: {p.putout(3,y,31);y=(y+31)%7;break;}
case 4: {p.putout(4,y,30);y=(y+30)%7;break;}
case 5: {p.putout(5,y,31);y=(y+31)%7;break;}
case 6: {p.putout(6,y,30);y=(y+30)%7;break;}
case 7: {p.putout(7,y,31);y=(y+31)%7;break;}
case 8: {p.putout(8,y,31);y=(y+31)%7;break;}
case 9: {p.putout(9,y,30);y=(y+30)%7;break;}
case 10: {p.putout(10,y,31);y=(y+31)%7;break;}
case 11: {p.putout(11,y,30);y=(y+30)%7;break;}
case 12: {p.putout(12,y,31);y=(y+31)%7;break;}
}
}
}
}
/*日历*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.regex.Pattern;
import javax.swing.*;
public class Demo28 extends JFrame {
int m = 1;
String[] monthchoose = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12" }; // 存放月份的字符数组
String[] columnNames = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; // 存放星期的字符数组
Calendar ca = Calendar.getInstance();
Container contentPane = getContentPane();
VectorString vector = new VectorString();
String[][] date = new String[6][7]; // 表格的显示数据的格式
TextField tf; // 文本框的值代表的是年份
JComboBox jb;
JTable table; // 把日期用table的方式显示出来
public void getDate(String year, String month, String week, int Max_Day) {
int n = 0, b = 0;
// 动态把传进来月份的天数存放到容器里
for (int j = 1; j = Max_Day; j++) {
vector.add(String.valueOf(j));
}
//每次往table里添加数据的时候,都预先把原table里 的 数据清空
for(int x = 0;xdate.length;x++){
for(int y = 0;ydate[x].length;y++){
date[x][y] = null;
}
}
// 根据传进来月份的第一天是星期几,来构建Table
for (int a = Integer.parseInt(week) - 1; a date[0].length; a++) {
date[0][a] = new String((String) vector.toArray()[n]);
n++;
}
for (int i = 1; i date.length; i++) {
for (int j = 0; j date[i].length; j++) {
if (n vector.size()) {
date[i][j] = new String((String) vector.toArray()[n]);
n++;
} else
break;
}
}
// 把容器里的数据全部清除,以备下次再存放新的数据
while (b vector.size()) {
vector.remove(b);
}
}
public void chooseDate(String day) {
JLabel label = new JLabel();
for (int y = 0; y date.length; y++) {
for (int z = 0; z date[y].length; z++) {
System.out.print(date[y][z] + " ");
System.out.println(day);
if (date[y][z] != null) {
if (date[y][z].equals(day)) {
table.setSelectionBackground(Color.yellow);
return;
}
}
}
}
}
public void paint() {
setTitle("日历");
setBounds(200, 200, 350, 178);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
int m = 0;
String year = String.valueOf(ca.get(Calendar.YEAR)); // 得到当前的系统时间的年份,并把这个数值存放到year这个变量里
String month = String.valueOf(ca.get(Calendar.MONTH) + 1); // 得到当前的系统时间的月份,并把这个数值存放到month这个变量里
String day = String.valueOf(ca.get(Calendar.DATE)); // 得到当前的系统时间的日期,并把这个数值存放到day这个变量里
ca.set(Calendar.DATE, 1); // 把Calendar 对象的DATA设置为1
String week = String.valueOf(ca.get(Calendar.DAY_OF_WEEK)); // 根据设置的Calendar对象,计算出这个月第一天是星期几
int Max_Day = ca.getActualMaximum(Calendar.DATE); // 得到当前系统时间月份有多少天
getDate(year, month, week, Max_Day);
// 从月份数组里取出与当前系统时间一样的月份值
for (int i = 0; i monthchoose.length; i++) {
if (monthchoose[i].equals(month)) {
m = i;
}
}
JToolBar toolBar = new JToolBar();
JButton b1 = new JButton("<");
b1.addMouseListener(new myMouseListener1());
JButton b2 = new JButton(">");
b2.addMouseListener(new myMouseListener2());
JLabel j1 = new JLabel("年");
JLabel j2 = new JLabel("月");
tf = new TextField(5);
tf.addKeyListener(new myKeyListener());
tf.setText(year);
jb = new JComboBox(monthchoose);
jb.setSelectedIndex(m);
jb.addActionListener(new myActionListener3());
table = new JTable(date, columnNames);
//table.addMouseListener(new tableMouseListener());
table.setPreferredScrollableViewportSize(new Dimension(350, 150));
JScrollPane jsp = new JScrollPane(table);
contentPane.add(jsp, BorderLayout.CENTER);
chooseDate(day);
toolBar.add(b1);
toolBar.add(tf);
toolBar.add(b2);
toolBar.add(j1);
toolBar.add(jb);
toolBar.add(j2);
toolBar.setLocation(0, 0);
toolBar.setSize(400, 15);
contentPane.add(toolBar, BorderLayout.NORTH);
setVisible(true);
new Thread(new PaintThread()).start(); // 调用内部类PaintThread,根据里面的设置来重画
}
按照你的要求编写的Java swing 带界面的万年历代码如下
//日历
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class CCI extends JFrame implements ActionListener{
JButton jb1=new JButton("");
JButton jb2=new JButton("");
JButton jb3=new JButton("");
JButton jb4=new JButton("");
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
JPanel jp4=new JPanel();
JLabel jl1=new JLabel();
JLabel jl2=new JLabel();
JLabel[]jl=new JLabel[49];
String []week={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
Calendar c=Calendar.getInstance();
int year,month,day;
int nowyear,nowmonth,nowday;
CCI(){
super("简单日历");
nowyear=c.get(Calendar.YEAR);
nowmonth=c.get(Calendar.MONTH)+1;
nowday=c.get(Calendar.DAY_OF_MONTH);
year=nowyear;
month=nowmonth;
day=nowday;
String s=year+"年"+month+"月";
jl1.setForeground(Color.RED);
jl1.setFont(new Font(null,Font.BOLD,20));
jl1.setText(s);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jp1.add(jb1);jp1.add(jb2);jp1.add(jl1);jp1.add(jb3);jp1.add(jb4);
jp2.setLayout(null);
createMonthPanel();
jp2.add(jp3);
jl2.setFont(new Font(null,Font.BOLD,20));
jl2.setText("今天是"+nowyear+"年"+nowmonth+"月"+nowday+"日");
jp4.add(jl2);
add(jp1,BorderLayout.NORTH);
add(jp2,BorderLayout.CENTER);
add(jp4,BorderLayout.SOUTH);
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==jb1){
year=year-1;
String s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb2){
if(month==1){
year=year-1;
month=12;
}else{
month=month-1;
}
String s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb3){
if(month==12){
year=year+1;
month=1;
}else{
month=month+1;
}
String s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb4){
year=year+1;
String s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
}
public static void main(String[] args) {
new CCI();
}
public int getMonthDays(int year, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 2:
if ((year%4==0year%100!=0)||year%400==0) {
return 29;
} else {
return 28;
}
default:
return 30;
}
}
public void createMonthPanel(){
c.set(year, month-1, getMonthDays(year,month));
int weekOfMonth=c.get(Calendar.WEEK_OF_MONTH);
if(weekOfMonth==6){
jp3.setLayout(new GridLayout(7,7));
jp3.setBounds(50, 20, 420, 350);
}else{
jp3.setLayout(new GridLayout(6,7));
jp3.setBounds(50, 20, 420, 300);
}
jp3.setBorder(BorderFactory.createEtchedBorder());
for(int i=0;i7;i++){
jl[i]=new JLabel(week[i],JLabel.CENTER);
jl[i].setFont(new Font(null,Font.BOLD,20));
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
c.set(year, month-1, 1);
int emptyFirst=c.get(Calendar.DAY_OF_WEEK)-1;
int daysOfMonth=getMonthDays(year,month);
for(int i=6+emptyFirst;i=7;i--){
int intyear=year;
int intmonth=month;
if(intmonth==1){
intyear=intyear-1;
intmonth=12;
}else{
intmonth=intmonth-1;
}
int intdays=getMonthDays(intyear,intmonth);
jl[i]=new JLabel((intdays+7-i)+"",JLabel.CENTER);
jl[i].setFont(new Font(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
for(int i=7+emptyFirst;idaysOfMonth+7+emptyFirst;i++){
jl[i]=new JLabel((i-7-emptyFirst+1)+"",JLabel.CENTER);
jl[i].setFont(new Font(null,Font.BOLD,20));
if((i+1)%7==0 || (i+1)%7==1){
jl[i].setForeground(Color.RED);
}else if((i-7-emptyFirst+1)==nowdaymonth==nowmonthyear==nowyear)
jl[i].setForeground(Color.BLUE);
else
jl[i].setForeground(Color.BLACK);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
if(weekOfMonth==6)
for(int i=48;i=daysOfMonth+emptyFirst+7;i--){
jl[i]=new JLabel((49-i)+"",JLabel.CENTER);
jl[i].setFont(new Font(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
else
for(int i=41;i=daysOfMonth+emptyFirst+7;i--){
jl[i]=new JLabel((42-i)+"",JLabel.CENTER);
jl[i].setFont(new Font(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
}
}
/**
* @(#)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();
//定义中英文字符数组存储星期信息,用于转换显示
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 = 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 系统日期中的月,诸如Jan\Feb
*@return int
*返回一个整数值,用于寻找stringMonthCn[]数组中对应的中文月份
*/
int i;
for(i=0;i12;i++)
if(month.equalsIgnoreCase(stringMonthEn[i]))
break;
return i;
}
/**
*setNowDate()
*设置当前系统日期
*/
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()
*获取组合框中应该显示的年份
*/
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) {
/**
*返回所选年月的天数,因为数组中的数值从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 (gregorianCalendar.isLeapYear(year)) {
//isLeapYear(year)确定当前纪元中的指定年份是否为闰年。
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()
*获取月组合框中的整数值,
*/
public int getComboMonth(){
return combo2.getSelectedIndex();
}
public int getInitWeek(String initWeek){
/**
*getWeekNow(String initWeek)
*@para nowWeek 系统日期中的星期
*返回当月中的1号是从星期几开始
*/
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 本月天数
*@para initWeek 初始星期
*@para day 今天日
*设置月历
*/
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("万年历程序--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("万年历程序--Aidy");
}
}
本文来自CSDN博客,转载请标明出处:
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类,月从0开始, 0代表一月 c.set(Calendar.DATE,1);//1号 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]); } } 把以上测试代码写作一个方法 方法的参数名为年月日, 即可。当然Calendar 还有很多功能,比如一周的第几天,一年的第几个月……