Dates in Java

Discussion in 'Plugin Development' started by ThrustLP, Aug 2, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    ThrustLP

    Hey guys! I am saving a Date in my config:

    Date d = new Date(System.currentTimeMillis());

    Now I have a String

    String datefromconfig = "2016-08-02T22:22:31.369Z";

    I want to transform this String back to an Date. It is the standard java date format. How can I do that?

    Thank you! (I want to compare it to the date at the moment, so it needs to be in that excact format)
     
  2. Offline

    ipodtouch0218

    Google is your friend.
    Code:
    Date <varName> = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(string);
    You may have to tweak the format to fit yours, not sure what the T and Z are for.
     
  3. Offline

    mine-care

    If this value is purely for your code to use and not for the user to understand and/or change, then consider using the system millisecond time rather than dates.
     
    MCMastery and Mokum like this.
  4. Offline

    ItzAndree

    if you want to save the date, I recommend you saving the System.currentTimesMilis() and if you want to get the date at that time, initialize the date with that value... Example:

    Code:
    //if you want to save the date initialization time do this:
    long currentMillis = System.currentTimeMillis();
    Date d = new Date(currentMillis);`
    //do whatever u want here
    getConfig.set("dateMillis", currentMillis);
    //in another part of the code...
    Date d = new Date(getConfig().getLong("dateMillis"));
    
    
    //OR if you want to store the date at the end or at any time u just change the place where u put the line "long currentMillis = System.currentTimeMillis();"
    if I didn't answer your question, tell me...
     
Thread Status:
Not open for further replies.

Share This Page