「打印日历java」打印日历表

博主:adminadmin 2023-01-01 01:51:09 817

今天给各位分享打印日历java的知识,其中也会对打印日历表进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

输入年份月份,打印输出当月日历 用java

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class java {

public static void main(String[] args) throws IOException {

BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));

System.out.println("请输入年份:");

String s1=buf.readLine();

System.out.println("请输入月份: ");

String s2=buf.readLine();

int year=Integer.parseInt(s1);

int month=Integer.parseInt(s2);

switch(month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 13:

System.out.println("在"+year+"年的"+month+"月有:31天");

break;

case 4:

case 6:

case 9:

case 11:

System.out.println("在"+year+"年的"+month+"月有:30天");

break;

case 2:

if(year%4==0year%100!=0||year%400==0)

{

System.out.println("在"+year+"年的"+month+"月有:29天");

}

else

{

System.out.println("在"+year+"年的"+month+"月有:28天");

}

break;

}

}

}

用JAVA打印日历表!求高手指导!

//加分吧,测试通过的。

package test;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.table.*;

public class MyCalendar extends JApplet {

public static final String WEEK_SUN = "SUN";

public static final String WEEK_MON = "MON";

public static final String WEEK_TUE = "TUE";

public static final String WEEK_WED = "WED";

public static final String WEEK_THU = "THU";

public static final String WEEK_FRI = "FRI";

public static final String WEEK_SAT = "SAT";

public static final Color background = Color.white;

public static final Color foreground = Color.black;

public static final Color headerBackground = Color.blue;

public static final Color headerForeground = Color.white;

public static final Color selectedBackground = Color.blue;

public static final Color selectedForeground = Color.white;

private JPanel cPane;

private JLabel yearsLabel;

private JSpinner yearsSpinner;

private JLabel monthsLabel;

private JComboBox monthsComboBox;

private JTable daysTable;

private AbstractTableModel daysModel;

private Calendar calendar;

public MyCalendar() {

cPane = (JPanel) getContentPane();

}

public void init() {

cPane.setLayout(new BorderLayout());

calendar = Calendar.getInstance();

//calendar = Calendar.getInstance();

yearsLabel = new JLabel("Year: ");

yearsSpinner = new JSpinner();

yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner, "0000"));

yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));

yearsSpinner.addChangeListener(new ChangeListener() {

public void stateChanged(ChangeEvent changeEvent) {

int day = calendar.get(Calendar.DAY_OF_MONTH);

calendar.set(Calendar.DAY_OF_MONTH, 1);

calendar.set(Calendar.YEAR, ((Integer) yearsSpinner.getValue()).intValue());

int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

calendar.set(Calendar.DAY_OF_MONTH, day maxDay ? maxDay : day);

updateView();

}

});

JPanel yearMonthPanel = new JPanel();

cPane.add(yearMonthPanel, BorderLayout.NORTH);

yearMonthPanel.setLayout(new BorderLayout());

yearMonthPanel.add(new JPanel(), BorderLayout.CENTER);

JPanel yearPanel = new JPanel();

yearMonthPanel.add(yearPanel, BorderLayout.WEST);

yearPanel.setLayout(new BorderLayout());

yearPanel.add(yearsLabel, BorderLayout.WEST);

yearPanel.add(yearsSpinner, BorderLayout.CENTER);

monthsLabel = new JLabel("Month: ");

monthsComboBox = new JComboBox();

for (int i = 1; i = 12; i++) {

monthsComboBox.addItem(new Integer(i));

}

monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));

monthsComboBox.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent actionEvent) {

int day = calendar.get(Calendar.DAY_OF_MONTH);

calendar.set(Calendar.DAY_OF_MONTH, 1);

calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex());

int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

calendar.set(Calendar.DAY_OF_MONTH, day maxDay ? maxDay : day);

updateView();

}

});

JPanel monthPanel = new JPanel();

yearMonthPanel.add(monthPanel, BorderLayout.EAST);

monthPanel.setLayout(new BorderLayout());

monthPanel.add(monthsLabel, BorderLayout.WEST);

monthPanel.add(monthsComboBox, BorderLayout.CENTER);

