How do I touch a folder with current time using set last modified date & time?












10














I'm trying to update the last modified date of a specific folder, here's what I've got:



public void touchFolder(){
File folderToTest = new File("C:\Temp");
SimpleDateFormat dateFormatUtc = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC"));
String newTime = dateFormatUtc.format(new Date());
folderToTest.setLastModified(Long.parseLong(newTime));
}


I am just putting this code in a test case so don't worry about calling this method etc.



I'm getting errors with the parsing that date format as a long, what's the format used in setting the last modified date & time?










share|improve this question




















  • 1




    Instead of formatting the date to some string and parsing that to a long (which wouldn't work because 2018-12-19 ... is not a long) just use System.currentTimeMillis() to get the current time as a long.
    – Thomas
    Dec 19 '18 at 15:23






  • 2




    Consider using java.nio and java.time for operations concerning time and the file system.
    – deHaar
    Dec 19 '18 at 15:24










  • last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970).File doc
    – TongChen
    Dec 19 '18 at 15:26
















10














I'm trying to update the last modified date of a specific folder, here's what I've got:



public void touchFolder(){
File folderToTest = new File("C:\Temp");
SimpleDateFormat dateFormatUtc = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC"));
String newTime = dateFormatUtc.format(new Date());
folderToTest.setLastModified(Long.parseLong(newTime));
}


I am just putting this code in a test case so don't worry about calling this method etc.



I'm getting errors with the parsing that date format as a long, what's the format used in setting the last modified date & time?










share|improve this question




















  • 1




    Instead of formatting the date to some string and parsing that to a long (which wouldn't work because 2018-12-19 ... is not a long) just use System.currentTimeMillis() to get the current time as a long.
    – Thomas
    Dec 19 '18 at 15:23






  • 2




    Consider using java.nio and java.time for operations concerning time and the file system.
    – deHaar
    Dec 19 '18 at 15:24










  • last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970).File doc
    – TongChen
    Dec 19 '18 at 15:26














10












10








10







I'm trying to update the last modified date of a specific folder, here's what I've got:



public void touchFolder(){
File folderToTest = new File("C:\Temp");
SimpleDateFormat dateFormatUtc = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC"));
String newTime = dateFormatUtc.format(new Date());
folderToTest.setLastModified(Long.parseLong(newTime));
}


I am just putting this code in a test case so don't worry about calling this method etc.



I'm getting errors with the parsing that date format as a long, what's the format used in setting the last modified date & time?










share|improve this question















I'm trying to update the last modified date of a specific folder, here's what I've got:



public void touchFolder(){
File folderToTest = new File("C:\Temp");
SimpleDateFormat dateFormatUtc = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC"));
String newTime = dateFormatUtc.format(new Date());
folderToTest.setLastModified(Long.parseLong(newTime));
}


I am just putting this code in a test case so don't worry about calling this method etc.



I'm getting errors with the parsing that date format as a long, what's the format used in setting the last modified date & time?







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 19 '18 at 15:25









Karol Dowbecki

16.4k82849




16.4k82849










asked Dec 19 '18 at 15:19









Tiffany

534




534








  • 1




    Instead of formatting the date to some string and parsing that to a long (which wouldn't work because 2018-12-19 ... is not a long) just use System.currentTimeMillis() to get the current time as a long.
    – Thomas
    Dec 19 '18 at 15:23






  • 2




    Consider using java.nio and java.time for operations concerning time and the file system.
    – deHaar
    Dec 19 '18 at 15:24










  • last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970).File doc
    – TongChen
    Dec 19 '18 at 15:26














  • 1




    Instead of formatting the date to some string and parsing that to a long (which wouldn't work because 2018-12-19 ... is not a long) just use System.currentTimeMillis() to get the current time as a long.
    – Thomas
    Dec 19 '18 at 15:23






  • 2




    Consider using java.nio and java.time for operations concerning time and the file system.
    – deHaar
    Dec 19 '18 at 15:24










  • last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970).File doc
    – TongChen
    Dec 19 '18 at 15:26








1




1




Instead of formatting the date to some string and parsing that to a long (which wouldn't work because 2018-12-19 ... is not a long) just use System.currentTimeMillis() to get the current time as a long.
– Thomas
Dec 19 '18 at 15:23




Instead of formatting the date to some string and parsing that to a long (which wouldn't work because 2018-12-19 ... is not a long) just use System.currentTimeMillis() to get the current time as a long.
– Thomas
Dec 19 '18 at 15:23




2




2




Consider using java.nio and java.time for operations concerning time and the file system.
– deHaar
Dec 19 '18 at 15:24




Consider using java.nio and java.time for operations concerning time and the file system.
– deHaar
Dec 19 '18 at 15:24












last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970).File doc
– TongChen
Dec 19 '18 at 15:26




last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970).File doc
– TongChen
Dec 19 '18 at 15:26












3 Answers
3






active

oldest

votes


















9














This is an example from the documentation, using java.nio.file.Files:



Path path = ...
FileTime now = FileTime.fromMillis(System.currentTimeMillis());
Files.setLastModifiedTime(path, now);





share|improve this answer





















  • FileTime can also be done as FileTime.from(Instance.now());
    – locus2k
    Dec 19 '18 at 15:42










  • My path was a string so I used this: Path path = Paths.get("path\of\directory"); Worked beautifully!
    – Tiffany
    Dec 19 '18 at 15:47












  • Tiffany why you accepted this answer and not the ones we added before this one with @Karol Dowbecki?
    – aBnormaLz
    Dec 19 '18 at 16:03










  • @aBnormaLz All are great answers but I am only able to accept one. This one has clear structure with documentation and is quick to implement. Thanks for all your help.
    – Tiffany
    Dec 19 '18 at 17:06



















6














I think you should just do folderToTest.setLastModified(System.currentTimeMillis());






