java日历小程序
java日历实验报告

java日历实验报告Java日历实验报告引言在计算机科学领域中,日历是一种常见的应用程序。
它不仅可以帮助我们追踪时间和日期,还可以提供一些方便的功能,如提醒、日程安排等。
在本次实验中,我们将使用Java编程语言来实现一个简单的日历应用程序,并对其进行测试和评估。
实验目的本次实验的主要目的是通过实现一个日历应用程序来加深对Java编程语言的理解和掌握。
通过这个实验,我们将学习如何使用Java的日期和时间类来处理日期和时间相关的操作,并且能够将这些操作应用到实际的应用程序中。
实验步骤1. 设计日历应用程序的功能和界面:在开始编写代码之前,我们首先需要确定日历应用程序的功能和界面设计。
我们可以考虑添加以下功能:显示当前日期、切换月份、添加日程安排等。
在界面设计方面,我们可以使用图形用户界面(GUI)来提供更好的用户体验。
2. 实现日历应用程序的核心功能:在设计完成后,我们可以开始编写代码来实现日历应用程序的核心功能。
首先,我们需要使用Java的日期和时间类来获取当前日期,并将其显示在界面上。
然后,我们可以添加一些按钮或菜单来实现切换月份的功能。
最后,我们可以使用文本框或文本区域来让用户输入日程安排,并将其保存到文件或数据库中。
3. 进行测试和评估:在完成编码后,我们需要对日历应用程序进行测试和评估。
我们可以输入一些测试用例,如切换月份、添加日程安排等,来验证程序的正确性和稳定性。
如果发现了问题或错误,我们需要进行调试和修复,直到程序能够正常运行。
实验结果经过测试和评估,我们的日历应用程序能够正常运行,并且具备基本的功能。
用户可以通过界面来查看当前日期,并且可以方便地切换月份。
用户还可以添加日程安排,并且程序能够将其保存和显示。
讨论与总结通过本次实验,我们深入学习了Java的日期和时间类,并将其应用到实际的日历应用程序中。
我们了解了如何获取当前日期、切换月份以及添加日程安排等操作。
同时,我们也发现了一些问题和改进的空间,如界面设计可以更加美观,功能可以更加完善等。
javacalendar日期实现不断加一天的代码

javacalendar⽇期实现不断加⼀天的代码
我就废话不多说了,⼤家还是直接看代码吧~
Calendar cc=Calendar.getInstance();//获得系统时间
cc.add(cc.DATE, 1);//让⽇⼦每天向后加⼀天
date=cc.getTime(); //这个时间就是系统时间加⼀天后的
补充知识:Java中对⽇期进⾏增加天数、⽉数、年
有时我们需要对时间数据进⾏处理,⽐如⽇期增加⼀天,增加⼀个⽉,下⾯是代码:
import java.util.Date;
int Day = 1;
Calendar calendar = new GregorianCalendar(); // 定义calendar对象
calendar.setTime(new Date()); // 把当前系统时间赋值给calendar
calendar.add(calendar.DATE, day); // 在⽇期中增加天数,1天
Date date = calendar.getTime(); // 把calendar转换回⽇期格式
以上这篇java calendar ⽇期实现不断加⼀天的代码就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
java万年历程序

