0

I know I'm probably doing something wrong, but I am trying to format a Date that is currently stored inside of a string but it won't let me parse it to a String because it doesn't recognize it as a Date (because it's in a String variable) and won't let me format it because it cannot format it in its current state. For reference, I am making a time clock application.

I apologize if I'm doing something stupid but I am fairly new to this and have never used SimpleDateFormat before. I put some snippets of code below:

ArrayList<String> punchHistoryTimes = new ArrayList<String>();
SimpleDateFormat sdf =new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

public void updatePunchHistory(Sheet sheet){
    for(int rowNum:rowNumbers){
        punchHistoryTimes.add(sheet.getRow(rowNum).getCell(1).getStringCellValue());
        punchHistory.add(new JLabel(sheet.getRow(rowNum).getCell(0).getRichStringCellValue().getString()+ " " + sheet.getRow(rowNum).getCell(1).getRichStringCellValue().getString()+ " " + sheet.getRow(rowNum).getCell(2).getRichStringCellValue().getString()));
    }
}

//other code is above this but not relevant to the issue
currentEmployee.setEndTime(sdf.format(currentDate));
if(punchHistory.get(punchHistory.size()-1).getText().contains("Clocked In")){
    calcTimeWorked(punchHistory.get(punchHistoryTimes.size()-1).getText(),currentEmployee.getEndTime());
}else{
    //This line below is where the error is happening
    //value of currentEmployee.getStartTime() at error: 1654653731536
    //value of currentEmployee.getEndTime() at error: 07-06-2022 21:02:12
    //Both currentEmployee.getStartTime() and currentEmployee.getEndTime() are stored as Strings
    calcTimeWorked(currentEmployee.getStartTime(),currentEmployee.getEndTime());
}
currentEmployee.setHoursWorked(differenceInTime);

I tried using the debugger and it shows the error is that it cannot parse 1654653731536. I understand the issue but cannot get a solution. I believe the issue is because when it stores the value in the excel file it is storing the date as a string but then when it pulls the date back out of the excel later (the application would have been closed between these events) it views it as a string and does not recognize that there is a Date inside of the String. Is there any way to cast the String 1654653731536 to a Date?

Abra
  • 19,142
  • 7
  • 29
  • 41
  • 1
    Refer to [Date Time](https://docs.oracle.com/javase/tutorial/datetime/index.html) trail in Oracle's Java tutorials. – Abra Jun 08 '22 at 02:17
  • 1
    [Edit] your question and post the stack trace. – Abra Jun 08 '22 at 02:17
  • 1
    `sdf.format(currentDate)` - I am guessing that `currentDate` is not a `Date` – Scary Wombat Jun 08 '22 at 02:26
  • 1
    I strongly recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `ZonedDateTime` and `DateTimeFormatter` and/or other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 08 '22 at 03:58
  • 1
    1654653731536 are milliseconds since the epoch (beyond any reasonable doubt) representing 2022-06-08T02:02:11.536Z (UTC). `Instant.ofEpochMilli(1654653731536L)` should get you on the right track. To convert to your time zone use something like `Instant.ofEpochMilli(1654653731536L).atZone(ZoneId.of("Pacific/Guadalcanal"))`. With the mentioned example time zone I got `2022-06-08T13:02:11.536+11:00[Pacific/Guadalcanal]`, but you will substitute your own time zone or perhaps `ZoneId.systemDefault()`. – Ole V.V. Jun 08 '22 at 04:03
  • 1
    For parsing the milliseconds as a string see for example [my answer here](https://stackoverflow.com/a/59468723/5772882). Assuming that you are not using the ThreeTen Backport, notice the point (bullet) about using `Instant::from` instead of `Instant.FROM`. – Ole V.V. Jun 08 '22 at 04:11
  • 1
    And obviously, if there is any way you can modify your employee class, store start and end time as `Instant` instead of `String`. – Ole V.V. Jun 08 '22 at 04:15
  • For dealing with the different strings that you have currently got I put together [this little demo](https://rextester.com/PKBBG48335). In addition to parsing the strings I am also calculating the work time. The current output is a `Duration` printed as `PT1H0.464S`, which means 1 hour 0.464 seconds. The `Duration` is fine for further processing or formatting. – Ole V.V. Jun 08 '22 at 04:39

0 Answers0