There is a java class in which the elements of their time and date are recorded in the map. Something like this:

new Map <String,String>() .put("time1","2015-01-25T03:01:00") 

We need a method that will find the old date (before (now)), and change it to a new one (literally 2.3 days ahead)

I will welcome any advice.

At first I tried to just download the file and change the data: (there is a lot of unnecessary in the code, but it works)

  import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; import java.util.ArrayList; import java.util.List; import java.io.BufferedWriter; public class FileReplace { List<String> lines = new ArrayList<String>(); String line = null; public void doIt() { try { File f1 = new File("TFile.java"); FileReader fr = new FileReader(f1); BufferedReader br = new BufferedReader(fr); while ((line = br.readLine()) != null) { if (line.contains("2015-01-25T03:01:00")) line = line.replace("2015-01-25T03:01:00", "2016-01-06T08:01:00 "); lines.add(line); } fr.close(); br.close(); FileWriter fw = new FileWriter(f1); BufferedWriter out = new BufferedWriter(fw); for(String s : lines) out.write(s); out.flush(); out.close(); } catch (Exception ex) { ex.printStackTrace(); } } public static void main(String args[]) { FileReplace fr = new FileReplace(); fr.doIt(); } } 

Then I think like comparing dates, something like this: (only sketches)

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); Date date1 = sdf; // Date date2 = ; data from the file if (date1.after(date2)) { date2 = data2.replace() // some random future data } 

I can not reduce everything to a heap, and I still need to get the time and date from the file, but as I wrote at the beginning, it’s written like this.

 new Map <String,String>() .put("time1","2015-01-25T03:01:00") 
  • Fatty will create a class for such trifles) - dirkgntly
  • Yes, and your classes never do anything, but the method ... is about them :) - dirkgntly

1 answer 1

