求帮写一个java小程序
问题描述:
读取一个txt文件,文件中有一个8位的日期(任意),读出日期并打印此从日期开始的一个月日历,最好能有详细代码和注释
答
参考代码如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateCalendar {
public static void main(String[] args) {
printLatestMonth("src/date.txt");
}
public static void printLatestMonth(String filePath){
String fileContent = readFromFile(filePath,null);
if(fileContent==null||fileContent.length()<8){
System.out.println("日期格式不正确,请保证日期格式为yyyyMMdd");
return;
}
DateFormat format = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
try {
Date currentDate = format.parse(fileContent);
calendar.setTime(currentDate);
System.out.println("crurent date is:"+currentDate);
for(int i = 0;i<30;i++){
calendar.add(Calendar.DATE, 1);
System.out.println(calendar.getTime());
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String readFromFile(String fileName,String charSet){
if (fileName == null || "".equals(fileName)) {
System.out.println("文件名称为空...");
return "";
}
// 设置字符编码
if(charSet==null||charSet.length()==0){
charSet = "UTF-8";
}
File file = new File(fileName);
FileInputStream fin = null;
StringBuffer buffer = new StringBuffer();
try {
fin = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(
fin, charSet));
String content = reader.readLine();
while (content != null) {
buffer.append(content);
// 循环读取下一行,没有这行代码就是死循环的啦。。。
content = reader.readLine();
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return buffer.toString();
}
}
src目录下文件格式:20160310
答
读txt可以 用BufferedReader或scanner,日期操作,用calender,你把这几个搞明白就行了
答
有C币没?这个问题的花费至少半小时,半小时的机会成本你如果能买单的话,就帮你写了。
答
读取文件的时间,再类型转换成时间类型.获得那个月的天数,以及那个月的第一天是星期几.在就是打印出来. 按照流程去API找那些方法就可以做出来了
答
这个问题还好啊。不是特别复杂,自己去写下吧,当做锻炼,。
大概思路:
1.使用InputStream读取到内存中,获取到日期;
2.使用日期格式化,格式化成Date;
3.然后用Calendar生成开始的下一月的数据即可。
答
获取F:/时间.txt的时间,打印出年月日。
public class DateTest {
/**
* @param args
*/
public static void main(String[] args) {
String dateString = fileInputStreamFile("F:/时间.txt");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = sdf.parse(dateString);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println("当前年为:" + calendar.get(Calendar.YEAR));
System.out.println("当前月为:" + (calendar.get(Calendar.MONTH)+1));//月份是从0月开始算的,所以要加1
System.out.println("当前日为:" + calendar.get(Calendar.DATE));
} catch (ParseException e) {
e.printStackTrace();
}
}
public static String fileInputStreamFile(String path) {
File file = new File(path);
if (!file.exists()) {
return "-1";
}
String text = "";
try {
FileInputStream in = new FileInputStream(file);
byte[] inbytes = new byte[1024];
int buffRead = 0;
while ((buffRead = in.read(inbytes)) != -1) {
/**
* 每次把读取文件的大小为1024byte,存入缓冲流中,在真正的读取是读取这个缓冲流,也就是这个数组,读取的长度为0,
* 到实际复制的长度buffRead ,因为最后的长度基本上不太可能是准确的1024byte,往往会小于1024byte,
* 所以不能直接读取1024byte数组 ,而要用buffRead
*/
text = new String(inbytes, 0, buffRead);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return "0";
}
return text;
}