如何在我的代码中追踪事件发生的时间?

huangapple go评论88阅读模式
英文:

How do i keep track of the time at which something happened in my code?

问题

public class BookStudentMerge {
    
     public static List<Row> getStudentInfo(Sheet sheet, DataFormatter formatter, FormulaEvaluator evaluator, String searchValue) {
          List<Row> result = new ArrayList<Row>();
          String cellValue = "";
          String searchV = searchValue.toLowerCase();
          for (Row row : sheet) {
           for (Cell cell : row) {
            cellValue = formatter.formatCellValue(cell, evaluator);
            String cellV = cellValue.toLowerCase();
            if (cellV.contains(searchV)) {
             result.add(row);
             break;
            }
           }
          }
          return result;
         }
     public static List<Row> bookSearch(Sheet sheet, DataFormatter formatter, FormulaEvaluator evaluator, String searchValue) {
         List<Row> none = new ArrayList<Row>();
         String searchM = searchValue.toLowerCase(); 
         List<Row> result = new ArrayList<Row>();
          String cellValue = "";
          for (Row row : sheet) {
           for (Cell cell : row) {
            cellValue = formatter.formatCellValue(cell, evaluator);
            String cellM = cellValue.toLowerCase();
            if (cellM.contains(searchM)) {
                result.add(row);
             break;
            }
           }
          }
          return result;
     }
     public static boolean checkAvailability (Row r, DataFormatter formatter, FormulaEvaluator evaluator) {
         String cellValue = "";
         for (Cell c : r) {
             cellValue = formatter.formatCellValue(c, evaluator);
             String cellM = cellValue.toLowerCase();
             if (cellM.equalsIgnoreCase("Available")) {
                 return true; 
         }
        }
         return false;
     }
     public static void changeStatus (Row r, DataFormatter formatter, FormulaEvaluator evaluator) {
         String cellValue = "";
         for (Cell c : r) {
             cellValue = formatter.formatCellValue(c, evaluator);
             String cellM = cellValue.toLowerCase();
         if (cellM.equalsIgnoreCase("Available")) {
             c.setCellValue("Unavailable");
         }
         else if (cellM.equalsIgnoreCase("Unavailable")) {
             c.setCellValue("Available"); 
         }
         }
     }

    public static void main(String[] args) throws IOException, InvalidFormatException {
        
        
        
        
        Workbook Books = WorkbookFactory.create(new FileInputStream("C:\\Users\\abdul\\Desktop\\University Files\\Object-Oriented Programming\\Project files\\Book class\\Books & DDC.xlsx"));
        Sheet SB = Books.getSheetAt(0);
        Workbook Students = WorkbookFactory.create(new FileInputStream("C:\\Users\\abdul\\Desktop\\University Files\\Object-Oriented Programming\\Project files\\Book class\\StudentsInfo.xlsx"));
        Sheet SS = Students.getSheetAt(0);
        DataFormatter FB = new DataFormatter();
        FormulaEvaluator EB =  Books.getCreationHelper().createFormulaEvaluator();        
        DataFormatter FS = new DataFormatter();
        FormulaEvaluator ES =  Students.getCreationHelper().createFormulaEvaluator();
        Scanner s = new Scanner (System.in);
              
        

             System.out.println("Welcome to our BiblioTheque library program!");
             System.out.println("Enter a valid student ID number: ");
             String inp = s.nextLine();
              Row headers = SS.getRow(0);
              for (Cell HS : headers) {
                  System.out.print(HS.getStringCellValue() + "\t\t  ");
              }
             System.out.println();
             List<Row> filteredStudents = getStudentInfo(SS, FS, ES, inp);
             for (Row row : filteredStudents) {
                   for (Cell cell : row) {
                       System.out.print(FS.formatCellValue(cell, ES));
                       System.out.print("\t     ");
                   }
                   System.out.println();
             }
             System.out.println();
             System.out.println("Search for a book by title, author, DDC, or ISBN: ");
             System.out.println("enter the name, author, ddc, isbn of your book: ");
             String search = s.nextLine();
              List<Row> filteredBooks = bookSearch(SB, FB, EB, search);
              Row HB = SB.getRow(0);
              for (Cell CB : HB) {
                  System.out.print(CB.getStringCellValue() + "\t\t\t");
              }
              System.out.println();
              for (Row row : filteredBooks) {
                  for (Cell cell : row) {
                System.out.print(FB.formatCellValue(cell, EB));
                System.out.print("\t\t");
               }
                  System.out.println();
              }
              if (filteredBooks.isEmpty()) {
                  System.out.println("no results found for the entered keyword/number");
              }
              else {
                  System.out.println("do you wish to borrow any of the displayed books?");
              }
              String ans = s.nextLine();
              if (ans.equalsIgnoreCase("yes")) {
                  System.out.println("choose the desired book by typing the item's list number: ");
              }
              int borrow = s.nextInt() - 1;
              Row Result = filteredBooks.get(borrow);
              if (checkAvailability(Result, FB, EB)) {
                  System.out.println("You are going to borrow the book " + Result.getCell(0) + ", please proceed to the help desk to complete the operation.");
                  changeStatus(Result, FB, EB);
              }
    }
}
英文:

