[英]How do i keep track of the time at which something happened in my code?
我正在制作一個讀取用戶輸入的程序,允許他們查看我圖書館中可用的書籍(從 excel 表導入),選擇一個項目並選擇借用它,但問題是我想放借用和歸還日期,我該怎么做? 例如:如果用戶確認借書,操作發生后需要在excel表中添加借書日期,並附帶一點請求; 如果有可能以某種方式忽略在循環數組中循環的 for 循環中的第一行並繼續處理其他行,那么請也幫我解決這個問題(這個循環在代碼的第 111 行,后面的那個到目前為止,“filteredBooks”列表是我的代碼:`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);
}
}
}`
一種可能性是java.time.LocalDateTime
:
import java.time.LocalDateTime;
public class Time {
public static void main(String[] args) {
LocalDateTime time = LocalDateTime.now();
System.out.println(time);
}
}
這將給出一個 output 像這樣: 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);
}
}
這將給出一個 output 像30-05-2020 13:59:13
。
所以現在我們有了一個帶有所需日期的String
。 我們現在只需將其寫入 csv 文件(我的建議是使用 csv 而不是 excel 文件。)。 這個答案中提供了一個關於如何做到這一點的例子。 如果您真的想使用 excel,我建議您閱讀這個問題及其答案。
通過上面提供的信息和示例,您應該能夠在代碼中自己實現它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.