Solved Timestamps from config.yml

Discussion in 'Spigot Plugin Development' started by torpkev, Sep 18, 2019.

  1. I have a SQLite database that I'm transitioning over back to YML files for a few off-topic reasons.

    I have a table of users which looks something like this:

    Code (Text):

    player_uuid: f2396035-bb27-3974-89f7-1b8cb301bbc5
    last_name: Alex29
    last_seen: 2019-06-14 14:35:42.326
    On startup, I'm reading the table and then pushing the data into a yml which looks like this:

    Code (YAML):
    : Alex29
    : 2019-06-14T19:35:42.326Z
    Then when I read that back out, I'm getting an error that the last_seen value is unparseable

    Here is the code I'm using:
    Code (Java):

    // This date format is set outside my loop
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
    // loop through playerUUID values -
    if (customConfig.contains("users." + playerUUID + ".last_seen")) {
                                    try {
                                        Date ftPlacedDateTime = dateFormat.parse(customConfig.getString("users." + playerUUID + ".last_seen"));
                                        lastSeen = new java.sql.Timestamp(ftPlacedDateTime.getTime());
                                    } catch(Exception e) {
                                        error = true;
                                                "Unexpected error getting users. " + "users." + playerUUID + ".last_seen is not a valid timestamp: " + e.getMessage()
    And my error message:

    [11:17:35] [Server thread/INFO]: !!! [Error][JoinedPlayerFileAccess.loadFromFile()] Unexpected error getting users. users.f2396035-bb27-3974-89f7-1b8cb301bbc5.last_seen is not a valid timestamp: Unparseable date: "Fri Jun 14 14:35:42 CDT 2019"

    Can someone point me in the right direction of where I'm going wrong here?

  2. Probably a formatting issue, fwiw I usually store timestamps in yml files as Long data types, not screwing around having to convert it from one format to another, a simple albeit large number. If you can easily read it from the database it shouldn’t be hard to convert the time stamp to a Long value ie milliseconds.

    Sent from my iPhone using Tapatalk
    • Agree Agree x 1
  3. That seems to be the path of least resistance.