网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

打印日历代码java 用java做出日历表的代码

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做日历,要求源代码

import java.util.Scanner; public class Wan{ public static void main(String[] args){ Scanner name = new Scanner(System.in); System.out.print("漏磨请输入要如慧查询的年份:"); int year = name.nextInt(); System.out.print("请输入该年的月份"); int month = name.nextInt(); } //累加 该年至输入的月份 天数 //比如 输入2009年的 3月分 // 那就累加 2009年的1月至 3月1号的总天数 public void sumDay(int year,int month){ int day = 0; int sumDay = 0; for(int i = 1;i=month;i++){ switch(i){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: day = 31; break; case 2: 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(int i = 2000;iyear;i++){ if( i % 4 == 0 || i %400== 0 i % 100 != 0){ sumDay += 366; }else{ sumDay += 365; } } /返橡斗/求该月一号为星期几 int week = sumDay % 7 +1; if(week == 7){ week = 0; } } public void fomatDate(int week,int day){ int g = 0; for(int i = 0;iweek;i++){ System.out.print("\t"); } for(int i = 1;i=day;i++){ System.out.print(i+"\t"); g = week + i; if(g % 7 == 0){ 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 用java做出日历表的代码
文章链接:http://bjjierui.cn/article/ddpepes.html

其他资讯