「用java做日历」java实现简单的日历思路
今天给各位分享用java做日历的知识,其中也会对java实现简单的日历思路进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、如何用JAVA写日历?
- 2、关于java做日历的问题
- 3、java制作日历
- 4、用JAVA做日历
- 5、怎么用java写日历
如何用JAVA写日历?
按照你的要求编写的Java日历验证程序如下
UI.java
import java.util.Scanner;
public class UI {
static Scanner sc=new Scanner(System.in);
public static int askInt(String s){
System.out.print(s);
return sc.nextInt();
}
public static void println(String s){
System.out.println(s);
}
}
EE.java
public class EE {
public void validateDateCore(){
int year =UI.askInt("Enter the year: ");
int month=UI.askInt("Enter the month: ");
int day=UI.askInt("Enter the day: ");
if(year 1){
UI.println("The year is not a valid number.");
return;
}
if(month1 || month12){
UI.println("The month is not a valid number.");
return;
}
int monthDay=0;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:monthDay=31;break;
case 4:
case 6:
case 9:
case 11:monthDay=30;break;
case 2:
if((year%4==0 year%100!=0) || year%400==0){
monthDay=29;
}else{
monthDay=28;
}
break;
}
if(day1 || daymonthDay){
UI.println("The day is not a valid number.");
return;
}else{
UI.println("It is "+day+"/"+month+"/"+year+".");
}
}
public static void main(String[] args) {
new EE().validateDateCore();
}
}
运行结果
关于java做日历的问题
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class MyCalendar
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String reg = "^(\\d+)[^\\d]+((0?[1-9])|(1[012]))$";
while(true)
{
System.out.println("输入年月(年和月用非数字隔开:如2015.1)(什么都不输入直接退出)");
String line = scanner.nextLine().trim();
if("".equals(line))
{
scanner.close();
break;
}
if(!line.matches(reg))
{
continue;
}
int year = Integer.parseInt(line.replaceAll(reg, "$1"));
int month = Integer.parseInt(line.replaceAll(reg, "$2"));
System.out.println("日\t一\t二\t三\t四\t五\t六");
Calendar calendar = Calendar.getInstance();
// 这个月的1号是星期几
calendar.set(year, month - 1, 1);
int day = calendar.get(Calendar.DAY_OF_WEEK);
int start = Calendar.SUNDAY;
calendar.add(Calendar.DATE, -day + start);
while(start day)
{
System.out.print(calendar.get(Calendar.DATE) + "\t");
calendar.add(Calendar.DATE, 1);
start++;
}
calendar.set(year, month - 1, 1);
Date now = calendar.getTime();
calendar.set(year, month, 1);
Date next = calendar.getTime();
for(Date cur = now; cur.before(next);)
{
calendar.setTime(cur);
int x = calendar.get(Calendar.DATE);
String tmp = x 10 ? "0" + x : x + "";
System.out.print(tmp + "\t");
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
{
System.out.println();
}
calendar.add(Calendar.DATE, 1);
cur = calendar.getTime();
}
calendar.add(Calendar.DATE, -1);
int to = calendar.get(Calendar.DAY_OF_WEEK);
int end = Calendar.SATURDAY;
while(to end)
{
calendar.add(Calendar.DATE, 1);
int x = calendar.get(Calendar.DATE);
String tmp = x 10 ? "0" + x : x + "";
System.out.print(tmp + "\t");
to++;
}
System.out.println();
}
}
}
java制作日历
package com;import java.awt.Color;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.util.Date;import java.util.GregorianCalendar;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;public class Calender2 extends JFrame implements ActionListener, ItemListener { private static final long serialVersionUID = 1L; public static void main(String args[]) { try { Calender2 frame = new Calender2(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } private Date date = new Date(); private GregorianCalendar gregorianCalendar = new GregorianCalendar(); private String[] stringWeek = new String[] { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" }; private String[] stringWeekCn = new String[] { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; private String[] stringMonth = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" }; private String[] strSysTime = new String[6]; // 存储当前日期信息 private String[] strSysNowTime = new String[6]; // 存储运行时日期信息 private JButton[] buttonDay = new JButton[42]; private JButton[] buttonWeek = new JButton[7]; private JLabel labelMonth = new JLabel(); private JButton buttonToday = new JButton(); private JButton buttonLastMonth = new JButton(); private JButton buttonNextMonth = new JButton(); private JComboBox comboYear = new JComboBox(); private JComboBox comboMonth = new JComboBox(); public Calender2() { super("万年历"); getContentPane().setLayout(new GridLayout(8, 7, 3, 5)); setBounds(250, 200, 530, 360); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); comboYear.setForeground(new Color(0, 0, 255)); comboYear.setFont(new Font("", Font.PLAIN, 14)); for (int y = 1900; y 2101; y++) { comboYear.addItem(" " + new Integer(y).toString()); } getContentPane().add(comboYear); comboYear.addItemListener(this); final JLabel labelYear = new JLabel(); labelYear.setForeground(Color.BLUE); labelYear.setFont(new Font("新宋体", Font.PLAIN, 14)); getContentPane().add(labelYear); labelYear.setText(" 年"); comboMonth.setForeground(new Color(0, 0, 255)); comboMonth.setFont(new Font("", Font.PLAIN, 14)); for (int m = 1; m 13; m++) { comboMonth.addItem(" " + new Integer(m).toString()); } getContentPane().add(comboMonth); comboMonth.addItemListener(this); getContentPane().add(labelMonth); labelMonth.setForeground(Color.BLUE); labelMonth.setFont(new Font("新宋体", Font.PLAIN, 14)); labelMonth.setText(" 月"); getContentPane().add(buttonLastMonth); buttonLastMonth.setForeground(Color.BLUE); buttonLastMonth.setFont(new Font("新宋体", Font.PLAIN, 14)); buttonLastMonth.setText("上月"); buttonLastMonth.addActionListener(this); getContentPane().add(buttonToday); buttonToday.setForeground(Color.BLUE); buttonToday.setFont(new Font("新宋体", Font.PLAIN, 14)); buttonToday.setText("今天"); buttonToday.addActionListener(this); getContentPane().add(buttonNextMonth); buttonNextMonth.setForeground(Color.BLUE); buttonNextMonth.setFont(new Font("新宋体", Font.PLAIN, 14)); buttonNextMonth.setText("下月"); buttonNextMonth.addActionListener(this); for (int i = 0; i 7; i++) { buttonWeek[i] = new JButton(); if (i == 0 || i == 6) { buttonWeek[i].setForeground(Color.RED); } else { buttonWeek[i].setForeground(Color.BLUE); } buttonWeek[i].setFont(new Font("新宋体", Font.PLAIN, 12)); buttonWeek[i].setText(stringWeekCn[i]); getContentPane().add(buttonWeek[i]); } for (int i = 0; i 42; i++) { buttonDay[i] = new JButton(); buttonDay[i].setText(""); getContentPane().add(buttonDay[i]); } this.setResizable(false); getSysNowTimeInfo(); setNowDate(); setNowDate(); } public void setSysDate(int year, int month) { // 将日期设置为year年month月1日 gregorianCalendar.set(year, month, 1); } public void actionPerformed(ActionEvent ae) { if (ae.getSource() == buttonToday) { setNowDate(); setNowDate(); } else if (ae.getSource() == buttonLastMonth) { setDate(-1); } else { setDate(1); } } public void itemStateChanged(ItemEvent arg0) { setDate(0); } public void getSysNowTimeInfo() { // 得到程序运行时的时间信息并存储在字符串数组strSysNowTime中 date = gregorianCalendar.getTime(); strSysNowTime = (date + "").split(" "); } public void getSysTimeInfo() { // 得到系统当前的时间信息并存储在字符串数组strSysTime中 date = gregorianCalendar.getTime(); strSysTime = (date + "").split(" "); } public int getNowMonth() { int month = 0; for (int i = 0; i 12; i++) { if (strSysNowTime[1].equalsIgnoreCase(stringMonth[i])) { month = i; break; } } return month; } public int weekStrat(String strWeek) { // 返回字符串strWeek与星期中的第几天匹配,SUN为第一天 int strat = 0; for (int i = 0; i 7; i++) { if (strWeek.equalsIgnoreCase(stringWeek[i])) { strat = i; break; } } return strat; } public void setNowDate() { // 将时间设置为程序运行时的时间 setSysTime(getNowYear(), getNowMonth()); getSysTimeInfo(); setDayNull(); getDay(getMonthDays(getNowYear(), getNowMonth() - 1), getMonthDays( getNowYear(), getNowMonth()), weekStrat(strSysTime[0]), getNowDay()); comboYear.setSelectedIndex(getNowYear() - 1900); comboMonth.setSelectedIndex(getNowMonth()); } public void setDate(int move) { // 将时间设置为选中的年月增加move个月之后的时间 setSysTime(getYear(), getMonth() + move); getSysTimeInfo(); setDayNull(); getDay(getMonthDays(getYear(), getMonth() + move - 1), getMonthDays( getYear(), getMonth() + move), weekStrat(strSysTime[0]), -1); if (move != 0) { if (getMonth() == 0 move 0) { move = 11; comboYear.setSelectedIndex(getYear() - 1901); } else if (getMonth() == 11 move 0) { move = -11; comboYear.setSelectedIndex(getYear() - 1899); } else { comboYear.setSelectedIndex(getYear() - 1900); } comboMonth.setSelectedIndex(getMonth() + move); } } public void setSysTime(int year, int month) { gregorianCalendar.set(year, month, 1); } public int getNowYear() { return Integer.parseInt(strSysNowTime[5]); } public int getNowDay() { return Integer.parseInt(strSysNowTime[2]); } public int getYear() { return comboYear.getSelectedIndex() + 1900; } public int getMonth() { return comboMonth.getSelectedIndex(); } public void setDayNull() { for (int d = 0; d 42; d++) { buttonDay[d].setText(""); } } public void getDay(int lastMonDays, int monthDays, int startWeek, int day) { // 设置日期颜色并打印 for (int d = 0; d startWeek + 1; d++) { buttonDay[d].setForeground(Color.GRAY); buttonDay[d].setText((lastMonDays - startWeek) + d + 1 + ""); } for (int d = startWeek; d startWeek + monthDays; d++) { if ((d - startWeek + 1) == day) { buttonDay[d].setForeground(Color.blue); } else if (d % 7 == 0 || d % 7 == 6) { buttonDay[d].setForeground(Color.RED); } else { buttonDay[d].setForeground(Color.BLACK); } buttonDay[d].setText(d - startWeek + 1 + ""); } for (int d = monthDays + startWeek; d 42; d++) { buttonDay[d].setForeground(Color.GRAY); buttonDay[d].setText(d - (monthDays + startWeek) + 1 + ""); } } public int getMonthDays(int year, int month) { // 返回year年month月的天数 switch (month) { case 3: case 5: case 8: case 10: return 30; case 1: if (gregorianCalendar.isLeapYear(year)) { return 29; } else { return 28; } default: return 31; } }}
用JAVA做日历
import java.util.*;
public class test {
public static void main( String[] args ) {
String[] wd= { "日", "一", "二", "三", "四", "五", "六" };
Scanner s = new Scanner(System.in);
P("请输入要查询的年份:");
int y = s.nextInt();
P("请输入月份:");
int m = s.nextInt();
if(y 2000 || y 2010) {
P("不在查询范围之内!");
return;
}
GregorianCalendar g = new GregorianCalendar( y, m-1, 1 );
P( "\n星期\t" );
for ( int j = 0; j wd.length; ++j )
P( wd[j] + "\t" );
P();
for ( int j = 0; j g.get( Calendar.DAY_OF_WEEK ); ++j )
P( "\t" );
int thisMonth = g.get( Calendar.MONTH );
for ( int j = 1; j = 31; ++j ) {
int d = g.get( Calendar.DAY_OF_MONTH );
P( d + "\t" );
if ( g.get( Calendar.DAY_OF_WEEK ) == 7 ) {
P("\n\t");
}
g.add( Calendar.DAY_OF_YEAR, 1 );
if ( g.get( Calendar.MONDAY ) != thisMonth ) {
P("\n\n");
break;
}
}
}
static void P( String s )
{
System.out.print( s );
}
static void P()
{
System.out.println();
}
}
怎么用java写日历
以下是两个类,请楼主分别存成两个java文件:
其中
MainFrame.java是显示日历程序,Clock.java是日历计算程序。编译后运行MainFrame这个类即可。
1.MainFrame.java
---
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Calendar;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainFrame extends JFrame {
/** *//**
*
*/
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel(new BorderLayout());
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel(new GridLayout(7, 7));
JPanel panel3 = new JPanel();
JLabel[] label = new JLabel[49];
JLabel y_label = new JLabel("年份");
JLabel m_label = new JLabel("月份");
JComboBox com1 = new JComboBox();
JComboBox com2 = new JComboBox();
int re_year, re_month;
int x_size, y_size;
String year_num;
Calendar now = Calendar.getInstance(); // 实例化Calendar
MainFrame() {
super("万年历");
setSize(300, 350);
x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());
y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());
setLocation((x_size - 300) / 2, (y_size - 350) / 2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.add(y_label);
panel1.add(com1);
panel1.add(m_label);
panel1.add(com2);
for (int i = 0; i 49; i++) {
label[i] = new JLabel("", JLabel.CENTER);// 将显示的字符设置为居中
panel2.add(label[i]);
}
panel3.add(new Clock(this));
panel.add(panel1, BorderLayout.NORTH);
panel.add(panel2, BorderLayout.CENTER);
panel.add(panel3, BorderLayout.SOUTH);
panel.setBackground(Color.white);
panel1.setBackground(Color.white);
panel2.setBackground(Color.white);
panel3.setBackground(Color.white);
Init();
com1.addActionListener(new ClockAction());
com2.addActionListener(new ClockAction());
setContentPane(panel);
setVisible(true);
setResizable(false);
}
class ClockAction implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
int c_year, c_month, c_week;
c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到当前所选年份
c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11
c_week = use(c_year, c_month); // 调用函数use,得到星期几
Resetday(c_week, c_year, c_month); // 调用函数Resetday
}
}
public void Init() {
int year, month_num, first_day_num;
String log[] = { "日", "一", "二", "三", "四", "五", "六" };
for (int i = 0; i 7; i++) {
label[i].setText(log[i]);
}
for (int i = 0; i 49; i = i + 7) {
label[i].setForeground(Color.red); // 将星期日的日期设置为红色
}
for (int i = 6; i 49; i = i + 7) {
label[i].setForeground(Color.green);// 将星期六的日期设置为绿色
}
for (int i = 1; i 10000; i++) {
com1.addItem("" + i);
}
for (int i = 1; i 13; i++) {
com2.addItem("" + i);
}
month_num = (int) (now.get(Calendar.MONTH)); // 得到当前时间的月份
year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份
com1.setSelectedIndex(year - 1); // 设置下拉列表显示为当前年
com2.setSelectedIndex(month_num); // 设置下拉列表显示为当前月
first_day_num = use(year, month_num);
Resetday(first_day_num, year, month_num);
}
public int use(int reyear, int remonth) {
int week_num;
now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天
week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期
return week_num;
}
@SuppressWarnings("deprecation")
public void Resetday(int week_log, int year_log, int month_log) {
int month_day_score; // 存储月份的天数
int count;
month_day_score = 0;
count = 1;
Date date = new Date(year_log, month_log + 1, 1); // now
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, -1); // 前个月
month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天
for (int i = 7; i 49; i++) { // 初始化标签
label[i].setText("");
}
week_log = week_log + 6; // 将星期数加6,使显示正确
month_day_score = month_day_score + week_log;
for (int i = week_log; i month_day_score; i++, count++) {
label[i].setText(count + "");
}
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
new MainFrame();
}
}
2.Clock.java
-----
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Calendar;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
//显示时间的类:Clock
/** *//**
* Clock.java
* Summary 数字时间显示
* Created on
* @author
* remark
*/
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Calendar;
class Clock extends Canvas implements Runnable{
/** *//**
*
*/
private static final long serialVersionUID = 3660124045489727166L;
MainFrame mf;
Thread t;
String time;
public Clock(MainFrame mf){
this.mf=mf;
setSize(280,40);
setBackground(Color.white);
t=new Thread(this); //实例化线程
t.start(); //调用线程
}
public void run(){
while(true){
try{
Thread.sleep(1000); //休眠1秒钟
}catch(InterruptedException e){
System.out.println("异常");
}
this.repaint(100);
}
}
public void paint(Graphics g){
Font f=new Font("宋体",Font.BOLD,16);
SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型
Calendar now=Calendar.getInstance();
time=SDF.format(now.getTime()); //得到当前日期和时间
g.setFont(f);
g.setColor(Color.orange);
g.drawString(time,45,25);
}
}
用java做日历的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java实现简单的日历思路、用java做日历的信息别忘了在本站进行查找喔。
发布于:2022-11-30,除非注明,否则均为
原创文章,转载请注明出处。