寫了個簡明的,
從事成都服務器托管,服務器租用,云主機,網絡空間,域名注冊,CDN,網絡代維等服務。
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日歷驗證程序如下
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();
}
}
運行結果
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();
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class MyCalendar extends JApplet {
public static final String WEEK_SUN = "SUN";
public static final String WEEK_MON = "MON";
public static final String WEEK_TUE = "TUE";
public static final String WEEK_WED = "WED";
public static final String WEEK_THU = "THU";
public static final String WEEK_FRI = "FRI";
public static final String WEEK_SAT = "SAT";
public static final Color background = Color.white;
public static final Color foreground = Color.black;
public static final Color headerBackground = Color.blue;
public static final Color headerForeground = Color.white;
public static final Color selectedBackground = Color.blue;
public static final Color selectedForeground = Color.white;
private JPanel cPane;
private JLabel yearsLabel;
private JSpinner yearsSpinner;
private JLabel monthsLabel;
private JComboBox monthsComboBox;
private JTable daysTable;
private AbstractTableModel daysModel;
private Calendar calendar;
public MyCalendar() {
cPane = (JPanel) getContentPane();
}
public void init() {
cPane.setLayout(new BorderLayout());
calendar = Calendar.getInstance();
calendar = Calendar.getInstance();
yearsLabel = new JLabel("Year: ");
yearsSpinner = new JSpinner();
yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner, "0000"));
yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));
yearsSpinner.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.YEAR, ((Integer) yearsSpinner.getValue()).intValue());
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, day maxDay ? maxDay : day);
updateView();
}
});
JPanel yearMonthPanel = new JPanel();
cPane.add(yearMonthPanel, BorderLayout.NORTH);
yearMonthPanel.setLayout(new BorderLayout());
yearMonthPanel.add(new JPanel(), BorderLayout.CENTER);
JPanel yearPanel = new JPanel();
yearMonthPanel.add(yearPanel, BorderLayout.WEST);
yearPanel.setLayout(new BorderLayout());
yearPanel.add(yearsLabel, BorderLayout.WEST);
yearPanel.add(yearsSpinner, BorderLayout.CENTER);
monthsLabel = new JLabel("Month: ");
monthsComboBox = new JComboBox();
for (int i = 1; i = 12; i++) {
monthsComboBox.addItem(new Integer(i));
}
monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
monthsComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex());
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, day maxDay ? maxDay : day);
updateView();
}
});
JPanel monthPanel = new JPanel();
yearMonthPanel.add(monthPanel, BorderLayout.EAST);
monthPanel.setLayout(new BorderLayout());
monthPanel.add(monthsLabel, BorderLayout.WEST);
monthPanel.add(monthsComboBox, BorderLayout.CENTER);
daysModel = new AbstractTableModel() {
public int getRowCount() {
return 7;
}
public int getColumnCount() {
return 7;
}
public Object getValueAt(int row, int column) {
if (row == 0) {
return getHeader(column);
}
row--;
Calendar calendar = (Calendar) MyCalendar.this.calendar.clone();
calendar.set(Calendar.DAY_OF_MONTH, 1);
int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int index = row * 7 + column;
int dayIndex = index - moreDayCount + 1;
if (index moreDayCount || dayIndex dayCount) {
return null;
} else {
return new Integer(dayIndex);
}
}
};
daysTable = new CalendarTable(daysModel, calendar);
daysTable.setCellSelectionEnabled(true);
daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
daysTable.setDefaultRenderer(daysTable.getColumnClass(0), new TableCellRenderer() {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
String text = (value == null) ? "" : value.toString();
JLabel cell = new JLabel(text);
cell.setOpaque(true);
if (row == 0) {
cell.setForeground(headerForeground);
cell.setBackground(headerBackground);
} else {
if (isSelected) {
cell.setForeground(selectedForeground);
cell.setBackground(selectedBackground);
} else {
cell.setForeground(foreground);
cell.setBackground(background);
}
}
return cell;
}
});
updateView();
cPane.add(daysTable, BorderLayout.CENTER);
}
public static String getHeader(int index) {
switch (index) {
case 0:
return WEEK_SUN;
case 1:
return WEEK_MON;
case 2:
return WEEK_TUE;
case 3:
return WEEK_WED;
case 4:
return WEEK_THU;
case 5:
return WEEK_FRI;
case 6:
return WEEK_SAT;
default:
return null;
}
}
public void updateView() {
daysModel.fireTableDataChanged();
daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),
calendar.get(Calendar.WEEK_OF_MONTH));
daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_WEEK) - 1,
calendar.get(Calendar.DAY_OF_WEEK) - 1);
}
public static class CalendarTable extends JTable {
private Calendar calendar;
public CalendarTable(TableModel model, Calendar calendar) {
super(model);
this.calendar = calendar;
}
public void changeSelection(int row, int column, boolean toggle, boolean extend) {
super.changeSelection(row, column, toggle, extend);
if (row == 0) {
return;
}
Object obj = getValueAt(row, column);
if (obj != null) {
calendar.set(Calendar.DAY_OF_MONTH, ((Integer)obj).intValue());
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Calendar Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyCalendar myCalendar = new MyCalendar();
myCalendar.init();
frame.getContentPane().add(myCalendar);
frame.setSize(240, 172);
frame.show();
}
}
網頁名稱:java用代碼寫日歷 java編寫一個日歷記事本
本文地址:http://m.newbst.com/article38/doihdpp.html
成都網站建設公司_創新互聯,為您提供微信公眾號、商城網站、微信小程序、營銷型網站建設、App開發、手機網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