need to get time and date from file

 import java.nio.file.*; import java.util.*; import java.io.*; public class Definition_Of_The_File_Creation_Date { public static void main(String[] args) { /* * ВмСсто / подстовляСм ΠΏΡƒΡ‚ΡŒ ΠΊ Ρ„Π°ΠΉΠ»Ρƒ ΠΈΠ»ΠΈ ΠΊΠΎΡ‚Π°Π»ΠΎΠ³Ρƒ, ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ статистичСский * ΠΌΠ΅Ρ‚ΠΎΠ΄ parsingFolder ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ "ΠΊΠ°Ρ€Ρ‚Ρƒ" Π·Π°ΠΏΠΎΠ»Π½Π΅Π½ΡƒΡŽ ΠΊΠ»ΡŽΡ‡Ρ‘ΠΌ - имя Ρ„Π°ΠΉΠ»Π° (Π‘Ρ‚Ρ€ΠΎΠΊΠ°) ΠΈ * Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ΠΌ Π΄Π°Ρ‚Ρ‹ послСднСго измСнСния Ρ„Π°ΠΉΠ»Π° (строка) */ Map<String, String> colection = parsingFolder("/"); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ всС ΠΊΠ»ΡŽΡ‡ΠΈ нашСй ΠΊΠΎΠ»Π΅ΠΊΡ†ΠΈΠΈ Set<String> nameFile = colection.keySet(); // Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ имя Ρ„Π°ΠΉΠ»Π° ΠΈ Π΅Π³ΠΎ Π΄Π°Ρ‚Ρƒ for(String name : nameFile){ System.out.println("Имя: " + name + " DATA: " + colection.get(name)); } /* * МоТно ΠΏΠΎΠΌΠ΅Π½ΡΡ‚ΡŒ всС ΠΊΠ»ΡŽΡ‡ΠΈ ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠΌ colection.replace("ΠΊΠ»ΡŽΡ‡", "НовоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅"), * Π’Π°ΠΊΠΆΠ΅ строку ΠΌΠΎΠΆΠ½ΠΎ Ρ€Π°Π·Π±ΠΈΡ‚ΡŒ java.util.StringTokenizer token = new StringTokenizer(data, "-T:Z") * ΠΈ соотвСтствСнно Π·Π°ΠΏΠΈΡΠ°Ρ‚ΡŒ Π΅Ρ‘ Ρ‡Π΅Ρ€Π΅Π· colection.replace("ΠΊΠ»ΡŽΡ‡", "НовоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅"), */ for(String name : nameFile){ colection.replace(name, "2016-01-06T08:01:00"); System.out.println("Имя: " + name + " DATA: " + colection.get(name)); } } public static TreeMap<String, String> parsingFolder(String nameFolder) { // Π‘ΠΎΠ·Π΄Π°Ρ‘ΠΌ ΠΊΠΎΠ»Π΅ΠΊΡ†ΠΈΡŽ, для Ρ€Π΅Π°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ сохранСния Π΄Π°Π½Π½Ρ‹Ρ… TreeMap<String, String> temp = new TreeMap<String, String>() {}; // устанавливаСм ΠΏΡƒΡ‚ΡŒ ΠΊ Ρ„Π°ΠΉΠ»Ρƒ, ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Ρƒ Path pathFile = Paths.get(nameFolder); // опрСдСляСм являСтся Π»ΠΈ ΠΏΡƒΡ‚ΡŒ (nameFolder) ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³ΠΎΠΌ ΠΈΠ»ΠΈ Ρ„Π°ΠΉΠ»ΠΎΠΌ if(Files.isDirectory(pathFile, LinkOption.NOFOLLOW_LINKS)) { /* * ΠΏΠ΅Ρ€Π΅Π±ΠΈΡ€Π°Π΅ΠΌ ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ Ρ„Π°ΠΉΠ» Π² ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Π΅ (nameFolder) ΠΈ записываСм * ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½Ρ‹Π΅ Π΄Π°Π½Π½Ρ‹Π΅ Π² ΠΊΠΎΠ»Π΅ΠΊΡ†ΠΈΡŽ */ try( DirectoryStream<Path> buffer = Files.newDirectoryStream(pathFile) ){ Iterator<Path> iterratorDirectory = buffer.iterator(); while(iterratorDirectory.hasNext()) { Path fileDirectory = iterratorDirectory.next(); /* * записали имя Ρ„Π°ΠΉΠ»Π° ΠΈ Π΄Π°Ρ‚Ρƒ послСднСй ΠΌΠΎΠ΄ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ Ρ„Π°ΠΉΠ»Π° Π² ΠΊΠΎΠ»Π΅ΠΊΡ†ΠΈΡŽ. * Files.getLastModifiedTime - являСтся Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚ΠΎΠΌ Ρ„Π°ΠΉΠ»Π°, соотвСствСнно вмСсто ΠΌΠΎΠΆΠ½ΠΎ * Π·Π°ΠΏΠΈΡΠ°Ρ‚ΡŒ ΠΈ ΠΈΠ½Ρ‹Π΅ Π΄Π°Π½Π½Ρ‹Π΅ ΠΎ Ρ„Π°ΠΉΠ»Π΅. ΠŸΠΎΡΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ класса Files Ρ‚ΠΈΠΏΠ° ΠΌΠ΅Ρ‚ΠΎΠ΄ getFileAttrebuteView() * readAttributes(), getAttribute() Π² API java.. Ρ‚Π°ΠΌ Π΅ΡΡ‚ΡŒ всС... */ temp.put( fileDirectory.getFileName().toString(), Files.getLastModifiedTime(fileDirectory, LinkOption.NOFOLLOW_LINKS).toString() ); } }catch(SecurityException | IOException exSecuritu){ System.out.println("ERROR " + exSecuritu); } } else { /* * Π”Π°Π½Π½ΠΎΠ΅ условиС выполняСтся Ссли ΡƒΠΊΠ°Π·Π°Π½Π½Ρ‹ΠΉ ΠΏΡ€ΠΈ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ ΠΏΡƒΡ‚ΡŒ являССтся Ρ„Π°ΠΉΠ»ΠΎΠΌ */ try{ // записали имя Ρ„Π°ΠΉΠ»Π° ΠΈ Π΄Π°Ρ‚Ρƒ ΠΌΠΎΠ΄ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ Ρ„Π°ΠΉΠ»Π° Π² ΠΊΠΎΠ»Π΅ΠΊΡ†ΠΈΡŽ temp.put( pathFile.getFileName().toString(), Files.getLastModifiedTime(pathFile, LinkOption.NOFOLLOW_LINKS).toString() ); }catch(IOException ioE) { System.out.println("ERROR " + ioE); } } return temp; } } 

Then I just do not understand what you want? Why change the date in the * .java file when creating a Map.? If you need to change the date of the file itself, this is one question, the date in the Map collection is another question, if you need to save the collection as a data file, then this is serialization and, accordingly, another question ....... Write in more detail please what you want . Well, either I have already answered your question :)