public Mycalendar(){import java.awt.*;import java.awt.event.*;import java.io.*;import java.text.DateFormat ;import java.text.SimpleDateFormat ;import java.util.Calendar ;import java.util.Date ;import javax.swing.*;import javax.swing.table.DefaultTableModel;public class Mycalendar extends JFrame implements ActionListener , MouseListener {private Calendar cld = Calendar .getInstance();private String [] astr = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};private DefaultTableModel dtm = new DefaultTableModel(null ,astr);private JTable table = new JTable(dtm); //装日期的表格private JScrollPane sp = new JScrollPane(table);private JButton bLastYear = new JButton("上一年");private JButton bNextYear = new JButton("下一年");private JButton bLastMonth = new JButton("上月");private JButton bNextMonth = new JButton("下月");private JTextField jtfYear = new JTextField(5);//jtfYear 年份显示和输入文本框private JTextField jtfMonth = new JTextField(2);//jtfMonth 月份显示文本框private JPanel p1 = new JPanel(); //装入控制日期按钮的模块private JPanel p2 = new JPanel();private JPanel p3 = new JPanel(new BorderLayout ());private JPanel p4 = new JPanel(new GridLayout (2,1));private JPanel p5 = new JPanel(new BorderLayout ());private JButton bAdd = new JButton("保存日志");private JButton bDel = new JButton("删除日志");private JTextArea jta = new JTextArea();private JScrollPane jsp = new JScrollPane(jta);//jta--JTextAreaprivate JLabel l = new JLabel("您可以在年份文本框中键入您要查找的年份");private JLabel lt = new JLabel();private JLabel ld = new JLabel();private int lastTime;super("万年历记事本"); //框架命名this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭函数this.getContentPane().setLayout(new BorderLayout(10, 0));jta.setLineWrap(true);table.setGridColor(Color.GRAY); //星期之间的网格线是灰色的table.setColumnSelectionAllowed(true);table.setSelectionBackground(Color.BLACK);//当选定某一天时这一天背景黑色table.setSelectionForeground(Color.RED);//选定的日期字体是绿色的table.setBackground(new Color(242,240,33));//日期显示表格颜色蓝色table.setFont(new Font("楷体",Font.BOLD,15));//日期数字字体格式table.setRowHeight(30);//表格的高度table.addMouseListener(this); //鼠标监听器jtfYear.addActionListener(this);//可输入年份的文本框//为各个按钮添加监听函数bAdd.addActionListener(this);bDel.addActionListener(this);bLastYear.addActionListener(this);bNextYear.addActionListener(this);bLastMonth.addActionListener(this);bNextMonth.addActionListener(this);//将按钮添加到Jpanel上p1.add(bLastYear);p1.add(jtfYear);//年份输入文本框p1.add(bNextYear);p1.add(bLastMonth);p1.add(jtfMonth);p1.add(bNextMonth);p2.add(bAdd);p2.add(bDel);p3.add(jsp, BorderLayout.CENTER);p3.add(p2, BorderLayout.SOUTH);p3.add(ld, BorderLayout.NORTH);p4.add(l);p4.add(lt);p5.add(p4, BorderLayout.SOUTH);p5.add(sp, BorderLayout.CENTER);p5.add(p1, BorderLayout.NORTH);this.getContentPane().add(p5, BorderLayout.CENTER);this.getContentPane().add(p3, BorderLayout.EAST);String [] strDate = DateFormat.getDateInstance().format(new Date()).split("-");//获得日期cld.set(Integer.parseInt(strDate[0]), Integer.parseInt(strDate[1])-1, 1);showCalendar(Integer.parseInt(strDate[0]), Integer.parseInt(strDate[1]), cld);jtfMonth.setEditable(false);//设置月份的文本框为不可编辑jtfYear.setText(strDate[0]);jtfMonth.setText(strDate[1]);this.showTextArea(strDate[2]);ld.setFont(new Font("楷体",Font.BOLD,14));new Timer(lt).start();this.setBounds(200,200,600,320);this.setResizable(false);this.setVisible(true);}public void showCalendar(int localYear, int localMonth, Calendar cld){int Days = getDaysOfMonth(localYear, localMonth) + cld.get(Calendar.DAY_OF_WEEK) - 2;Object [] ai = new Object[7];lastTime = 0;for (int i = cld.get(Calendar.DAY_OF_WEEK)-1; i <= Days; i++){ai[i%7] = String.valueOf(i- (cld.get(Calendar.DAY_OF_WEEK)-2));if (i%7 == 6){dtm.addRow(ai);ai = new Object[7];lastTime++;}}dtm.addRow(ai);}public int getDaysOfMonth(int year, int Month) //显示所选月份的天数{if(Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12){return 31;}if(Month == 4 || Month == 6 || Month == 9 || Month == 11){return 30;}if(year%4 == 0 && year%100 != 0 || year%400 == 0)//闰年{return 29;}else{return 28;}}public void actionPerformed(ActionEvent e){if(e.getSource() == jtfYear || e.getSource() == bLastYear || e.getSource() == bNextYear ||e.getSource() == bLastMonth || e.getSource() == bNextMonth){int m, y;try//控制输入的年份正确,异常控制{if (jtfYear.getText().length() != 4){throw new NumberFormatException();}y = Integer.parseInt(jtfYear.getText());m = Integer.parseInt(jtfMonth.getText());}catch (NumberFormatException ex){JOptionPane.showMessageDialog(this, "请输入位-9的数字! ", "年份有误", JOptionPane.ERROR_MESSAGE);return ;}ld.setText("没有选择日期");for (int i = 0; i < lastTime+1; i++){ dtm.removeRow(0);}if(e.getSource() == bLastYear){ jtfYear.setText(String.valueOf(--y)); }if(e.getSource() == bNextYear){jtfYear.setText(String.valueOf(++y)); }if(e.getSource() == bLastMonth){if(m == 1){jtfYear.setText(String.valueOf(--y));m = 12;jtfMonth.setText(String.valueOf(m));}else{jtfMonth.setText(String.valueOf(--m));}}if(e.getSource() == bNextMonth){if(m == 12){jtfYear.setText(String.valueOf(++y));m = 1;jtfMonth.setText(String.valueOf(m));}else{jtfMonth.setText(String.valueOf(++m));}}cld.set(y, m-1, 0);showCalendar(y, m, cld);}if(e.getSource() == bAdd){int r = table.getSelectedRow();int c = table.getSelectedColumn();if(!ld.getText().equals("没有选择日期")){try{File file = new File(ld.getText() + ".txt");BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));bw.write(jta.getText());bw.close();}catch (FileNotFoundException ex){ex.printStackTrace();}catch (IOException ex){ex.printStackTrace();}}}if(e.getSource() == bDel){int r = table.getSelectedRow();int c = table.getSelectedColumn();File filedel = new File(ld.getText() + ".txt");if(filedel.exists()){if(filedel.delete()){jta.setText("日志删除成功");}else{jta.setText("日志删除失败");}}else{jta.setText("没有找到日志文件");}}}public void mouseClicked(MouseEvent e){jta.setText(null);int r = table.getSelectedRow();int c = table.getSelectedColumn();if (table.getValueAt(r,c) == null){ld.setText("没有选择日期");}else{this.showTextArea(table.getValueAt(r,c));}}public void mousePressed(MouseEvent e){}public void mouseReleased(MouseEvent e){}public void mouseEntered(MouseEvent e){}public void mouseExited(MouseEvent e){}private void showTextArea(Object selected){ld.setText(jtfYear.getText()+"年"+jtfMonth.getText()+"月"+selected+"日");File filein = new File(ld.getText() + ".txt");if(filein.exists()){try{BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filein)));String strRead = br.readLine();jta.setText(null);while(strRead != null){jta.append(strRead);strRead = br.readLine();}br.close();}catch (FileNotFoundException ex){ex.printStackTrace();}catch (IOException ex){ex.printStackTrace();}}}public static void main(String[] args){JFrame.setDefaultLookAndFeelDecorated(true);JDialog.setDefaultLookAndFeelDecorated(true);new Mycalendar();}}class Timer extends Thread//显示系统时间{private JLabel lt;private SimpleDateFormat fy = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");private SimpleDateFormat fn = new SimpleDateFormat("yyyy.MM.dd G 'at' HH mm ss z");private boolean b = true;public Timer(JLabel lt){this.lt = lt;}public void run(){while (true){try{if (b){lt.setText(fy.format(new Date()));}else{lt.setText(fn.format(new Date()));}b = !b;this.sleep(500);}catch (InterruptedException ex){ex.printStackTrace();}}}}。
JAVA----日历源代码

JAVA----⽇历源代码1:先创建⼀个CalendarBean类:代码:1. import java.util.Calendar;2. public class CalendarBean3. {4. String day[];5. int year=2005,month=0;6. public void setYear(int year)7. {8. this.year=year;9. }10. public int getYear()11. {12. return year;13. }14. public void setMonth(int month)15. {16. this.month=month;17. }18. public int getMonth()19. {20. return month;21. }22. public String[] getCalendar()23. {24. String a[]=new String[42];25. Calendar date=Calendar.getInstance();26. date.set(year,month-1,1);27. int week=date.get(Calendar.DAY_OF_WEEK)-1;28. int day=0;29. //判断⼤⽉份30. if(month==1||month==3||month==5||month==731. ||month==8||month==10||month==12)32. {33. day=31;34. }35. //判断⼩⽉36. if(month==4||month==6||month==9||month==11)37. {38. day=30;39. }40. //判断平年与闰年41. if(month==2)42. {43. if(((year%4==0)&&(year%100!=0))||(year%400==0))44. {45. day=29;46. }47. else48. {49. day=28;50. }51. }52. for(int i=week,n=1;i<week+day;i++)53. {54. a[i]=String.valueOf(n) ;55. n++;56. }57. return a;58. }59. }2:创建⼀个CalendarFrame类import javax.swing.*;public class CalendarFrame extends JFrame implements ActionListener {JLabel labelDay[]=new JLabel[42];JTextField text=new JTextField(10);JButton titleName[]=new JButton[7];JButton button=new JButton();String name[]={"⽇","⼀","⼆","三","四","五","六"};JButton nextMonth,previousMonth;int year=1996,month=1;CalendarBean calendar;JLabel showmessage=new JLabel("",JLabel.CENTER);JLabel lbl1=new JLabel("请输⼊年份:");JLabel lbl2=new JLabel(" ");public CalendarFrame(){JPanel pCenter=new JPanel();//将pCenter的布局设置为7⾏7列的GridLayout布局pCenter.setLayout(new GridLayout(7,7));//pCenter添加组件titleName[i]for(int i=0;i<7;i++){titleName[i]=new JButton(name[i]);pCenter.add(titleName[i]);}//pCenter添加组件LabelDay[i]for(int i=0;i<42;i++){labelDay[i]=new JLabel("",JLabel.CENTER);pCenter.add(labelDay[i]);}text.addActionListener(this);calendar=new CalendarBean();calendar.setYear(year);calendar.setMonth(month);String day[]=calendar.getCalendar();for(int i=0;i<42;i++){labelDay[i].setText(day[i]);}nextMonth=new JButton("下⽉");previousMonth=new JButton("上⽉");button=new JButton("确定");//注册监听器nextMonth.addActionListener(this);previousMonth.addActionListener(this);button.addActionListener(this);JPanel pNorth=new JPanel(),pSouth=new JPanel();pNorth.add(showmessage);pNorth.add(lbl2);pNorth.add(previousMonth);pNorth.add(nextMonth);pSouth.add(lbl1);pSouth.add(text);pSouth.add(button);showmessage.setText("⽇历"+calendar.getYear()+"年"+calendar.getMonth()+"⽉"); ScrollPane scrollPane=new ScrollPane();scrollPane.add(pCenter);add(scrollPane,BorderLayout.CENTER);//窗⼝添加到ScrollPane 中间位置add(pNorth,BorderLayout.NORTH); //窗⼝添加pNorth在窗⼝北⾯add(pSouth,BorderLayout.SOUTH); //窗⼝添加pSouth在窗⼝的南⾯}public void actionPerformed(ActionEvent e){if(e.getSource()==nextMonth){month+=1;if(month>12)month=1;calendar.setMonth(month);String day[]=calendar.getCalendar();for(int i=0;i<42;i++){labelDay[i].setText(day[i]);}}else if(e.getSource()==button){month+=1;if(month>12)month=1;calendar.setYear(Integer.parseInt(text.getText()));String day[]=calendar.getCalendar();for(int i=0;i<42;i++){labelDay[i].setText(day[i]);}showmessage.setText("⽇历"+calendar.getYear()+"年"+calendar.getMonth()+"⽉"); }}}3:在创建⼀个CalendarMainClass类代码:import javax.swing.JFrame;import javax.swing.UIManager;public class CalendarMainClass {public static void main(String[]args){try{UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); }catch(Exception e){e.printStackTrace();}CalendarFrame frame=new CalendarFrame();frame.setBounds(100,100,360,300);frame.setTitle("中华⽇历");frame.setLocationRelativeTo(null);//窗体居中显⽰frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}运⾏结果:。
java万年历思路

java万年历思路Java万年历思路前言万年历是一个常见的日历应用程序,它可以显示公历和农历日期、节气、节日等信息。
在本文中,我们将介绍如何使用Java编写一个简单的万年历程序。
一、需求分析在开始编写程序之前,我们需要确定我们的程序需要实现哪些功能。
下面是我们的需求分析:1. 显示当前日期和时间2. 显示公历和农历日期3. 显示当天节气和节日4. 支持查看任意日期的公历和农历信息二、算法设计为了实现上述功能,我们需要使用一些算法来计算日期信息。
下面是我们将要使用的算法:1. 计算某一年是否是闰年2. 计算某一月有多少天3. 计算某一天是星期几4. 计算某一天对应的农历日期5. 计算某一天对应的节气和节日三、界面设计为了让用户能够方便地使用我们的程序,我们需要设计一个简单易用的界面。
下面是我们将要使用的界面元素:1. 标题栏:显示程序名称和当前日期时间2. 日历区域:显示公历和农历日期以及当天节气和节日3. 左右箭头按钮:用于切换日期4. 日期选择框:用于选择任意日期四、代码实现在进行代码实现之前,我们需要先导入一些Java类库来帮助我们完成任务。
下面是我们需要使用的Java类库:1. java.util.Calendar:用于处理日期和时间2. java.util.GregorianCalendar:Calendar的子类,用于处理公历日期3. java.text.SimpleDateFormat:用于格式化日期和时间4. java.util.TimeZone:用于设置时区5. java.util.HashMap:用于存储节气和节日信息下面是我们的代码实现:1. 计算某一年是否是闰年public static boolean isLeapYear(int year) {return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);}2. 计算某一月有多少天public static int getDaysOfMonth(int year, int month) {if (month == 2) {return isLeapYear(year) ? 29 : 28;} else if (month == 4 || month == 6 || month == 9 || month== 11) {return 30;} else {return 31;}}3. 计算某一天是星期几public static int getWeekday(int year, int month, int day) {Calendar calendar = new GregorianCalendar(year, month - 1, day);return calendar.get(Calendar.DAY_OF_WEEK) - 1;}4. 计算某一天对应的农历日期public static String getLunarDate(int year, int month, int day) { // 省略代码}5. 计算某一天对应的节气和节日public static String getSolarTerm(int year, int month, int day) { // 省略代码}public static String getFestival(int year, int month, int day) {// 省略代码}6. 界面实现// 省略代码五、总结在本文中,我们介绍了如何使用Java编写一个简单的万年历程序。
日历源代码——java

日历源代码——java//import java.sql.Date;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.Locale;public class GregorianCalendar日历 {public static void main(String[] args) { //设置不同地区Locale.setDefault();//创建当前日历对象GregorianCalendar now = new GregorianCalendar();//从当前时期对象是取出时间日期对象//编辑错误:Type mismatch: cannot convert from java.util.Date to java.sql.DateDate date = now.getTime();//将时间日期对象按字符形式打印System.out.println(date.toString());//重新将时间对象设置到日期对象中now.setTime(date);//从当前日期对象是取出当前月份、日期int today =now.get(Calendar.DAY_OF_MONTH);int month = now.get(Calendar.MONTH);//获取本月开始日期now.set(Calendar.DAY_OF_MONTH, 1);//获取本月开始日期在一周中的编号int week = now.get(Calendar.DAY_OF_WEEK);//打印日历头并换行设置当前月中第一天的开始位置System.out.println("星期日星期一星期二星期三星期四星期五星期六");//设置当前月中第一天的开始位置for( int i = Calendar.SUNDAY; i < week; i++){ System.out.print(" ");//按规格打印当前月的日期数字while(now.get(Calendar.MONTH) ==month){//取出当前日期int day =now.get(Calendar.DAY_OF_MONTH);//设置日期数字小于10与不小于10两种情况的打印规格if(day < 10){//设置当前日期的表现形式if(day == today)System.out.print(" <" + day + "> ");elseSystem.out.print(" " + day + " ");}else{//设置当前日期的表现形式if(day == today)System.out.print(" <" + day + "> ");elseSystem.out.print(" " + day + " ");}//设置什么时候换行if(week == Calendar.SATURDAY)System.out.println();//设置日期与星期几为下一天now.add(Calendar.DAY_OF_MONTH, 1);week = now.get(Calendar.DAY_OF_WEEK);}}}}。
java日历编写代码
java⽇历编写代码 //Jcalendar.javaimport java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.GridLayout;import java.awt.SystemColor;import java.awt.event.ActionEvent;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;import java.util.Calendar;import java.util.GregorianCalendar;import java.util.Locale;import java.util.Date;import java.util.StringTokenizer;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.JToggleButton;import javax.swing.SwingConstants;import javax.swing.UIManager;/*** <p>Title: Swing⽇历</p>* <p>Description: 操作⽇期</p>* @author duxu2004* @version 1.0.1*/class JCalendar extends JPanel{//动态表⽰年⽉⽇private int year=0;private int month=0;private int day=0;//主⾯板private JPanel Main = new JPanel();//⽇⾯板private JPanel jPanelDay = new JPanel();//⽉⾯板private JPanel jPanelMonth = new JPanel();//年的输⼊位置private JTextField Year = new JTextField();//⽉的输⼊位置private JTextField Month = new JTextField();//减少⽉份private JButton MonthDown = new JButton();//增加⽉份private JButton MonthUp = new JButton();private JPanel jPanelButton = new JPanel();//减少年份private JButton YearDown = new JButton();//增加年份private JButton YearUp = new JButton();//显⽰⽇期的位置private JLabel Out = new JLabel();//中国时区,以后可以从这⾥扩展可以设置时区的功能private Locale l=Locale.CHINESE;//主⽇历private GregorianCalendar cal=new GregorianCalendar(l);//星期⾯板private JPanel weekPanel=new JPanel();//天按钮组private JToggleButton[] days=new JToggleButton[42];//天⾯板private JPanel Days = new JPanel();//标⽰private JLabel jLabel1 = new JLabel();private JLabel jLabel2 = new JLabel();private JLabel jLabel3 = new JLabel();private JLabel jLabel4 = new JLabel();private JLabel jLabel5 = new JLabel();private JLabel jLabel6 = new JLabel();private JLabel jLabel7 = new JLabel();//当前选择的天数按钮private JToggleButton cur=null;//⽉份天数数组,⽤来取得当⽉有多少天// 1 2 3 4 5 6 7 8 9 10 11 12private int[] mm={31,28,31,30,31,30,31,31,30,31,30,31}; //空⽇期构造函数public JCalendar() {try {jbInit();}catch(Exception e) {e.printStackTrace();}}//带⽇期设置的构造函数public JCalendar(int year, int month, int day) {cal.set(year, month, day);try {jbInit();}catch (Exception e) {e.printStackTrace();}}//带⽇历输⼊的构造函数public JCalendar(GregorianCalendar calendar) {cal=calendar;try {jbInit();}catch (Exception e) {e.printStackTrace();}}//带⽇期输⼊的构造函数public JCalendar(Date date) {cal.setTime(date);try {jbInit();}catch (Exception e) {e.printStackTrace();}}//初始化组件private void jbInit() throws Exception {//初始化年、⽉、⽇iniCalender();this.setLayout(new BorderLayout());this.setBorder(BorderFactory.createRaisedBevelBorder()); this.setMaximumSize(new Dimension(200, 200));this.setMinimumSize(new Dimension(200, 200));this.setPreferredSize(new Dimension(200, 200));Main.setLayout(new BorderLayout());Main.setBackground();Main.setBorder(null);Out.setBackground(Color.lightGray);Out.setHorizontalAlignment(SwingConstants.CENTER);Out.setMaximumSize(new Dimension(100, 19));Out.setMinimumSize(new Dimension(100, 19));Out.setPreferredSize(new Dimension(100, 19));jLabel1.setForeground(Color.red);jLabel1.setHorizontalAlignment(SwingConstants.CENTER);jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);jLabel1.setText("⽇");jLabel2.setForeground(Color.blue);jLabel2.setHorizontalAlignment(SwingConstants.CENTER);jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);jLabel2.setText("六");jLabel3.setHorizontalAlignment(SwingConstants.CENTER);jLabel3.setHorizontalTextPosition(SwingConstants.CENTER);jLabel3.setText("五");jLabel4.setHorizontalAlignment(SwingConstants.CENTER);jLabel4.setHorizontalTextPosition(SwingConstants.CENTER);jLabel4.setText("四");jLabel5.setHorizontalAlignment(SwingConstants.CENTER);jLabel5.setHorizontalTextPosition(SwingConstants.CENTER);jLabel5.setText("三");jLabel6.setBorder(null);jLabel6.setHorizontalAlignment(SwingConstants.CENTER);jLabel6.setHorizontalTextPosition(SwingConstants.CENTER);jLabel6.setText("⼆");jLabel7.setBackground(Color.lightGray);jLabel7.setForeground(Color.black);jLabel7.setBorder(null);jLabel7.setHorizontalAlignment(SwingConstants.CENTER);jLabel7.setHorizontalTextPosition(SwingConstants.CENTER);jLabel7.setText("⼀");weekPanel.setBackground(UIManager.getColor("InternalFrame.activeTitleGradient")); weekPanel.setBorder(BorderFactory.createEtchedBorder());weekPanel.setLayout(new GridLayout(1,7));weekPanel.add(jLabel1, null);weekPanel.add(jLabel7, null);weekPanel.add(jLabel6, null);weekPanel.add(jLabel5, null);weekPanel.add(jLabel4, null);weekPanel.add(jLabel3, null);weekPanel.add(jLabel2, null);MonthUp.setAlignmentX((float) 0.0);MonthUp.setActionMap(null);jPanelMonth.setBackground();jPanelMonth.setLayout(new BorderLayout());jPanelMonth.setBorder(BorderFactory.createEtchedBorder());Month.setBorder(null);Month.setHorizontalAlignment(SwingConstants.CENTER);Month.addMouseListener(new java.awt.event.MouseAdapter() {public void mouseClicked(MouseEvent e) {Month_mouseClicked(e);}});Month.addKeyListener(new java.awt.event.KeyAdapter() {public void keyPressed(KeyEvent e) {Month_keyPressed(e);}});MonthDown.setBorder(null);MonthDown.setText("\u25C4");MonthDown.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(ActionEvent e) {MonthDown_actionPerformed(e);}});MonthUp.setBorder(null);MonthUp.setText("\u25BA");MonthUp.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(ActionEvent e) {MonthUp_actionPerformed(e);}});jPanelButton.setLayout(null);jPanelButton.setBorder(null);jPanelButton.addComponentListener(new ponentAdapter() { public void componentResized(ponentEvent evt) { jPanelButtonComponentResized(evt);}});Year.setBorder(BorderFactory.createEtchedBorder());Year.setMaximumSize(new Dimension(80, 25));Year.setMinimumSize(new Dimension(80, 25));Year.setPreferredSize(new Dimension(80, 25));Year.setHorizontalAlignment(SwingConstants.CENTER);Year.addMouseListener(new java.awt.event.MouseAdapter() {public void mouseClicked(MouseEvent e) {Year_mouseClicked(e);}});Year.addKeyListener(new java.awt.event.KeyAdapter() {public void keyPressed(KeyEvent e) {Year_keyPressed(e);}});YearDown.setBorder(null);YearDown.setMaximumSize(new Dimension(16, 16));YearDown.setMinimumSize(new Dimension(16, 16));YearDown.setPreferredSize(new Dimension(16, 16));YearDown.setSize(new Dimension(16, 16));YearDown.setText("▼");YearDown.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(ActionEvent e) {YearDown_actionPerformed(e);}});YearUp.setBorder(null);YearUp.setMaximumSize(new Dimension(16, 16));YearUp.setMinimumSize(new Dimension(16, 16));YearUp.setPreferredSize(new Dimension(16, 16));YearUp.setSize(new Dimension(16, 16));YearUp.setText("▲");YearUp.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(ActionEvent e) {YearUp_actionPerformed(e);}});jPanelDay.setLayout(new BorderLayout());Days.setLayout(new GridLayout(6,7));Days.setBackground();for(int i=0;i<42;i++){days[i]=new JToggleButton();days[i].setBorder(null);days[i].setBackground();days[i].setHorizontalAlignment(SwingConstants.CENTER);days[i].setHorizontalTextPosition(SwingConstants.CENTER);//days[i].setSize(l,l);days[i].addActionListener(new java.awt.event.ActionListener(){public void actionPerformed(ActionEvent e) {day=Integer.parseInt(((JToggleButton)e.getSource()).getText());showDate();showDays();}});Days.add(days[i]);}this.add(Main, BorderLayout.NORTH);this.add(jPanelDay, BorderLayout.CENTER);this.add(jPanelMonth, BorderLayout.SOUTH);Main.add(Year, BorderLayout.CENTER);Main.add(Out, BorderLayout.WEST);Main.add(jPanelButton, BorderLayout.EAST);jPanelButton.add(YearUp);jPanelButton.add(YearDown);jPanelDay.add(weekPanel,BorderLayout.NORTH);jPanelDay.add(Days, BorderLayout.CENTER);jPanelMonth.add(Month, BorderLayout.CENTER);jPanelMonth.add(MonthDown, BorderLayout.WEST);jPanelMonth.add(MonthUp, BorderLayout.EAST);showMonth();showYear();showDate();showDays();}//⾃定义重画年选择⾯板void jPanelButtonComponentResized(ponentEvent evt){YearUp.setLocation(0,0);YearDown.setLocation(0,YearUp.getHeight());jPanelButton.setSize(YearUp.getWidth(),YearUp.getHeight()*2);jPanelButton.setPreferredSize(new Dimension(YearUp.getWidth(),YearUp.getHeight()*2)); jPanelButton.updateUI();}//测试⽤public static void main(String[] args){JFrame f=new JFrame();f.setContentPane(new JCalendar());f.pack();//f.setResizable(false);f.show();}//增加年份void YearUp_actionPerformed(ActionEvent e) {year++;showYear();showDate();showDays();}//减少年份void YearDown_actionPerformed(ActionEvent e) {year--;showYear();showDate();showDays();}//减少⽉份void MonthDown_actionPerformed(ActionEvent e) {month--;if(month<0) {month = 11;year--;showYear();}showMonth();showDate();showDays();}//增加⽉份void MonthUp_actionPerformed(ActionEvent e) {month++;if(month==12) {month=0;year++;showYear();}showMonth();showDate();showDays();}//初始化年⽉⽇void iniCalender(){year=cal.get(Calendar.YEAR);month=cal.get(Calendar.MONTH);day=cal.get(Calendar.DAY_OF_MONTH);}//刷新⽉份void showMonth(){Month.setText(Integer.toString(month+1)+"⽉");}//刷新年份void showYear(){Year.setText(Integer.toString(year)+"年");}//刷新⽇期void showDate(){Out.setText(Integer.toString(year)+"-"+Integer.toString(month+1)+"-"+Integer.toString(day)); }//重画天数选择⾯板void showDays() {cal.set(year,month,1);int firstDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);int n=mm[month];if(cal.isLeapYear(year)&&month==1) n++;int i=0;for(;i<firstDayOfWeek-1;i++){days[i].setEnabled(false);days[i].setSelected(false);days[i].setText("");}int d=1;for(;d<=n;d++){days[i].setText(Integer.toString(d));days[i].setEnabled(true);if(d==day) days[i].setSelected(true);else days[i].setSelected(false);;i++;}for(;i<42;i++){days[i].setEnabled(false);days[i].setSelected(false);days[i].setText("");}}//单击年份⾯板选择整个年份字符串void SelectionYear(){Year.setSelectionStart(0);Year.setSelectionEnd(Year.getText().length());}//单击⽉份⾯板选择整个⽉份字符串void SelectionMonth(){Month.setSelectionStart(0);Month.setSelectionEnd(Month.getText().length());}//⽉份⾯板响应⿏标单击事件void Month_mouseClicked(MouseEvent e) {//SelectionMonth();inputMonth();}//检验输⼊的⽉份void inputMonth(){String s;if(Month.getText().endsWith("⽉")){s=Month.getText().substring(0,Month.getText().length()-1); }else s=Month.getText();month=Integer.parseInt(s)-1;this.showMe();}//⽉份⾯板键盘敲击事件响应void Month_keyPressed(KeyEvent e) {if(e.getKeyChar()==10)inputMonth();}//年份⾯板响应⿏标单击事件void Year_mouseClicked(MouseEvent e) {//SelectionYear();inputYear();}//年份键盘敲击事件响应void Year_keyPressed(KeyEvent e) {//System.out.print(new Integer(e.getKeyChar()).byteValue()); if(e.getKeyChar()==10)inputYear();}//检验输⼊的年份字符串void inputYear() {String s;if(Year.getText().endsWith("年")){s=Year.getText().substring(0,Year.getText().length()-1); }else s=Year.getText();year=Integer.parseInt(s);this.showMe();}//以字符串形式返回⽇期,yyyy-mm-ddpublic String getDate(){return Out.getText();}//以字符串形式输⼊⽇期,yyyy-mm-ddpublic void setDate(String date){if(date!=null){StringTokenizer f = new StringTokenizer(date, "-");if(f.hasMoreTokens())year = Integer.parseInt(f.nextToken());if(f.hasMoreTokens())month = Integer.parseInt(f.nextToken());if(f.hasMoreTokens())day = Integer.parseInt(f.nextToken());cal.set(year,month,day);}this.showMe();}//以⽇期对象形式输⼊⽇期public void setTime(Date date){cal.setTime(date);this.iniCalender();this.showMe();}//返回⽇期对象public Date getTime(){return cal.getTime();}//返回当前的⽇public int getDay() {return day;}//设置当前的⽇public void setDay(int day) {this.day = day;cal.set(this.year,this.month,this.day);this.showMe();}//设置当前的年public void setYear(int year) {this.year = year;cal.set(this.year,this.month,this.day);this.showMe();}//返回当前的年public int getYear() {return year;}//返回当前的⽉public int getMonth() {return month;}//设置当前的⽉public void setMonth(int month) {this.month = month;cal.set(this.year,this.month,this.day); this.showMe();}//刷新public void showMe(){this.showDays();this.showMonth();this.showYear();this.showDate();}}测试代码JFrame f=new JFrame();f.setContentPane(new JCalendar());f.pack();//f.setResizable(false);// f.show();f.setVisible(true);。
一个java Swing做的日历控件
tbCalendar .getTableHeader().setResizingAllowed(false);
/////设置列大小不可改变
nowDate=new JLabel();
nowDate.setText("今天是:"+gc.get(Calendar.YEAR)+"年"+
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
if(e.getKeyCode()==KeyEvent.VK_ENTER)
btnYearDown=new JButton("▼");
//年份减按钮
btnYearDown.setFont(font);
btnYearDown.setMargin(new Insets(0,0,0,0));
btnYearUp.setBounds(40, 6, 20, 10);
//按钮定位
btnYearDown.setBounds(40, 16, 20, 10);
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
isSelected, boolean hasFocus,
int row, int column) {
Object o=tbCalendar.getValueAt(row,
日历应用使用ReactNative和JavaScript开发的小程序
日历应用使用ReactNative和JavaScript开发的小程序日历是我们日常生活中必不可少的工具之一,随着技术的发展,手机日历应用成为我们管理时间的重要助手。
本文将介绍一个使用ReactNative和JavaScript开发的小程序,该小程序能够提供完善的日历功能,满足用户对时间管理的需求。
一、介绍在移动应用开发中,ReactNative是一种流行的开发框架,它基于JavaScript语言,可以同时为iOS和Android平台开发应用。
结合ReactNative和JavaScript的特性,我们可以开发出高效、易用的日历应用。
二、开发环境搭建在开始开发日历应用之前,我们需要准备好开发环境。
首先,安装Node.js以及npm包管理器。
接着,通过命令行安装ReactNative命令行工具。
然后,创建一个新的ReactNative项目,并安装所需的依赖。
三、设计与布局在日历应用中,设计和布局是非常重要的。
我们需要考虑用户界面的美观性和易用性。
通过合理的布局和配色方案,我们可以提供给用户一种舒适的使用体验。
四、日历视图日历视图是日历应用的核心组件,它显示了用户所选择的日期的详细信息。
我们可以使用ReactNative提供的组件来构建日历视图,比如ScrollView、View和Text等。
五、日期选择日期选择是日历应用的一个重要功能,它允许用户选择一个特定的日期,并查看该日期的详细信息。
我们可以使用ReactNative提供的DatePicker组件来实现日期选择功能。
六、事件提醒除了基本的日期管理功能,日历应用还应该提供事件提醒功能,让用户可以添加和管理个人的日程安排。
我们可以使用ReactNative提供的Alert组件来实现事件提醒功能。
七、数据存储为了保证用户数据的安全性和可靠性,我们需要将用户的日历数据进行存储。
可以使用ReactNative提供的AsyncStorage组件来实现数据的本地存储功能。
java获取年月日的方法
java获取年月日的方法Java是一种面向对象的编程语言,广泛应用于软件开发领域。
在Java中,获取年月日是一项常见的操作,可以通过多种方法实现。
下面,我将为大家介绍一些常用的获取年月日的方法,并解释它们的使用方式。
一、使用Java内置的日期类Java提供了多个日期类,其中最常用的是`Date`和`Calendar`类。
这两个类都可以用于获取年月日,但它们的使用方式有所不同。
1. `Date`类`Date`类是`java.util`包中的一个类,可以表示一个特定的时间点。
要获取当前的年月日,可以使用`Date`类的`getYear()`、`getMonth()`和`getDate()`方法。
需要注意的是,`getYear()`方法返回的年份是相对于1900的偏移量,所以需要将其加上1900才能得到真实的年份。
```javaDate currentDate = new Date();int year = currentDate.getYear() + 1900;int month = currentDate.getMonth() + 1;int day = currentDate.getDate();System.out.println("当前日期:" + year + "年" + month + "月" + day + "日");```2. `Calendar`类`Calendar`类是`java.util`包中的一个抽象类,它提供了丰富的日期操作方法。
与`Date`类相比,`Calendar`类更常用,因为它可以处理更复杂的日期计算。
要获取当前的年月日,可以使用`Calendar`类的`get()`方法,并传入相应的`Calendar`字段参数,如`Calendar.YEAR`、`Calendar.MONTH`和`Calendar.DAY_OF_MONTH`。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
java日历小程序
Java是一种广泛使用的编程语言,具有跨平台性和易于学习的特点。
在这篇文章中,我将介绍一个使用Java编写的日历小程序,该程序可以匡助用户查看任意年份和月份的日历。
1. 程序概述
这个日历小程序的主要功能是根据用户输入的年份和月份,生成对应月份的日历。
用户可以通过命令行输入所需的年份和月份,程序将输出一个按照星期罗列的日历表格。
2. 实现思路
为了实现这个日历小程序,我们需要使用Java提供的日期和时间相关的类。
Java提供了一个Calendar类,可以用于处理日期和时间。
我们可以使用Calendar 类中的方法来获取当前日期,以及计算指定日期的星期等信息。
程序的主要逻辑如下:
- 获取用户输入的年份和月份。
- 创建一个Calendar对象,并设置为用户输入的年份和月份的第一天。
- 获取该月的天数,并计算该月的第一天是星期几。
- 输出日历表格,按照星期罗列。
3. 代码实现
下面是一个简单的Java代码示例,用于实现上述日历小程序的功能:
```java
import java.util.Calendar;
public class CalendarProgram {
public static void main(String[] args) {
// 获取用户输入的年份和月份
int year = Integer.parseInt(args[0]);
int month = Integer.parseInt(args[1]);
// 创建一个Calendar对象,并设置为用户输入的年份和月份的第一天
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
// 获取该月的天数,并计算该月的第一天是星期几
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
// 输出日历表格
System.out.println("日一二三四五六");
for (int i = 1; i < firstDayOfWeek; i++) {
System.out.print(" ");
}
for (int i = 1; i <= daysInMonth; i++) {
System.out.printf("%2d ", i);
if ((i + firstDayOfWeek - 1) % 7 == 0) {
System.out.println();
}
}
}
}
```
4. 运行结果
以下是一个示例运行结果,用户输入年份为2022,月份为3(即3月份):
```
日一二三四五六
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
```
5. 总结
通过这个简单的Java日历小程序,我们可以看到Java语言的灵便性和易用性。
使用Java的日期和时间相关的类,我们可以轻松地实现各种日期和时间的计算和
处理。
希翼这个小程序对于学习Java编程和理解日期时间处理有所匡助。