<small id='pqfriaua'></small><noframes id='2kikwk5r'>

      <i id='6m1l9na2'><tr id='snpdjktg'><dt id='nadj0iyr'><q id='u702eq1x'><span id='vhr9u8ua'><b id='86onlz2a'><form id='0kzoktpu'><ins id='aeivckz3'></ins><ul id='01lt8306'></ul><sub id='3tunftiy'></sub></form><legend id='aw42o5xg'></legend><bdo id='uul37xfu'><pre id='9dky57q2'><center id='2bsnharz'></center></pre></bdo></b><th id='qcchz6n1'></th></span></q></dt></tr></i><div id='2nhjerph'><tfoot id='89ezttfy'></tfoot><dl id='10l98dqs'><fieldset id='hmrgvzis'></fieldset></dl></div>

      • <bdo id='ic52qxm2'></bdo><ul id='pbqto86l'></ul>
    1. <tfoot id='ioeb8yfs'></tfoot>

        <legend id='70d9e91p'><style id='5g2lhdj4'><dir id='yvhaib9y'><q id='uxn60s71'></q></dir></style></legend>
        欢迎来到入门教程网!

        Java编程

        当前位置:主页 > 软件编程 > Java编程 >

        java时钟日历代码 java实现日历

        来源:本站原创|时间:2023-04-07|栏目:Java编程|点击:

        java calendar 时间

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

        Calendar time=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

        Calendar cal=Calendar.getInstance();

        Date date=cal.getTime();

        (2) Date转化为Calendar

        Date date=new Date();

        Calendar cal=Calendar.getInstance();

        cal.setTime(date);

        3、格式化输出日期时间

        Date date=new Date();

        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        System.out.println(df.format(date));

        4、计算一年中的第几星期

        (1)计算某一天是一年中的第几星期

        Calendar cal=Calendar.getInstance();

        cal.set(Calendar.YEAR, 2006);

        cal.set(Calendar.MONTH, 8);

        cal.set(Calendar.DAY_OF_MONTH, 3);

        int weekno=cal.get(Calendar.WEEK_OF_YEAR);

        (2)计算一年中的第几星期是几号

        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal=Calendar.getInstance();

        cal.set(Calendar.YEAR, 2006);

        cal.set(Calendar.WEEK_OF_YEAR, 1);

        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        System.out.println(df.format(cal.getTime()));

        怎么用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.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 日历的代码问题

        首先,控制台输入肯定要是形如2013-03-06这样的格式,这是SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD")所要求的。输入2013-03会进入异常处理提示输入格式不正确。至于打印日历,你可以算出该月的第一号是星期几,然后按顺序排下去,满7天换行就是了

        <small id='y9o5gec9'></small><noframes id='kgduzsdf'>

          1. <i id='23kea1wk'><tr id='ziltn2uy'><dt id='zgrqx5ie'><q id='7p66uy2x'><span id='aht5o3hd'><b id='lu7tcnxw'><form id='4wg5sjec'><ins id='p0jkftq3'></ins><ul id='i6g1dtxl'></ul><sub id='w0pkxy56'></sub></form><legend id='05vns49g'></legend><bdo id='t00ug5l1'><pre id='0pqdlx2t'><center id='yfl5t3ru'></center></pre></bdo></b><th id='j5xze2e5'></th></span></q></dt></tr></i><div id='wudg1ipr'><tfoot id='j3rtgssw'></tfoot><dl id='vdwwfmal'><fieldset id='2bx9ivf7'></fieldset></dl></div>
              • <bdo id='6dy6fdew'></bdo><ul id='rfecak63'></ul>
              • <tfoot id='vwcwbi68'></tfoot>

                  <legend id='d0byrftz'><style id='yrm6p2jq'><dir id='cenag9q5'><q id='g4s0qo3n'></q></dir></style></legend>
                    <tbody id='gq6kv3z2'></tbody>

                  上一篇:java购物车系统源代码 javabean购物车

                  栏    目:Java编程

                  下一篇:没有了

                  本文标题:java时钟日历代码 java实现日历

                  本文地址:https://www.xiuzhanwang.com/a1/Javabiancheng/17303.html

                  网页制作CMS教程网络编程软件编程脚本语言数据库服务器

                  如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

                  联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

                  Copyright © 2002-2020 脚本教程网 版权所有

                  <tfoot id='2gh1qhqj'></tfoot>

                  <i id='8scuz3sy'><tr id='xm8lg4l2'><dt id='eql3g1oc'><q id='b10e9mjs'><span id='tpakj0jd'><b id='8e28t0sg'><form id='imdd2lk5'><ins id='ap145ywr'></ins><ul id='33kjom5a'></ul><sub id='zeln887s'></sub></form><legend id='h4g8l242'></legend><bdo id='kbjjp860'><pre id='mebntftn'><center id='f4t0oba7'></center></pre></bdo></b><th id='gigkwr8u'></th></span></q></dt></tr></i><div id='siapk2kc'><tfoot id='u5wu16bi'></tfoot><dl id='ud9fbhe8'><fieldset id='ekw33k2d'></fieldset></dl></div>

                  1. <legend id='puumv925'><style id='kh4aaic4'><dir id='qhlfk7q1'><q id='jasqsmsi'></q></dir></style></legend>

                      • <bdo id='6t7kv84k'></bdo><ul id='lu59743n'></ul>
                    1. <small id='xxlwvy1r'></small><noframes id='hnet9bku'>