share|improve this answer





























    5














    In your code newTime is a formatted date 2018-12-19 15:21:31 which can't be parsed to Long. What you want to do is supply the time in milliseconds e.g.:



    Date d = new Date();
    file.setLastModified(d.getTime());


    As per File.setLastModified() method javadoc:




    time - The new last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)







    share|improve this answer





















    • You don't need to instantiate a new Date() for this. you can just use System.currentTimeMillis()
      – aBnormaLz
      Dec 19 '18 at 15:25






    • 1




      @aBnormaLz true, it's just an example of using arbitrary Date object instead of current time in case OP has a precise date in mind.
      – Karol Dowbecki
      Dec 19 '18 at 15:26










    • Oh that's true :)
      – aBnormaLz
      Dec 19 '18 at 15:27











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53854203%2fhow-do-i-touch-a-folder-with-current-time-using-set-last-modified-date-time%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    9














    This is an example from the documentation, using java.nio.file.Files:



    Path path = ...
    FileTime now = FileTime.fromMillis(System.currentTimeMillis());
    Files.setLastModifiedTime(path, now);





    share|improve this answer





















    • FileTime can also be done as FileTime.from(Instance.now());
      – locus2k
      Dec 19 '18 at 15:42










    • My path was a string so I used this: Path path = Paths.get("path\of\directory"); Worked beautifully!
      – Tiffany
      Dec 19 '18 at 15:47












    • Tiffany why you accepted this answer and not the ones we added before this one with @Karol Dowbecki?
      – aBnormaLz
      Dec 19 '18 at 16:03










    • @aBnormaLz All are great answers but I am only able to accept one. This one has clear structure with documentation and is quick to implement. Thanks for all your help.
      – Tiffany
      Dec 19 '18 at 17:06
















    9














    This is an example from the documentation, using java.nio.file.Files:



    Path path = ...
    FileTime now = FileTime.fromMillis(System.currentTimeMillis());
    Files.setLastModifiedTime(path, now);





    share|improve this answer





















    • FileTime can also be done as FileTime.from(Instance.now());
      – locus2k
      Dec 19 '18 at 15:42










    • My path was a string so I used this: Path path = Paths.get("path\of\directory"); Worked beautifully!
      – Tiffany
      Dec 19 '18 at 15:47












    • Tiffany why you accepted this answer and not the ones we added before this one with @Karol Dowbecki?
      – aBnormaLz
      Dec 19 '18 at 16:03










    • @aBnormaLz All are great answers but I am only able to accept one. This one has clear structure with documentation and is quick to implement. Thanks for all your help.
      – Tiffany
      Dec 19 '18 at 17:06














    9












    9








    9






    This is an example from the documentation, using java.nio.file.Files:



    Path path = ...
    FileTime now = FileTime.fromMillis(System.currentTimeMillis());
    Files.setLastModifiedTime(path, now);





    share|improve this answer












    This is an example from the documentation, using java.nio.file.Files:



    Path path = ...
    FileTime now = FileTime.fromMillis(System.currentTimeMillis());
    Files.setLastModifiedTime(path, now);






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 19 '18 at 15:34









    Radiodef

    31.7k126596




    31.7k126596












    • FileTime can also be done as FileTime.from(Instance.now());
      – locus2k
      Dec 19 '18 at 15:42










    • My path was a string so I used this: Path path = Paths.get("path\of\directory"); Worked beautifully!
      – Tiffany
      Dec 19 '18 at 15:47












    • Tiffany why you accepted this answer and not the ones we added before this one with @Karol Dowbecki?
      – aBnormaLz
      Dec 19 '18 at 16:03










    • @aBnormaLz All are great answers but I am only able to accept one. This one has clear structure with documentation and is quick to implement. Thanks for all your help.
      – Tiffany
      Dec 19 '18 at 17:06


















    • FileTime can also be done as FileTime.from(Instance.now());
      – locus2k
      Dec 19 '18 at 15:42










    • My path was a string so I used this: Path path = Paths.get("path\of\directory"); Worked beautifully!
      – Tiffany
      Dec 19 '18 at 15:47












    • Tiffany why you accepted this answer and not the ones we added before this one with @Karol Dowbecki?
      – aBnormaLz
      Dec 19 '18 at 16:03










    • @aBnormaLz All are great answers but I am only able to accept one. This one has clear structure with documentation and is quick to implement. Thanks for all your help.
      – Tiffany
      Dec 19 '18 at 17:06
















    FileTime can also be done as FileTime.from(Instance.now());
    – locus2k
    Dec 19 '18 at 15:42




    FileTime can also be done as FileTime.from(Instance.now());
    – locus2k
    Dec 19 '18 at 15:42












    My path was a string so I used this: Path path = Paths.get("path\of\directory"); Worked beautifully!
    – Tiffany
    Dec 19 '18 at 15:47






    My path was a string so I used this: Path path = Paths.get("path\of\directory"); Worked beautifully!
    – Tiffany
    Dec 19 '18 at 15:47














    Tiffany why you accepted this answer and not the ones we added before this one with @Karol Dowbecki?
    – aBnormaLz
    Dec 19 '18 at 16:03




    Tiffany why you accepted this answer and not the ones we added before this one with @Karol Dowbecki?
    – aBnormaLz
    Dec 19 '18 at 16:03












    @aBnormaLz All are great answers but I am only able to accept one. This one has clear structure with documentation and is quick to implement. Thanks for all your help.
    – Tiffany
    Dec 19 '18 at 17:06




    @aBnormaLz All are great answers but I am only able to accept one. This one has clear structure with documentation and is quick to implement. Thanks for all your help.
    – Tiffany
    Dec 19 '18 at 17:06













    6














    I think you should just do folderToTest.setLastModified(System.currentTimeMillis());






    share|improve this answer


























      6














      I think you should just do folderToTest.setLastModified(System.currentTimeMillis());






      share|improve this answer
























        6












        6








        6






        I think you should just do folderToTest.setLastModified(System.currentTimeMillis());






        share|improve this answer












        I think you should just do folderToTest.setLastModified(System.currentTimeMillis());







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 19 '18 at 15:22









        aBnormaLz

        352212




        352212























            5














            In your code newTime is a formatted date 2018-12-19 15:21:31 which can't be parsed to Long. What you want to do is supply the time in milliseconds e.g.:



            Date d = new Date();
            file.setLastModified(d.getTime());


            As per File.setLastModified() method javadoc:




            time - The new last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)







            share|improve this answer





















            • You don't need to instantiate a new Date() for this. you can just use System.currentTimeMillis()
              – aBnormaLz
              Dec 19 '18 at 15:25






            • 1




              @aBnormaLz true, it's just an example of using arbitrary Date object instead of current time in case OP has a precise date in mind.
              – Karol Dowbecki
              Dec 19 '18 at 15:26










            • Oh that's true :)
              – aBnormaLz
              Dec 19 '18 at 15:27
















            5














            In your code newTime is a formatted date 2018-12-19 15:21:31 which can't be parsed to Long. What you want to do is supply the time in milliseconds e.g.:



            Date d = new Date();
            file.setLastModified(d.getTime());


            As per File.setLastModified() method javadoc:




            time - The new last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)







            share|improve this answer





















            • You don't need to instantiate a new Date() for this. you can just use System.currentTimeMillis()
              – aBnormaLz
              Dec 19 '18 at 15:25






            • 1




              @aBnormaLz true, it's just an example of using arbitrary Date object instead of current time in case OP has a precise date in mind.
              – Karol Dowbecki
              Dec 19 '18 at 15:26










            • Oh that's true :)
              – aBnormaLz
              Dec 19 '18 at 15:27














            5












            5








            5






            In your code newTime is a formatted date 2018-12-19 15:21:31 which can't be parsed to Long. What you want to do is supply the time in milliseconds e.g.:



            Date d = new Date();
            file.setLastModified(d.getTime());


            As per File.setLastModified() method javadoc:




            time - The new last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)







            share|improve this answer












            In your code newTime is a formatted date 2018-12-19 15:21:31 which can't be parsed to Long. What you want to do is supply the time in milliseconds e.g.:



            Date d = new Date();
            file.setLastModified(d.getTime());


            As per File.setLastModified() method javadoc:




            time - The new last-modified time, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 19 '18 at 15:22









            Karol Dowbecki

            16.4k82849




            16.4k82849












            • You don't need to instantiate a new Date() for this. you can just use System.currentTimeMillis()
              – aBnormaLz
              Dec 19 '18 at 15:25






            • 1




              @aBnormaLz true, it's just an example of using arbitrary Date object instead of current time in case OP has a precise date in mind.
              – Karol Dowbecki
              Dec 19 '18 at 15:26










            • Oh that's true :)
              – aBnormaLz
              Dec 19 '18 at 15:27


















            • You don't need to instantiate a new Date() for this. you can just use System.currentTimeMillis()
              – aBnormaLz
              Dec 19 '18 at 15:25






            • 1




              @aBnormaLz true, it's just an example of using arbitrary Date object instead of current time in case OP has a precise date in mind.
              – Karol Dowbecki
              Dec 19 '18 at 15:26










            • Oh that's true :)
              – aBnormaLz
              Dec 19 '18 at 15:27
















            You don't need to instantiate a new Date() for this. you can just use System.currentTimeMillis()
            – aBnormaLz
            Dec 19 '18 at 15:25




            You don't need to instantiate a new Date() for this. you can just use System.currentTimeMillis()
            – aBnormaLz
            Dec 19 '18 at 15:25




            1




            1




            @aBnormaLz true, it's just an example of using arbitrary Date object instead of current time in case OP has a precise date in mind.
            – Karol Dowbecki
            Dec 19 '18 at 15:26




            @aBnormaLz true, it's just an example of using arbitrary Date object instead of current time in case OP has a precise date in mind.
            – Karol Dowbecki
            Dec 19 '18 at 15:26












            Oh that's true :)
            – aBnormaLz
            Dec 19 '18 at 15:27




            Oh that's true :)
            – aBnormaLz
            Dec 19 '18 at 15:27


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53854203%2fhow-do-i-touch-a-folder-with-current-time-using-set-last-modified-date-time%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre