import java.text.SimpleDateFormat;
成都创新互联-云计算及IDC服务提供商,涵盖公有云、IDC机房租用、达州电信机房、等保安全、私有云建设等企业级互联网基础服务,咨询热线:18980820575
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]);
}
}
按照你的要求编写的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.Scanner; public class Wan{ public static void main(String[] args){ Scanner name = new Scanner(System.in); System.out.print("请输入要查询的年份:"); int year = name.nextInt(); System.out.print("请输入该年的月份"); int month = name.nextInt(); } //累加 该年至输入的月份 天数 //比如 输入2009年的 3月分 // 那就累加 2009年的1月至 3月1号的总天数 public void sumDay(int year,int month){ int day = 0; int sumDay = 0; for(int i = 1;i=month;i++){ switch(i){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: day = 31; break; case 2: if(year % 4 == 0 || year % 400 == 0 year %100!=0){ day = 29; }else{ day = 28; } break; default: day = 30; } //最后一个月份不要累加 因为我们只是要算到该月的一号就可以了 if(i month){ sumDay += day; } } //累加 2000年到该年的一月一号天数 for(int i = 2000;iyear;i++){ if( i % 4 == 0 || i %400== 0 i % 100 != 0){ sumDay += 366; }else{ sumDay += 365; } } //求该月一号为星期几 int week = sumDay % 7 +1; if(week == 7){ week = 0; } } public void fomatDate(int week,int day){ int g = 0; for(int i = 0;iweek;i++){ System.out.print("\t"); } for(int i = 1;i=day;i++){ System.out.print(i+"\t"); g = week + i; if(g % 7 == 0){ System.out.println(); } } } } 给点分哈 写得好累
也不知道你具体需求是什么,以前改过一个日历程序,一共四个java类,放在同一个包里。经测试可以运行。
//Start.java
import java.awt.*;
import javax.swing.*;
class Start{
public static void main(String [] args){
DateFrame frame=new DateFrame();
frame.setLocationRelativeTo(frame);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//DateInfo.java
import java.util.*;
public class DateInfo{
private int mYear, mMonth;
private int mDayOfMonth, mFristWeek;
public DateInfo(int year, int month) throws DateException{
mYear = year;
if (month 0 || month 12){
throw (new DateException());
}
mMonth = month;
mDayOfMonth = getDayOfMonth(mYear, mMonth);
mFristWeek = getFristWeek(mYear, mMonth);
}
private int getDayOfMonth(int year, int month){
int[][] ary = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
return (ary[isLeapYear(year)][month]);
}
private int isLeapYear(int year){
if (year % 4 == 0 year % 100 != 0 ||year % 400 == 0){
return (1);
}
else{
return (0);
}
}
private int getFristWeek(int year, int month){
java.util.Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, 1);
return (cal.get(Calendar.DAY_OF_WEEK) - 1);
}
public String toString(){
String str;
str = "\t\t" + mYear + "年" + mMonth + "月\n";
str += "日\t一\t二\t三\t四\t五\t六\n";
int i;
for (i = 1; i = mFristWeek; i++){
str += " \t";
}
for (int j = 1; j = mDayOfMonth; j++, i++){
str +=j+"\t" ;
if (i % 7 == 0){
str += "\n";
}
}
return (str);
}
}
//DateFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;
class DateFrame extends JFrame implements Runnable{
Calendar date=Calendar.getInstance();
String[] str={"1","2","3","4","5","6","7","8","9","10","11","12"};
JLabel lblYear=new JLabel("年 ");
JLabel lblMonth=new JLabel("月 ");
JLabel lblDate=new JLabel("现在的时间是:");
JLabel lblShowDate=new JLabel();
// javax.swing.JTextField trxt =new JTextField(10);
// trxt.setHorizontalAlignment(JTextField.RIGHT); //设置文本从右边输入
JComboBox cboMonth=new JComboBox(str);
JComboBox cboYear=new JComboBox();
JTextArea txaShow=new JTextArea();
JPanel pnlNorth=new JPanel();
JPanel pnlSOUTH=new JPanel();
JButton btnShow=new JButton("显示");
JButton btnClose=new JButton("关闭");
JScrollPane jsp=new JScrollPane(txaShow);
Container c=this.getContentPane();
public DateFrame(){
Thread thread=new Thread(this);
thread.start();
this.setTitle("玩玩日历拉!!!");
this.setSize(300,260);
for (int i = 1990; i=2025; i++) {
cboYear.addItem(""+i);
}
cboYear.setSelectedItem(""+(date.get(Calendar.YEAR)));
cboMonth.setSelectedItem(""+(date.get(Calendar.MONTH)+1));
pnlNorth.add(cboYear);
txaShow.setTabSize(4); //设置tab键的距离
txaShow.setForeground(Color.GREEN);
pnlNorth.add(lblYear);
pnlNorth.add(cboMonth);
pnlNorth.add(lblMonth);
pnlNorth.add(lblDate);
pnlNorth.add(lblShowDate);
c.add(pnlNorth,BorderLayout.NORTH);
c.add(jsp);
pnlSOUTH.add(btnShow);
pnlSOUTH.add(btnClose);
c.add(pnlSOUTH,BorderLayout.SOUTH);
btnShow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int year=Integer.parseInt((String)cboYear.getSelectedItem());
int month=Integer.parseInt((String)cboMonth.getSelectedItem());
try {
DateInfo date=new DateInfo(year,month);
txaShow.setText(""+date);
}
catch (DateException ex) {
ex.printStackTrace();
}
}
});
btnClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
}
public void run(){
try {
while(true){
Thread.sleep(1000);
int hour=date.get(Calendar.HOUR);
int minute=date.get(Calendar.MINUTE);
int second=date.get(Calendar.SECOND);
String str=hour+":"+minute+":"+second;
lblShowDate.setText(str);
//this.repaint();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
//DateException.java
public class DateException extends Exception{
public DateException(){
super("日期数据不合法.");
}
}