当前位置:首页 » 编程语言 » java日历

java日历

发布时间: 2022-01-16 20:07:23

❶ 怎样用java编写日历

Java编写日历代码:

importjava.util.Scanner;
publicclassWan{publicstaticvoidmain(String[]args)
{
Scannername=newScanner(System.in);
System.out.print("请输入要查询的年份:");
intyear=name.nextInt();
System.out.print("请输入该年的月份");
intmonth=name.nextInt();
}//累加该年至输入的月份天数
//比如输入2009年的3月分
//那就累加2009年的1月至
3月1号的总天数
publicvoidsumDay(intyear,intmonth)
{
intday=0;
intsumDay=0;
for(inti=1;i<=month;i++){switch(i)
{
case1:
case3:
case5:
case7:
case8:
case10:
case12:
day=31;
break;
case2:
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(inti=2000;i<year;i++)
{if(i%4==0||i%400==0&&i%100!=0)
{sumDay+=366;}else{sumDay+=365;}}
//求该月一号为星期几
intweek=sumDay%7+1;if(week==7){week=0;}}
publicvoidfomatDate(intweek,intday)
{
intg=0;for(inti=0;i<week;i++)
{System.out.print(" ");}
for(inti=1;i<=day;i++){System.out.print(i+" ");
g=week+i;if(g%7==0){System.out.println();}
}
}
}

❷ java 中日历怎么写

Date d = new Date();//获取当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
System.out.println(sdf.format(d));//2016年12月14日22时07分37秒

❸ 关于java的日历程序

修改部分见注释

publicstaticvoidmain(String[]args)
{
System.out.println("日一二三四五六");
Calendarrili=Calendar.getInstance();
rili.set(2011,3,1);
intxingqi=rili.get(Calendar.DAY_OF_WEEK)-1;
Stringa[]=newString[xingqi+30];
for(inti=0;i<xingqi;i++)
{
a[i]="**";
}
for(inti=xingqi,n=1;i<xingqi+30;i++)
{
if(n<=9)
a[i]=String.valueOf(n)+"";
else
a[i]=String.valueOf(n);
n++;
}

//a[i]的i从0开始,你下面从1开始循,自然少了第一个
for(inti=0;i<a.length;i++)
{
//遇到星期六应该是先打印日期,再换行,你之前是先换行,再打星期六
System.out.print(""+a[i]);

//改了循环起始位置,此处改为i+1
if((i+1)%7==0)
{
System.out.println("");

}
}
}

❹ 用JAVA做日历

import java.util.*;

public class test {
public static void main( String[] args ) {
String[] wd= { "日", "一", "二", "三", "四", "五", "六" };
Scanner s = new Scanner(System.in);
P("请输入要查询的年份:");
int y = s.nextInt();
P("请输入月份:");
int m = s.nextInt();
if(y < 2000 || y > 2010) {
P("不在查询范围之内!");
return;
}

GregorianCalendar g = new GregorianCalendar( y, m-1, 1 );

P( "\n星期\t" );
for ( int j = 0; j < wd.length; ++j )
P( wd[j] + "\t" );
P();
for ( int j = 0; j < g.get( Calendar.DAY_OF_WEEK ); ++j )
P( "\t" );
int thisMonth = g.get( Calendar.MONTH );
for ( int j = 1; j <= 31; ++j ) {
int d = g.get( Calendar.DAY_OF_MONTH );
P( d + "\t" );
if ( g.get( Calendar.DAY_OF_WEEK ) == 7 ) {
P("\n\t");
}
g.add( Calendar.DAY_OF_YEAR, 1 );
if ( g.get( Calendar.MONDAY ) != thisMonth ) {
P("\n\n");
break;
}
}
}

static void P( String s )
{
System.out.print( s );
}
static void P()
{
System.out.println();
}
}

❺ Java中日历类(CalendarClass)的用途

Java中日历类(Calendar类)的用途如下:

Calendar类的静态方法getInstance()可以初始化一个日历对象:Calendar now = Calendar.getInstance();

可以使用下面三个方法把日历定到任何一个时间:

set(intyear,intmonth,intdate);
set(intyear,intmonth,intdate,inthour,intminute);
set(intyear,intmonth,intdate,inthour,intminute,intsecond);

如果想获得年份、月份、小时等信息可以使用:

Now.get(Calendar.Month);//这样的方法0表示一月,1表示二月
get(Calendar.DAY_OF_MONTH);//获得这个月的第几天
get(Calendar.DAY_OF_WEEK);//获得这个星期的第几天
get(Calendar.DAY_OF_YEAR);//获得这个年的第几天
getTimeMillis();//获得当前时间的毫秒表示

1.计算某一月份的最大天数

Calendartime=Calendar.getInstance();
time.clear();
time.set(Calendar.YEAR,year);
time.set(Calendar.MONTH,i-1);//注意,Calendar对象默认一月为0
intday=time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数


注:在使用set方法之前,必须先clear一下,否则很多信息会继承自系统当前时间
2.Calendar和Date的转化
(1) Calendar转化为Date

Calendarcal=Calendar.getInstance();
Datedate=cal.getTime();


(2) Date转化为Calendar

Datedate=newDate();
Calendarcal=Calendar.getInstance();
cal.setTime(date);


3.格式化输出日期时间

Datedate=newDate();
SimpleDateFormatdf=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss");
System.out.println(df.format(date));

4.计算相隔天数的方法

publicintgetDaysBetween(Calendard1,Calendard2){
if(d1.after(d2)){//
java.util.Calendarswap=d1;
d1=d2;
d2=swap;
}
intdays=d2.get(Calendar.DAY_OF_YEAR)-d1.get(Calendar.DAY_OF_YEAR);
inty2=d2.get(Calendar.YEAR);
if(d1.get(Calendar.YEAR)!=y2){
d1=(Calendar)d1.clone();
do{
days+=d1.getActualMaximum(Calendar.DAY_OF_YEAR);//得到当年的实际天数
d1.add(Calendar.YEAR,1);
}while(d1.get(Calendar.YEAR)!=y2);
}
returndays;
}

❻ 怎么用java写日历

以下是两个类,请楼主分别存成两个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.(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日历

http://hi..com/qdmmy6/blog/item/7b2b83fffcb739365d60081d.html
这里有张图片,你说的是这种么?

❽ JAVA编写日历

最主要的就是
java.util.Calendar

这个类。

如果你只是要写一个应用程序,就不需要其它特别的

如果你是要窗口形式的,就需要一些swing方面的知识。
如果你是要web形式的,就需要j2ee方面的知识。

❾ java如何在窗口中显示日历

MainFrame.java是显示日历程序,Clock.java是日历计算程序(可以不要)。
编译后运行MainFrame这个类即可。

swing窗口显示万年历,jdk1.4以上环境编译运行。
1132133packageorg.test;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.GridLayout;importjava.awt.Toolkit;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.Date;importjava.util.Calendar;importjavax.swing.JComboBox;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;{=-306484324260972141L;JPanelpanel=newJPanel(newBorderLayout());JPanelpanel1=newJPanel();JPanelpanel2=newJPanel(newGridLayout(7,7));JPanelpanel3=newJPanel();JLabel[]label=newJLabel[49];JLabely_label=newJLabel("年份");JLabelm_label=newJLabel("月份");JComboBoxcom1=newJComboBox();JComboBoxcom2=newJComboBox();intre_year,re_month,x_size,y_size;Stringyear_num;Calendarnow=Calendar.getInstance();//实例化CalendarMainFrame(){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(inti=0;i<49;i++){label[i]=newJLabel("",JLabel.CENTER);//将显示的字符设置为居中panel2.add(label[i]);}panel3.add(newClock(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(newClockAction());com2.addActionListener(newClockAction());setContentPane(panel);setVisible(true);setResizable(false);}{publicvoidactionPerformed(ActionEventarg0){intc_year,c_month,c_week;c_year=Integer.parseInt(com1.getSelectedItem().toString());//得到当前所选年份c_month=Integer.parseInt(com2.getSelectedItem().toString())-1;//得到当前月份,并减1,计算机中的月为0-11c_week=use(c_year,c_month);//调用函数use,得到星期几Resetday(c_week,c_year,c_month);//调用函数Resetday}}publicvoidInit(){intyear,month_num,first_day_num;Stringlog[]={"日","一","二","三","四","五","六"};for(inti=0;i<7;i++){label[i].setText(log[i]);}for(inti=0;i<49;i=i+7){label[i].setForeground(Color.red);//将星期日的日期设置为红色}for(inti=6;i<49;i=i+7){label[i].setForeground(Color.green);//将星期六的日期设置为绿色}for(inti=1;i<10000;i++){com1.addItem(""+i);}for(inti=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);}publicintuse(intreyear,intremonth){intweek_num;now.set(reyear,remonth,1);//设置时间为所要查询的年月的第一天week_num=(int)(now.get(Calendar.DAY_OF_WEEK));//得到第一天的星期returnweek_num;}@SuppressWarnings("deprecation")publicvoidResetday(intweek_log,intyear_log,intmonth_log){intmonth_day_score;//存储月份的天数intcount;month_day_score=0;count=1;Datedate=newDate(year_log,month_log+1,1);//nowCalendarcal=Calendar.getInstance();cal.setTime(date);cal.add(Calendar.MONTH,-1);//前个月month_day_score=cal.getActualMaximum(Calendar.DAY_OF_MONTH);//最后一天for(inti=7;i<49;i++){//初始化标签label[i].setText("");}week_log=week_log+6;//将星期数加6,使显示正确month_day_score=month_day_score+week_log;for(inti=week_log;i<month_day_score;i++,count++){label[i].setText(count+"");}}publicstaticvoidmain(String[]args){JFrame.(true);newMainFrame();}}
0414243packageorg.test;importjava.awt.Color;importjava.util.Calendar;importjava.awt.Canvas;importjava.awt.Font;importjava.awt.Graphics;importjava.text.SimpleDateFormat;{=3660124045489727166L;MainFramemf;Threadt;Stringtime;publicClock(MainFramemf){this.mf=mf;setSize(280,40);setBackground(Color.white);t=newThread(this);//实例化线程t.start();//调用线程}publicvoidrun(){while(true){try{Thread.sleep(1000);//休眠1秒钟}catch(InterruptedExceptione){System.out.println("异常");}this.repaint(100);}}publicvoidpaint(Graphicsg){Fontf=newFont("宋体",Font.BOLD,16);SimpleDateFormatSDF=newSimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型Calendarnow=Calendar.getInstance();time=SDF.format(now.getTime());//得到当前日期和时间g.setFont(f);g.setColor(Color.orange);g.drawString(time,45,25);}}

❿ 如何用JAVA写日历

按照你的要求编写的Java日历验证程序如下

UI.java

importjava.util.Scanner;
publicclassUI{
staticScannersc=newScanner(System.in);
publicstaticintaskInt(Strings){
System.out.print(s);
returnsc.nextInt();
}
publicstaticvoidprintln(Strings){
System.out.println(s);
}
}

EE.java

publicclassEE{
publicvoidvalidateDateCore(){
intyear=UI.askInt("Entertheyear:");
intmonth=UI.askInt("Enterthemonth:");
intday=UI.askInt("Entertheday:");
if(year<1){
UI.println("Theyearisnotavalidnumber.");
return;
}
if(month<1||month>12){
UI.println("Themonthisnotavalidnumber.");
return;
}
intmonthDay=0;
switch(month){
case1:
case3:
case5:
case7:
case8:
case10:
case12:monthDay=31;break;
case4:
case6:
case9:
case11:monthDay=30;break;
case2:
if((year%4==0&&year%100!=0)||year%400==0){
monthDay=29;
}else{
monthDay=28;
}
break;
}
if(day<1||day>monthDay){
UI.println("Thedayisnotavalidnumber.");
return;
}else{
UI.println("Itis"+day+"/"+month+"/"+year+".");
}
}
publicstaticvoidmain(String[]args){
newEE().validateDateCore();
}
}

运行结果

热点内容
绿色linux 发布:2024-11-14 12:56:11 浏览:349
游戏本缓存 发布:2024-11-14 12:55:28 浏览:649
微软提供的编译软件 发布:2024-11-14 12:55:16 浏览:17
长沙java培训机构哪家好 发布:2024-11-14 12:40:53 浏览:228
外存储器硬盘能存储的高清电影数 发布:2024-11-14 12:33:23 浏览:265
python分号作用 发布:2024-11-14 12:31:50 浏览:223
方舟编译器下载要钱吗 发布:2024-11-14 12:29:20 浏览:62
jspoa源码 发布:2024-11-14 12:21:31 浏览:420
不记得了密码怎么办 发布:2024-11-14 12:18:58 浏览:442
python字符串的大小 发布:2024-11-14 12:17:24 浏览:222