Parsing txt file with Holiday and displaying it with sorting

 
 
 
Сообщения:1
package holiday.com;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Program {
static TreeSet<Holiday> tr = new TreeSet<>();
	
	public static void main(String[] args) throws Exception {
		Program program =new Program();
		program.readFile();
		
		System.out.println("Is TreeSet empty?: "+tr.isEmpty());
		System.out.println("Size of TreeSet: " +tr.size());
//Creating static date variable
		String staticD = "2013/6/15";
		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		Date staticDate = df.parse(staticD);
// Get current date
		Date curDate = new Date();
		SimpleDateFormat curDf = new SimpleDateFormat("yyyy/MM/dd");
		String stringCurentDate = curDf.format(curDate);
		Date currentDate = curDf.parse(stringCurentDate);
		System.out.println("Today Date: "+stringCurentDate);
		System.out.println("Today holidays:");
		program.getHoliday(currentDate);
// Get tomorrow date
		Date tomDate=new Date();
		Calendar c = Calendar.getInstance(); 
		c.setTime(tomDate); 
		c.add(Calendar.DATE, 1);
		tomDate = c.getTime();
		SimpleDateFormat tomDf = new SimpleDateFormat("yyyy/MM/dd");
		String stringTomorrowDate = tomDf.format(tomDate);
		Date tomorowDate = tomDf.parse(stringTomorrowDate);
		System.out.println("Tomorrow Date: "+stringTomorrowDate);
		System.out.println("Tomorrow holidays:");
		program.getHoliday(tomorowDate);
		
	
	}

	public void readFile() throws Exception{
		//Reading the inpur file
				FileInputStream fis = new FileInputStream(new File("c:/holidays.txt"));
		//red file with encoding "windows-1251" Windows Cyrilic	
				InputStreamReader isr = new InputStreamReader(fis, "windows-1251");
				BufferedReader br = new BufferedReader(isr);
		//Initialize variable line
				String line = null;
		//Until .txt document have lines do...
				while ((line = br.readLine()) != null) {
		//Use pattern to split in 3 arrays( date, string 1, string 2)
					Pattern p = Pattern.compile("(.*?) (.*?) (\\(.*\\))");
					Matcher m = p.matcher(line);
					if (!m.matches())
						throw new Exception("Invalid string");
		//Read 1 array of line (it is Date)
					String s1 = m.group(1); // 2013/01/30
					DateFormat df = new SimpleDateFormat("yyyy/MM/dd"); //Format String into Date format
					Date date = df.parse(s1); //Format String into Date format
					String s2 = m.group(2); // name of holiday
					String s3 = m.group(3); // country of holiday
		//Use default constructor to keep info in object class Holiday 
					Holiday h = new Holiday(date, s2, s3);
		//Add Holiday object to TreeSet
					tr.add(h);
				}
				br.close();
	}
	
	public String getHoliday(Date date){
		String a=null;
		//Create iterator
				Iterator<Holiday> itr = tr.iterator();
				while (itr.hasNext()) { //while TreeSet have element do...

					Holiday s = itr.next();//Get next element of TreeSet
					Date d = s.date; //get date form [i] element of TreeSet
					String country = s.country;//get country form [i] element of TreeSet
					String name = s.name;//get name form [i] element of TreeSet
		

					if (d.compareTo(date) == 0) {
						
						System.out.println("		-" + name +""+ country);
							
				
					} 
				}
		
		
		return a;
	}

}
 
Модераторы:LuckysenleftgurookatAlisaTestPetraStillJulikigorss
Сейчас эту тему просматривают:Нет