daysModel = new AbstractTableModel() {

public int getRowCount() {

return 7;

}

public int getColumnCount() {

return 7;

}

public Object getValueAt(int row, int column) {

if (row == 0) {

return getHeader(column);

}

row--;

Calendar calendar = (Calendar) MyCalendar.this.calendar.clone();

calendar.set(Calendar.DAY_OF_MONTH, 1);

int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) - 1;

int index = row * 7 + column;

int dayIndex = index - moreDayCount + 1;

if (index moreDayCount || dayIndex dayCount) {

return null;

} else {

return new Integer(dayIndex);

}

}

};

daysTable = new CalendarTable(daysModel, calendar);

daysTable.setCellSelectionEnabled(true);

daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

daysTable.setDefaultRenderer(daysTable.getColumnClass(0), new TableCellRenderer() {

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,

boolean hasFocus, int row, int column) {

String text = (value == null) ? "" : value.toString();

JLabel cell = new JLabel(text);

cell.setOpaque(true);

if (row == 0) {

cell.setForeground(headerForeground);

cell.setBackground(headerBackground);

} else {

if (isSelected) {

cell.setForeground(selectedForeground);

cell.setBackground(selectedBackground);

} else {

cell.setForeground(foreground);

cell.setBackground(background);

}

}

return cell;

}

});

updateView();

cPane.add(daysTable, BorderLayout.CENTER);

}

public static String getHeader(int index) {

switch (index) {

case 0:

return WEEK_SUN;

case 1:

return WEEK_MON;

case 2:

return WEEK_TUE;

case 3:

return WEEK_WED;

case 4:

return WEEK_THU;

case 5:

return WEEK_FRI;

case 6:

return WEEK_SAT;

default:

return null;

}

}

public void updateView() {

daysModel.fireTableDataChanged();

daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),

calendar.get(Calendar.WEEK_OF_MONTH));

daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_WEEK) - 1,

calendar.get(Calendar.DAY_OF_WEEK) - 1);

}

public static class CalendarTable extends JTable {

private Calendar calendar;

public CalendarTable(TableModel model, Calendar calendar) {

super(model);

this.calendar = calendar;

}

public void changeSelection(int row, int column, boolean toggle, boolean extend) {

super.changeSelection(row, column, toggle, extend);

if (row == 0) {

return;

}

Object obj = getValueAt(row, column);

if (obj != null) {

calendar.set(Calendar.DAY_OF_MONTH, ((Integer)obj).intValue());

}

}

}

public static void main(String[] args) {

JFrame frame = new JFrame("Calendar Application");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MyCalendar myCalendar = new MyCalendar();

myCalendar.init();

frame.getContentPane().add(myCalendar);

frame.setSize(240, 172);

frame.show();

}

}

Java日历打印,怎么加输入年份,打印整年12个月打印,输入月份打印季节,下面代码如何优化,给个思路?

建议封装函数,使用数组完成所有的打印

比如获取天数,getDays(int year,int month)

获取星期,getDay(int allDays)

获取极度,getQuater(int month)

然后打印重载写print方法,传入的flag不同打印年 嫉妒等

Java 打印两个月的日历

水平排列有点蛋疼,不过还是想到办法搞定了,希望你运行的时候排列不会有问题。请接招:

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Date;

import java.util.List;

import java.util.Scanner;