i am making a program that reads input from a user, allows them to view the books available in my library (which is imported from an excel sheet), picking an item and choosing to borrow it, but the thing is that i want to put borrowing and return dates, how do i do that exactly? for example: if a user confirms the borrowing of a book, after the operation happens the date of borrowing should be added in the excel sheet, and also a little side request with this; if it is possible to somehow neglect the first row in a for loop that is looping across an array of loops and proceed with other rows then please help me out with that as well (this loop is at line 111 in the code, the one after the list "filteredBooks") here is my code so far:
`public class BookStudentMerge {

 public static List&lt;Row&gt; getStudentInfo(Sheet sheet, DataFormatter formatter, FormulaEvaluator evaluator, String searchValue) {
List&lt;Row&gt; result = new ArrayList&lt;Row&gt;();
String cellValue = &quot;&quot;;
String searchV = searchValue.toLowerCase();
for (Row row : sheet) {
for (Cell cell : row) {
cellValue = formatter.formatCellValue(cell, evaluator);
String cellV = cellValue.toLowerCase();
if (cellV.contains(searchV)) {
result.add(row);
break;
}
}
}
return result;
}
public static List&lt;Row&gt; bookSearch(Sheet sheet, DataFormatter formatter, FormulaEvaluator evaluator, String searchValue) {
List&lt;Row&gt; none = new ArrayList&lt;Row&gt;();
String searchM = searchValue.toLowerCase(); 
List&lt;Row&gt; result = new ArrayList&lt;Row&gt;();
String cellValue = &quot;&quot;;
for (Row row : sheet) {
for (Cell cell : row) {
cellValue = formatter.formatCellValue(cell, evaluator);
String cellM = cellValue.toLowerCase();
if (cellM.contains(searchM)) {
result.add(row);
break;
}
}
}
return result;
}
public static boolean checkAvailability (Row r, DataFormatter formatter, FormulaEvaluator evaluator) {
String cellValue = &quot;&quot;;
for (Cell c : r) {
cellValue = formatter.formatCellValue(c, evaluator);
String cellM = cellValue.toLowerCase();
if (cellM.equalsIgnoreCase(&quot;Available&quot;)) {
return true; 
}
}
return false;
}
public static void changeStatus (Row r, DataFormatter formatter, FormulaEvaluator evaluator) {
String cellValue = &quot;&quot;;
for (Cell c : r) {
cellValue = formatter.formatCellValue(c, evaluator);
String cellM = cellValue.toLowerCase();
if (cellM.equalsIgnoreCase(&quot;Available&quot;)) {
c.setCellValue(&quot;Unavailable&quot;);
}
else if (cellM.equalsIgnoreCase(&quot;Unavailable&quot;)) {
c.setCellValue(&quot;Available&quot;); 
}
}
}
public static void main(String[] args) throws IOException, InvalidFormatException {
Workbook Books = WorkbookFactory.create(new FileInputStream(&quot;C:\\Users\\abdul\\Desktop\\University Files\\Object-Oriented Programming\\Project files\\Book class\\Books &amp; DDC.xlsx&quot;));
Sheet SB = Books.getSheetAt(0);
Workbook Students = WorkbookFactory.create(new FileInputStream(&quot;C:\\Users\\abdul\\Desktop\\University Files\\Object-Oriented Programming\\Project files\\Book class\\StudentsInfo.xlsx&quot;));
Sheet SS = Students.getSheetAt(0);
DataFormatter FB = new DataFormatter();
FormulaEvaluator EB =  Books.getCreationHelper().createFormulaEvaluator();		
DataFormatter FS = new DataFormatter();
FormulaEvaluator ES =  Students.getCreationHelper().createFormulaEvaluator();
Scanner s = new Scanner (System.in);
System.out.println(&quot;Welcome to our BiblioTheque library program!&quot;);
System.out.println(&quot;Enter a valid student ID number: &quot;);
String inp = s.nextLine();
Row headers = SS.getRow(0);
for (Cell HS : headers) {
System.out.print(HS.getStringCellValue() + &quot;\t\t  &quot;);
}
System.out.println();
List&lt;Row&gt; filteredStudents = getStudentInfo(SS, FS, ES, inp);
for (Row row : filteredStudents) {
for (Cell cell : row) {
System.out.print(FS.formatCellValue(cell, ES));
System.out.print(&quot;\t     &quot;);
}
System.out.println();
}
System.out.println();
System.out.println(&quot;Search for a book by title, author, DDC, or ISBN: &quot;);
System.out.println(&quot;enter the name, author, ddc, isbn of your book: &quot;);
String search = s.nextLine();
List&lt;Row&gt; filteredBooks = bookSearch(SB, FB, EB, search);
Row HB = SB.getRow(0);
for (Cell CB : HB) {
System.out.print(CB.getStringCellValue() + &quot;\t\t\t&quot;);
}
System.out.println();
for (Row row : filteredBooks) {
for (Cell cell : row) {
System.out.print(FB.formatCellValue(cell, EB));
System.out.print(&quot;\t\t&quot;);
}
System.out.println();
}
if (filteredBooks.isEmpty()) {
System.out.println(&quot;no results found for the entered keyword/number&quot;);
}
else {
System.out.println(&quot;do you wish to borrow any of the displayed books?&quot;);
}
String ans = s.nextLine();
if (ans.equalsIgnoreCase(&quot;yes&quot;)) {
System.out.println(&quot;choose the desired book by typing the item&#39;s list number: &quot;);
}
int borrow = s.nextInt() - 1;
Row Result = filteredBooks.get(borrow);
if (checkAvailability(Result, FB, EB)) {
System.out.println(&quot;You are going to borrow the book &quot; + Result.getCell(0) + &quot;, please proceed to the help desk to complete the operation.&quot;);
changeStatus(Result, FB, EB);
}
}

}`

