I am interested to know if my code is correct and it doesn't have any issues. Date is taken from binary file, written as a string in such format YYYY-MM-DD/Hours-minutes-seconds example: 2022-01-23/12:00:00.
Program was meant to check date if it's expiring or expired, add it to proper list and display it after loops end.
public static void expiration_date(String filepath){
try {
DataInputStream read = new DataInputStream(new FileInputStream(filepath));
DateTimeFormatter format_day = DateTimeFormatter.ofPattern("uuuu/MM/dd");
DateTimeFormatter format_hour = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalDate now_day = LocalDate.now();
LocalDateTime now_hour = LocalDateTime.now();
ArrayList<String> dates = new ArrayList<>();
ArrayList<String> expire_in_week = new ArrayList<>();
ArrayList<String> expire_tomorrow = new ArrayList<>();
ArrayList<String> expire_today = new ArrayList<>();
ArrayList<String> expired = new ArrayList<>();
while(read.available()>0) {
String name = read.readUTF();
String surname = read.readUTF();
String date = read.readUTF();
String cardcode = read.readUTF();
String cardtype = read.readUTF();
int contract_num = read.readInt();
String cert_num = read.readUTF();
String phone_num = read.readUTF();
String email = read.readUTF();
String status = read.readUTF();
String comment = read.readUTF();
dates.add(date);
for (String s:dates){
String[] data = s.split("/");
String days =data[0];
LocalDate date_days = LocalDate.parse(days.replace("-", "/"), format_day);
String hours =data[1];
LocalTime date_hours = LocalTime.parse(hours.replace("-", ":"),format_hour);
long daysbetween = ChronoUnit.DAYS.between(now_day,date_days);
long hoursbetween = ChronoUnit.HOURS.between(now_hour,date_hours);
if (daysbetween==7){
expire_in_week.add(cert_num);
}else if (daysbetween==1){
expire_tomorrow.add(cert_num);
}else if (daysbetween==0 && hoursbetween>0){
expire_today.add(cert_num);
}else if (daysbetween<0 || (daysbetween==0 && hoursbetween<0)){
expired.add(cert_num);
}else{}
}
}
System.out.println("Certificates that expires:");
System.out.println("Next week");
for (String w:expire_in_week){
System.out.print(w "|");
}
System.out.println("Tomorrow");
for (String tm:expire_tomorrow){
System.out.print(tm "|");
}
System.out.println("Today");
for (String td:expire_today){
System.out.print(td "|");
}
System.out.println("Today");
for (String ex:expired){
System.out.print(ex "|");
}
}catch(FileNotFoundException ex){ex.printStackTrace();}
catch(IOException ex){ex.printStackTrace();}
}
When i start it i got error message returned:
Exception in thread "main" java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: 12:00 of type java.time.LocalTime
at java.base/java.time.LocalDateTime.from(LocalDateTime.java:463)
at java.base/java.time.LocalDateTime.until(LocalDateTime.java:1677)
at java.base/java.time.temporal.ChronoUnit.between(ChronoUnit.java:272)
at com.company.program.expiration_date(program.java:189)
at com.company.program.main(program.java:364)
Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: 12:00 of type java.time.LocalTime
at java.base/java.time.LocalDate.from(LocalDate.java:398)
at java.base/java.time.LocalDateTime.from(LocalDateTime.java:458)
... 4 more
CodePudding user response:
The issue is because date_hours is of type LocalDateTime and date_hours is of type LocalTime. Try using the same type for both.
LocalTime now_hour = LocalTime.now();
Side note to parse the values of LocalDate and LocalTime you can try this
String data = "2022-01-23/12-00-00";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd/HH-mm-ss");
LocalDate dateDays = LocalDate.parse(data, dateTimeFormatter);
LocalTime dateHours = LocalTime.parse(data, dateTimeFormatter);
CodePudding user response:
See working example of your code here: https://github.com/RobbingDaHood/answers/blob/master/so70824574/src/Main.java
You LocalTime date_hours needs a timezone before it can be compared with LocalDateTime now_hour. So either add one to date_hours as I did in the code or remove the one from now_hour.
In my code the date_hours would get the system timezone, as the code is now. Be aware that maybe your file is not using that timezone.
Output of the example code is:
Certificates that expires: Next week Tomorrow Today Today Cert1|
You can read more here
https://www.baeldung.com/java-8-date-time-intro That will give you a good intro to the different date types in Java.
