package springboot;
import javautilScanner;
public class 日历最终版 {
public static void main(String[] args) {
Scanner in = new Scanner(Systemin);
while (true) {
Systemoutprintln("请输入年份:");
int year = innextInt();
Systemoutprintln("请输入月份:");
int month = innextInt();
int sum = 0;
for (int i = 0; i < year - 1; i++) {//修改过的地方
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
sum += 366;
} else {
sum += 365;
}
}
for (int i = 1; i <= month - 1; i++) {
if (i == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
sum += 29;
} else {
sum += 28;
}
} else {
if (i == 4 || i == 6 || i == 9 || i == 11) {
sum += 30;
} else {
sum += 31;
}
}
}
int days = 0;
if (month == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
} else {
if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
days = 31;
}
}
int weday = sum % 7;
Systemoutprintln("日\t一\t二\t三\t四\t五\t六");
for (int i = 0; i < weday; i++) {
Systemoutprint("\t");
}
for (int i = 1; i <= days; i++) {
if (sum % 7 == 6) {
Systemoutprint(i + "\n");
} else {
Systemoutprint(i + "\t");
}
sum++;
}
Systemoutprintln("\n\n如果想继续请输入“1”!!!");
int A = innextInt();
if (A == 1) {
continue;
}
break;
}
}
}
MainFramejava是显示日历程序,Clockjava是日历计算程序(可以不要)。
编译后运行MainFrame这个类即可。
swing窗口显示万年历,jdk14以上环境编译运行。
package orgtest;import javaawtBorderLayout;
import javaawtColor;
import javaawtGridLayout;
import javaawtToolkit;
import javaawteventActionEvent;
import javaawteventActionListener;
import javasqlDate;
import javautilCalendar;
import javaxswingJComboBox;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel;
public class MainFrame extends JFrame {
private static final long serialVersionUID = -306484324260972141L;
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, x_size, y_size;
String year_num;
Calendar now = CalendargetInstance(); // 实例化Calendar
MainFrame() {
super("万年历");
setSize(300, 350);
x_size = (int) (ToolkitgetDefaultToolkit()getScreenSize()getWidth());
y_size = (int) (ToolkitgetDefaultToolkit()getScreenSize()getHeight());
setLocation((x_size - 300) / 2, (y_size - 350) / 2);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
panel1add(y_label);
panel1add(com1);
panel1add(m_label);
panel1add(com2);
for (int i = 0; i < 49; i++) {
label[i] = new JLabel("", JLabelCENTER);// 将显示的字符设置为居中
panel2add(label[i]);
}
panel3add(new Clock(this));
paneladd(panel1, BorderLayoutNORTH);
paneladd(panel2, BorderLayoutCENTER);
paneladd(panel3, BorderLayoutSOUTH);
panelsetBackground(Colorwhite);
panel1setBackground(Colorwhite);
panel2setBackground(Colorwhite);
panel3setBackground(Colorwhite);
Init();
com1addActionListener(new ClockAction());
com2addActionListener(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 = IntegerparseInt(com1getSelectedItem()toString()); // 得到当前所选年份
c_month = IntegerparseInt(com2getSelectedItem()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(Colorred); // 将星期日的日期设置为红色
}
for (int i = 6; i < 49; i = i + 7) {
label[i]setForeground(Colorgreen);// 将星期六的日期设置为绿色
}
for (int i = 1; i < 10000; i++) {
com1addItem("" + i);
}
for (int i = 1; i < 13; i++) {
com2addItem("" + i);
}
month_num = (int) (nowget(CalendarMONTH)); // 得到当前时间的月份
year = (int) (nowget(CalendarYEAR)); // 得到当前时间的年份
com1setSelectedIndex(year - 1); // 设置下拉列表显示为当前年
com2setSelectedIndex(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;
nowset(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天
week_num = (int) (nowget(CalendarDAY_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 = CalendargetInstance();
calsetTime(date);
caladd(CalendarMONTH, -1); // 前个月
month_day_score = calgetActualMaximum(CalendarDAY_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) {
JFramesetDefaultLookAndFeelDecorated(true);
new MainFrame();
}
}package orgtest;
import javaawtColor;
import javautilCalendar;
import javaawtCanvas;
import javaawtFont;
import javaawtGraphics;
import javatextSimpleDateFormat;
public class Clock extends Canvas implements Runnable{
private static final long serialVersionUID = 3660124045489727166L;
MainFrame mf;
Thread t;
String time;
public Clock(MainFrame mf){
thismf=mf;
setSize(280,40);
setBackground(Colorwhite);
t=new Thread(this);//实例化线程
tstart();
//调用线程
}
public void run(){
while(true){
try{
Threadsleep(1000);//休眠1秒钟
}catch(InterruptedException e){
Systemoutprintln("异常");
}
thisrepaint(100);
}
}
public void paint(Graphics g){
Font f=new Font("宋体",FontBOLD,16);
SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型
Calendar now=CalendargetInstance();
time=SDFformat(nowgetTime()); //得到当前日期和时间
gsetFont(f);
gsetColor(Colororange);
gdrawString(time,45,25);
}
}
你的问题就是把一周的第一天设置为星期一?
那你贴这么多代码干嘛,一句话的事儿吗不是。建议你多看一下api
Calendar里面的方法就有啊, setFirstDayOfWeek
输入本年的某个月后,屏幕输出这个月的月历,每星期一行,从星期日开始,到星期六结束。
import javautil;
public class Year{
/
判断是否为闰年
/
public boolean getIsRun(int year){
if((year%4==0 && year %100 !=0)||(year%400==0)){
return true;
}
return false;
}
/
返回某年某月有多少天。
/
public int getDay(int year ,int month){
int[] day={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//闰年月份
int[] day1={31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//普通月份
if(thisgetIsRun(year)){
return day[month+1];
}else{
return day1[month+1];
}
}
/
返回这个月的1号是星期几
/
public int getWeek(int year,int month){
Date date=new Date();
datesetYear(year);
datesetMonth(month);
Calendar calendar=CalendargetInstance();
calendarsetTime(date);
return calendarget(calendarDAY_OF_WEEK);
}
/
打印出来,嘎嘎
/
public void showPrint(int year,int month){
int day=thisgetDay(year,month);
int week=thisgetWeek(year,month);
Systemoutprintln("日\t一\t二\t三\t四\t五\t六\n");
for(int i=0;i<=week;i++){//
Systemoutprint (" \t");
}
for(int j=1;j<=day;j++){
if((j+week)%7==0){
Systemoutprintln ();
}
Systemoutprint (j+"\t");
}
// Systemoutprintln(week);
}
public static void main(String[] args){
Year y=new Year();
yshowPrint(2008,3);
}
}
package mycalendar;
import javautil;
class ViewMonth {
int month;
int year;
ViewMonth(final int displayMonth, final int displayYear) {
month = displayMonth;
year = displayYear;
}
private String checkMonth() {
String[] months = {
"1 月" , "2 月" , "3 月",
"4 月" , "5 月" , "6 月",
"7 月" , "8 月" , "9 月",
"10 月" , "11 月" , "12 月"
};
return months[month];
}
private int checkDays() {
int[] numofDays = {
31, 28, 31,
30, 31, 30,
31, 31, 30,
31, 30, 31
};
return numofDays[month];
}
/
使用此方法打印该月的日历
/
void printMonth() {
/ 将该月份起始处的天数留空 /
int initialSpaces = 0;
try {
/ 获取月份名称 /
String monthName = checkMonth();
Systemoutprintln();
Systemoutprintln("\t\t\t " + year + " 年 " + monthName );
Systemoutprintln();
} catch (ArrayIndexOutOfBoundsException ae) {
Systemoutprintln("超出范围 ");
Systemexit(0);
}
GregorianCalendar cal = new GregorianCalendar(year, month, 1);
Systemoutprintln("\t日\t一\t二\t三\t四\t五\t六");
initialSpaces = calget(CalendarDAY_OF_WEEK) - 1;
/ 获取天数 /
int daysInMonth = checkDays();
/ 检查是否为闰年并为二月增加一天 /
if (calisLeapYear(calget(CalendarYEAR)) && month == 1) {
++daysInMonth;
}
for (int ctr = 0; ctr < initialSpaces; ctr++) {
Systemoutprint("\t");
}
for (int ctr = 1; ctr <= daysInMonth; ctr++) {
/ 为单个日期添加空格 /
if (ctr <= 9) {
Systemoutprint(" ");
}
Systemoutprint("\t" + ctr);
/ 检查行的末尾 /
if ((initialSpaces + ctr) % 7 == 0) {
Systemoutprintln();
} else {
Systemoutprint(" ");
}
}
Systemoutprintln();
}
}
class J7上机2 {
protected J7上机2() {
}
public static void main(String[] args) {
int month, year;
if (argslength == 2) {
Systemoutprintln("显示日历");
Systemoutprintln();
int mon = IntegerparseInt(args[0]);
month = mon - 1;
year = IntegerparseInt(args[1]);
} else {
Calendar today = CalendargetInstance();
month = todayget(CalendarMONTH);
year = todayget(CalendarYEAR);
}
ViewMonth mv = new ViewMonth(month, year);
mvprintMonth();
}
}
给你
以上就是关于java程序问题关于日历的全部的内容,包括:java程序问题关于日历的、用java做一个日历显示数据功能,在页面上显示日期数据。、java核心技术的日历程序,模仿着写出来发现一个星期的第一天是星期日,试了各种方法都改不了,附代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)