public class CalendarOutput {

private static Calendar cal1, cal2;

private static final String[] DAY_OF_WEEK = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

private static DateFormat df = new SimpleDateFormat("yyyy年MM月");

private static DateFormat parseDf = new SimpleDateFormat("yyyyMM");

private static Scanner sc;

public static void main(String[] args) {

cal1 = Calendar.getInstance();

cal1.set(Calendar.DATE, 1);

cal1.setFirstDayOfWeek(Calendar.SUNDAY);

cal2 = Calendar.getInstance();

cal2.set(Calendar.DATE, 1);

sc = new Scanner(System.in);

System.out.println("请输入年月(如201411):");

Date d = null;

while(null == d) {

try {

String line = sc.nextLine();

d = parseDf.parse(line);

} catch (ParseException e) {

System.out.println("日期格式不正确,请重新输入!");

d = null;

}

}

cal1.setTime(d);

cal2.set(Calendar.YEAR, cal1.get(Calendar.YEAR));

cal2.set(Calendar.MONTH, cal1.get(Calendar.MONTH));

cal2.add(Calendar.MONTH, 1);

printCal();

}

private static void printCal() {

// 先在最顶部显示年份

System.out.println(String.format("\t%s\t\t\t%s",

df.format(cal1.getTime()), df.format(cal2.getTime())));

printWeek(); // 然后输出星期日到星期一

// 先将两个月的日期保存到两个数组中

String[][] month1 = putMonth(cal1);

String[][] month2 = putMonth(cal2);

// 再将数组中的文字取出来并显示

for(int i = 0; i  month1.length; i ++) {

String[] month = month1[i];

// 逐周输出日期

for(int j = 0; j  month.length; j ++) {

System.out.print(month[j]);

}

// 如果第二个月周数不小于第一个月,则在本行再输出第二个月的日期

System.out.print("\t");

if(month2.length  i) {

for(int j = 0; j  month.length; j ++) {

System.out.print(month2[i][j]);

}

}

System.out.println();

}

// 如果第二个月周数大于第一个月,则再输出第二个月的日期(可用2月份检验)

if(month1.length  month2.length) {

int deltaMonth = month2.length - month1.length;

for(int i = 0; i  deltaMonth; i ++) {

for(int j = 0; j  7; j ++) {

System.out.print("    ");

}

System.out.print("\t");

for(int j = 0; j  7; j ++) {

System.out.print(month2[month1.length + i][j]);

}

}

}

}

private static String[][] putMonth(Calendar cal) {

ListListString listArray = new ArrayListListString();

int month = cal.get(Calendar.MONTH);

cal.set(Calendar.DATE, 1);

int startDayOfWeek1 = cal.get(Calendar.DAY_OF_WEEK);

ListString list = new ArrayListString();

// 先设置为本周属于上个月的几天,以空字符显示

cal.add(Calendar.DATE, -(startDayOfWeek1 - Calendar.SUNDAY) - 1);

for(int i = Calendar.SUNDAY; i  startDayOfWeek1; i ++) {

list.add("\t");

}

// 重新初始化为本月第一天

cal.set(Calendar.MONTH, month);

cal.set(Calendar.DATE, 1);

int maxDate = cal.getActualMaximum(Calendar.DATE); // 本月最多的天数(不用再去判断是否闰年了)

for(int j = 1; j = maxDate; j ++) {

list.add(String.format("%4s", j + ""));

if(list.size() == 7) {

ListString list2 = new ArrayListString(list);

listArray.add(list2); // 碰到星期六就换行

list = new ArrayListString();

} else if(j == maxDate) {

listArray.add(list); // 最后一天就将最后一周添加进来

}

}

String[][] monthDates = new String[listArray.size()][7];

for(int i = 0; i  listArray.size(); i ++) {

ListString item = listArray.get(i);

for(int j = 0; j  7; j ++) {

if(item.size()  j) { // 如果是最后一周的话可能存在下标越界

monthDates[i][j] = item.get(j);

} else { // 下标越界的时候设置为空字符串

monthDates[i][j] = "\t";

}

}

}

return monthDates;

}

private static void printWeek() {

for(int i = Calendar.SUNDAY; i = Calendar.SATURDAY; i ++) {

System.out.print(String.format("%4s", DAY_OF_WEEK[i - 1]));

}

System.out.print("\t");

for(int i = Calendar.SUNDAY; i = Calendar.SATURDAY; i ++) {

System.out.print(String.format("%4s", DAY_OF_WEEK[i - 1]));

}

System.out.println();

}

}

java中如何读取文件中内容,并打印日历

