import?java.util.Calendar;
創新互聯公司一直在為企業提供服務,多年的磨煉,使我們在創意設計,營銷型網站到技術研發擁有了開發經驗。我們擅長傾聽企業需求,挖掘用戶對產品需求服務價值,為企業制作有用的創意設計體驗。核心團隊擁有超過十年以上行業經驗,涵蓋創意,策化,開發等專業領域,公司涉及領域有基礎互聯網服務雅安服務器托管、成都App制作、手機移動建站、網頁設計、網絡整合營銷。
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.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文件:
其中
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 萬年歷代碼stm32
文章分享:http://vcdvsql.cn/article36/ddeiepg.html
成都網站建設公司_創新互聯,為您提供標簽優化、搜索引擎優化、網站策劃、微信小程序、關鍵詞優化、做網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