答案1

得分: 1

以下是翻译好的部分:

可能性之一是 java.time.LocalDateTime

import java.time.LocalDateTime;    

public class Time {    
  public static void main(String[] args) {    
   LocalDateTime time = LocalDateTime.now();  
   System.out.println(time);  
  }    
}    

这将产生类似于这样的输出:2020-05-30T13:29:14.566

如果你想要更改格式,你将需要使用 DateTimeFormatter

import java.time.LocalDateTime; 
import java.time.format.DateTimeFormatter;  

public class Time {    
  public static void main(String[] args) {    
   LocalDateTime time = LocalDateTime.now();

   DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
   String timeDate = time.format(formatter);
   System.out.println(timeDate);  
  }    
}      

这将产生类似于 30-05-2020 13:59:13 的输出。

因此,现在我们有了一个带有所需日期的 String。现在我们只需要将其写入 csv 文件(我建议使用 csv 而不是 excel 文件!)。关于如何实现这一点的示例在这个答案中提供。如果你真的想要使用 excel,我建议阅读这个问题及其答案。

更多信息可以在这里这里找到。

有了上面提供的信息和示例,你应该能够自己在你的代码中实现它。

英文:

One possibility is java.time.LocalDateTime:

import java.time.LocalDateTime;    

public class Time {    
  public static void main(String[] args) {    
   LocalDateTime time = LocalDateTime.now();  
   System.out.println(time);  
  }    
}    

This will give an output like this: 2020-05-30T13:29:14.566.

If you want to change the format, you will have to use a DateTimeFormatter:

import java.time.LocalDateTime; 
import java.time.format.DateTimeFormatter;  

public class Time {    
  public static void main(String[] args) {    
   LocalDateTime time = LocalDateTime.now();

   DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;dd-MM-yyyy HH:mm:ss&quot;);
   String timeDate = time.format(formatter);
   System.out.println(timeDate);  
  }    
}      

This will give an output like 30-05-2020 13:59:13.

So now we have a String with the wanted date. We now just have to write this to a csv-file (my recommendation is to use a csv and not a excel-file!). One example on how this is possible is provided in this answer. If you really want to use excel, I suggest reading this question and its answers.

More information can be found here and here.

With the information and examples provided above, you should be able to implement it by yourself in your code.

huangapple
  • 本文由 发表于 2020年5月30日 18:53:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/62101369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定