public class ReadFromFile {

/**

* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。

*/

public static void readFileByBytes(String fileName) {

File file = new File(fileName);

InputStream in = null;

try {

System.out.println("以字节为单位读取文件内容,一次读一个字节:");

// 一次读一个字节

in = new FileInputStream(file);

int tempbyte;

while ((tempbyte = in.read()) != -1) {

System.out.write(tempbyte);

}

in.close();

} catch (IOException e) {

e.printStackTrace();

return;

}

try {

System.out.println("以字节为单位读取文件内容,一次读多个字节:");

// 一次读多个字节

byte[] tempbytes = new byte[100];

int byteread = 0;

in = new FileInputStream(fileName);

ReadFromFile.showAvailableBytes(in);

// 读入多个字节到字节数组中,byteread为一次读入的字节数

while ((byteread = in.read(tempbytes)) != -1) {

System.out.write(tempbytes, 0, byteread);

}

} catch (Exception e1) {

e1.printStackTrace();

} finally {

if (in != null) {

try {

in.close();

} catch (IOException e1) {

}

}

}

}

/**

* 以字符为单位读取文件,常用于读文本,数字等类型的文件

*/

public static void readFileByChars(String fileName) {

File file = new File(fileName);

Reader reader = null;

try {

System.out.println("以字符为单位读取文件内容,一次读一个字节:");

// 一次读一个字符

reader = new InputStreamReader(new FileInputStream(file));

int tempchar;

while ((tempchar = reader.read()) != -1) {

// 对于windows下,\r\n这两个字符在一起时,表示一个换行。

// 但如果这两个字符分开显示时,会换两次行。

// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。

if (((char) tempchar) != '\r') {

System.out.print((char) tempchar);

}

}

reader.close();

} catch (Exception e) {

e.printStackTrace();

}

try {

System.out.println("以字符为单位读取文件内容,一次读多个字节:");

// 一次读多个字符

char[] tempchars = new char[30];

int charread = 0;

reader = new InputStreamReader(new FileInputStream(fileName));

// 读入多个字符到字符数组中,charread为一次读取字符数

while ((charread = reader.read(tempchars)) != -1) {

// 同样屏蔽掉\r不显示

if ((charread == tempchars.length)

(tempchars[tempchars.length - 1] != '\r')) {

System.out.print(tempchars);

} else {

for (int i = 0; i charread; i++) {

if (tempchars[i] == '\r') {

continue;

} else {

System.out.print(tempchars[i]);

}

}

}

}

} catch (Exception e1) {

e1.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e1) {

}

}

}

}

/**

* 以行为单位读取文件,常用于读面向行的格式化文件

*/

public static void readFileByLines(String fileName) {

File file = new File(fileName);

BufferedReader reader = null;

try {

System.out.println("以行为单位读取文件内容,一次读一整行:");

reader = new BufferedReader(new FileReader(file));

String tempString = null;

int line = 1;

// 一次读入一行,直到读入null为文件结束

while ((tempString = reader.readLine()) != null) {

// 显示行号

System.out.println("line " + line + ": " + tempString);

line++;

}

reader.close();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e1) {

}

}

}

}

/**

* 随机读取文件内容

*/

public static void readFileByRandomAccess(String fileName) {

RandomAccessFile randomFile = null;

try {

System.out.println("随机读取一段文件内容:");

// 打开一个随机访问文件流,按只读方式

randomFile = new RandomAccessFile(fileName, "r");

// 文件长度,字节数

long fileLength = randomFile.length();

// 读文件的起始位置

int beginIndex = (fileLength 4) ? 4 : 0;

// 将读文件的开始位置移到beginIndex位置。

randomFile.seek(beginIndex);

byte[] bytes = new byte[10];

int byteread = 0;

// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。

// 将一次读取的字节数赋给byteread

while ((byteread = randomFile.read(bytes)) != -1) {

System.out.write(bytes, 0, byteread);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if (randomFile != null) {

try {

randomFile.close();

} catch (IOException e1) {

}

}

}

}

/**

* 显示输入流中还剩的字节数

*/

private static void showAvailableBytes(InputStream in) {

try {

System.out.println("当前字节输入流中的字节数为:" + in.available());

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

String fileName = "C:/temp/newTemp.txt";

ReadFromFile.readFileByBytes(fileName);

ReadFromFile.readFileByChars(fileName);

ReadFromFile.readFileByLines(fileName);

ReadFromFile.readFileByRandomAccess(fileName);

}

}

关于打印日历java和打印日历表的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。