`
ynp
  • 浏览: 442234 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

一个面试题

阅读更多
package com.xx;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 一文件格式如下:
 * 12月27日
 * 12月29日
 * 12月26日-12月29日
 * 12月29日
 * 文件中有重复的日期,有日期段
 * 要求:把文件中所有的日期按顺序输出,不能有重复,包含的天数;
 */
public class DataOutPut {

	public static void main(String[] args) throws Exception {
		int slength = "12月27日".length();
		File file = new File("D:/报表.txt");
		
		List <Date>dateList = new ArrayList<Date>();
		for(String s : readFile(file)){
			//日期段的情况
			if(s.length() > slength ){
				String[]dates = DateSection(s);
				for(Date date : findDates(format(dates[0]),format(dates[1]))){
					putList(dateList,date);
				}
			}else{//日期的情况
				putList(dateList,format(s));
			}
		}
		System.out.println("共"+dateList.size()+"天");
		System.out.println("文件中的日期是:");
		//对日期集合排序
		Collections.sort(dateList);
		for(Date d : dateList){
			SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
			System.out.println(sdf.format(d));
		}
	}
	
	/**
	 * 读取文件
	 * @param file 要读取的文件
	 * @return list<String>
	 */
	public static List<String> readFile(File file){
		List<String>list = new ArrayList<String>();
		BufferedReader readfile = null;
		try {
		    readfile = new BufferedReader(new FileReader(file));
			String tempString = null;
			while((tempString = readfile.readLine()) != null){
				if(!list.contains(tempString))
					list.add(tempString);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(readfile != null){
				try {
					readfile.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return list;
	}

	/**
	 * 某时间段之间的日期
	 * @param dBegin 开始时间
	 * @param dEnd  结束时间
	 * @return List<Date>
	 * @throws Exception 
	 */
	public static List<Date> findDates(Date dBegin, Date dEnd) throws Exception {
		if (dBegin.after(dEnd)) {
			throw new Exception("开始时间不能大于结束时间");
		}
		List<Date> lDate = new ArrayList<Date>();
		lDate.add(dBegin);
		Calendar cal = Calendar.getInstance();
		cal.setTime(dBegin);
		while (dEnd.after(cal.getTime())) {
			cal.add(Calendar.DAY_OF_MONTH, 1);
			lDate.add(cal.getTime());
		}
		lDate.add(dEnd);
		return lDate;
	}
	
	/**
	 * 把日期加入到list中
	 * @param datas 存放date的list
	 * @param date 要放入list的date
	 */
	public static void putList(List <Date>datas,Date date){
		if(!datas.contains(date)){
			datas.add(date);
		}
	}
	
	/**
	 * 分离日期段
	 * @param DateSection
	 * @return 包含日期的数组
	 */
	public static String[] DateSection(String DateSection){
		return  DateSection.split("-");
	}
	
	/**
	 * 格式化函数 格式化成 MM月dd日
	 * @param s
	 * @return 格式化后的日期
	 */
	public static Date format(String s){
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
		Date date = null;
		try {
			date = sdf.parse(s);
		} catch (ParseException e1) {
			e1.printStackTrace();
		} 
		return date;
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics