本文目錄一覽:
- 1、Java編寫程序,輸入年份,輸出本年度各月份日曆
- 2、在java里怎麼做萬年曆,一年的啊
- 3、Java日曆打印,怎麼加輸入年份,打印整年12個月打印,輸入月份打印季節,下面代碼如何優化,給個思路?
- 4、java日曆,求代碼
Java編寫程序,輸入年份,輸出本年度各月份日曆
寫了個簡明的,
import java.util.Calendar;
import java.util.Scanner;
public class Test {
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;iwday-1;i++) System.out.print(” \t”);
}
System.out.print(mday+”\t”);
if(wday==7) System.out.println();
c.add(Calendar.DAY_OF_YEAR, 1);
}
}
}
=======
請輸入年份:
2012
日 一 二 三 四 五 六 第1月
—————————————————
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 31
日 一 二 三 四 五 六 第2月
—————————————————
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
日 一 二 三 四 五 六 第3月
—————————————————
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 31
日 一 二 三 四 五 六 第4月
—————————————————
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
日 一 二 三 四 五 六 第5月
—————————————————
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 31
日 一 二 三 四 五 六 第6月
—————————————————
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
日 一 二 三 四 五 六 第7月
—————————————————
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 31
日 一 二 三 四 五 六 第8月
—————————————————
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 31
日 一 二 三 四 五 六 第9月
—————————————————
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
日 一 二 三 四 五 六 第10月
—————————————————
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 31
日 一 二 三 四 五 六 第11月
—————————————————
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
日 一 二 三 四 五 六 第12月
—————————————————
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 31
在java里怎麼做萬年曆,一年的啊
先上張效果圖:以下是實現代碼:/*日曆*/
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,根據裏面的設置來重畫
}
public static void main(String[] args) {
Demo28 d28 = new Demo28();
d28.paint();
}
// 鼠標單擊左邊按鈕觸發的事件
class myMouseListener1 extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
String str = tf.getText().trim(); // 得到文本框的值
int i = Integer.parseInt(str);
i = i – 1;
tf.setText(String.valueOf(i));
String new_year = String.valueOf(i); // 把表示年份的文本框的值存放到變量new_year里
ca.set(Calendar.YEAR, i); // 把Calendar 對象的YEAR設置為用戶設置的年份
String new_month = (String) jb.getSelectedItem(); // 得到月份值
ca.set(Calendar.MONTH, Integer.parseInt(new_month) – 1); // 把Calendar對象的MONTH設置為用戶設置的月份
ca.set(Calendar.DATE, 1); // 把Calendar 對象的DATA設置為1
String new_week = String.valueOf(ca.get(Calendar.DAY_OF_WEEK)); // 根據設置的Calendar對象,計算出這個月第一天是星期幾
int Max_Day = ca.getActualMaximum(Calendar.DATE); // 根據設置後的Calendar對象計算這個月份有多少天
getDate(new_year, new_month, new_week, Max_Day);
}
}
class myKeyListener extends KeyAdapter {
public void keyReleased(KeyEvent e) {
try {
int i = Integer.parseInt(tf.getText().trim());
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
String new_year = String.valueOf(i);
ca.set(Calendar.YEAR, i); // 把Calendar對象的YEAR設置為用戶設置的年份
String new_month = (String) jb.getSelectedItem(); // 得到月份值
ca.set(Calendar.MONTH, Integer.parseInt(new_month) – 1); // 把Calendar對象的MONTH設置為用戶設置的月份
ca.set(Calendar.DATE, 1); // 把Calendar 對象的DATA設置為1
String new_week = String.valueOf(ca
.get(Calendar.DAY_OF_WEEK)); // 根據設置的Calendar對象,計算出這個月第一天是星期幾
int Max_Day = ca.getActualMaximum(Calendar.DATE); // 根據設置後的Calendar對象計算這個月份有多少天
getDate(new_year, new_month, new_week, Max_Day);
}
} catch (NumberFormatException excption) {
System.out.println(“你輸入的年份不正確!”);
}
}
}
// 鼠標單擊右邊按鈕觸發的事件
class myMouseListener2 extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
String str = tf.getText().trim();
int i = Integer.parseInt(str);
i = i + 1;
tf.setText(String.valueOf(i));
String new_year = String.valueOf(i); // 把表示年份的文本框的值存放到變量new_year里
ca.set(Calendar.YEAR, i); // 把Calendar 對象的YEAR設置為用戶設置的年份
String new_month = (String) jb.getSelectedItem(); // 得到月份值
ca.set(Calendar.MONTH, Integer.parseInt(new_month) – 1); // 把Calendar對象的MONTH設置為用戶設置的月份
ca.set(Calendar.DATE, 1); // 把Calendar 對象的DATA設置為1
String new_week = String.valueOf(ca.get(Calendar.DAY_OF_WEEK)); // 根據設置的Calendar對象,計算出這個月第一天是星期幾
int Max_Day = ca.getActualMaximum(Calendar.DATE); // 根據設置後的Calendar對象計算這個月份有多少天
getDate(new_year, new_month, new_week, Max_Day);
}
}
// 鼠標單擊選擇框觸發的事件
class myActionListener3 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String new_year = String.valueOf(ca.get(Calendar.YEAR)); // 把表示年份的文本框的值存放到變量new_year里
String new_month = (String) jb.getSelectedItem(); // 得到用戶設置的月份
ca.set(Calendar.MONTH, Integer.parseInt(new_month) – 1); // 把Calendar對象的月份值設置為用戶定義的月份
ca.set(Calendar.DATE, 1); // 把Calendar 對象的DATA設置為1
String new_week = String.valueOf(ca.get(Calendar.DAY_OF_WEEK)); // 根據設置的Calendar對象,計算出這個月第一天是星期幾
int Max_Day = ca.getActualMaximum(Calendar.DATE); // 根據設置後的Calendar對象計算這個月份有多少天
getDate(new_year, new_month, new_week, Max_Day);
}
}
// 重畫組件
private class PaintThread implements Runnable {
public void run() {
while (true) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Java日曆打印,怎麼加輸入年份,打印整年12個月打印,輸入月份打印季節,下面代碼如何優化,給個思路?
建議封裝函數,使用數組完成所有的打印
比如獲取天數,getDays(int year,int month)
獲取星期,getDay(int allDays)
獲取極度,getQuater(int month)
然後打印重載寫print方法,傳入的flag不同打印年 嫉妒等
java日曆,求代碼
import java.util.Scanner;
/**
* java輸入年,月得到日曆
*
* @author young
*
*/
public class PrintCalendar {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(“Enter full year (eg:2001):”);
int year = input.nextInt();
System.out.print(“Enter month in number 1 between 12:”);
int month = input.nextInt();
printMonth(year, month);
}
public static void printMonth(int year, int month) {
printMonthTitle(year, month);
printMonthBody(year, month);
}
public static void printMonthTitle(int year, int month) {
System.out.println(” ” + getMonthName(month) + ” ” + year);
System.out.println(“______________________________”);
System.out.println(” Sun Mon Tue Wed Thu Fri Sat”);
}
public static String getMonthName(int month) {
String monthName = ” “;
switch (month) {
case 1:
monthName = “Fanuary”;
break;
case 2:
monthName = “January”;
break;
case 3:
monthName = “March”;
break;
case 4:
monthName = “April”;
break;
case 5:
monthName = “May”;
break;
case 6:
monthName = “June”;
break;
case 7:
monthName = “July”;
break;
case 8:
monthName = “August”;
break;
case 9:
monthName = “September”;
break;
case 10:
monthName = “October”;
break;
case 11:
monthName = “November”;
break;
case 12:
monthName = “December”;
}
return monthName;
}
public static void printMonthBody(int year, int month) {
int startDay = getStartDay(year, month);
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
int i = 0;
for (i = 0; i startDay; i++) {
System.out.printf(“%4s”, ” “);
}
for (i = 1; i = numberOfDaysInMonth; i++) {
System.out.printf(“%4d”, i);
if ((i + startDay) % 7 == 0)
System.out.println();
}
// System.out.println();
}
public static int getStartDay(int year, int month) {
final int START_DAY_FOR_JAN_1_1800 = 3;
int totalNumberOfDays = getTotalNumberOfDays(year, month);
return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;
}
public static int getTotalNumberOfDays(int year, int month) {
int total = 0;
for (int i = 1800; i year; i++)
if (isLeapYear(i))
total = total + 366;
else
total = total + 365;
for (int i = 1; i month; i++)
total = total + getNumberOfDaysInMonth(year, i);
return total;
}
// 每個月天數
public static int getNumberOfDaysInMonth(int year, int month) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12)
return 31;
if (month == 4 || month == 6 || month == 8 || month == 11)
return 30;
if (month == 2)
return isLeapYear(year) ? 29 : 28;
return 0;
}
// 判斷閏年
public static boolean isLeapYear(int year) {
return year % 400 == 0 || (year % 4 == 0 year % 100 != 0);
}
}
原創文章,作者:OAAN,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/136886.html