简体   繁体   中英

How to properly convert string with time from text file to time?

I use Calendar to get time. I want to get string from file(time) and parse it to calendar object. I will compare time from file and current time using after(); and before(); method of Calendar class

If time from file is after current time -> print ("Time from file is after curretn time.

If time from file is before current time -> print ("Time from file is before

What I did:

  • I Created file
  • Got current time
  • Parsed current time to a string
  • Saved a string to a file
  • Got current time
  • Read etx from file
  • Parsed text from file to Date object

Returned time from file is correct, but he problem is that parsed time from file return Thu Jan 01 20:59:00 GMT+07:00 1970 not my current year

That's why if statement always return first argument

How to properly parse time from file to make comparison of two object of time?

Here is my full code:

public class Main {


  public static void main(String[] args) throws IOException, ParseException {

    Calendar calNow = Calendar.getInstance();
    Date dateNow = new Date();
    calNow.setTime(dateNow);


    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, 5);
    SimpleDateFormat df = new SimpleDateFormat("HH:mm");
    String formatted = df.format(cal.getTime());

    String formattedFut = df.format(calNow.getTime());

    Path file = Paths.get("testFile.txt");

    if (Files.exists(file) && Files.size(file) == 0) {

      Files.write(file, List.of(formatted));

    } 


    String timeFromFile = new String(Files.readAllBytes(Paths.get("testFile.txt")));

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    Date dateOld = sdf.parse(timeFromFile);
    Calendar calendarOld = Calendar.getInstance();
    calendarOld.setTime(dateOld);


    if (dateNow.after(dateOld)) {
      System.out.println(dateNow + " is after day from file");
    }

    if (dateNow.before(dateOld)) {
      System.out.println(dateNow + " is before day from file");

    }

  }

}

SOLUTION FOUND:

   public class Main {


   public static void main(String[] args) throws IOException, ParseException {

 SimpleDateFormat("HH:mm").format(Calendar.getInstance().getTime());


Calendar calendarNow = Calendar.getInstance();
Date dateNow = new Date();
calendarNow.setTime(dateNow);


Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 5);
SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
String formatted = df.format(cal.getTime());

String formattedFut = df.format(calendarNow.getTime());

Path file = Paths.get("testFile.txt");

if (Files.exists(file) && Files.size(file) == 0) {

  Files.write(file, List.of(formatted));    
} 

Calendar calendarOld = Calendar.getInstance();
String timeFromFile = new String(Files.readAllBytes(Paths.get("testFile.txt")));

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
calendarOld.setTime(sdf.parse(timeFromFile));

if (calendarNow.after(calendarOld)) {
  System.out.println(timeFromFile + " \ntime passed");
}

if (calendarNow.before(calendarOld)) {
  System.out.println(timeFromFile + " \n time NOT PASSED");

    }

  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM