Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: HELP "code needed"

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default HELP "code needed"

    Hello

    Can someone please show me code in Java that stores the date and time into a file every minute?
    The reason for this is that i am looking to copy every minute from the computer clock in order to see if changes have been made.

    I am not sure if I need to use a loop of some sort or whether there is code itself that will do this for me.

    Any help will be appreciated

    Thanks
    Dave


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: HELP "code needed"

    Welcome to Java Programming Forums Dave.

    Well lets see what you have as a starter, so we have a place to build off. A loop will be needed to make it run yes Thread.sleep() may also be helpful in timing, or you could use System timing if you wish to be more accurate.

    Chris

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    Dave (July 31st, 2009)

  4. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: HELP "code needed"

    How about this?

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.io.*;
     
    public class DavesTimer {
     
        /**
         * JavaProgrammingForums.com
         */    
         public static final String DATE_FORMAT_NOW = "HH:mm:ss";
         public static String time;
     
         public static String Time() 
         {
             Calendar cal = Calendar.getInstance();
             SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
             return sdf.format(cal.getTime());
         }
     
         public static void GetTime() throws Exception{
     
            time = Time();
            System.out.println(time);
            WriteFile();
     
         }
     
         public static void WriteFile() throws Exception{
     
                 String newLine = System.getProperty("line.separator");
     
                Writer output = null;
                File file = new File("myFile.txt");
                output = new BufferedWriter(new FileWriter(file, true));
                output.write(time);
                output.write(newLine);
                output.close();
                System.out.println("File written");
                System.out.println("Waiting 1 minute...");
                Thread.sleep(60000);
                GetTime();         
         }
     
        public static void main(String[] args) throws Exception {
     
            DavesTimer dt = new DavesTimer();
            dt.GetTime();
     
        }
     
    }

    Example console output:

    15:31:51
    File written
    Waiting 1 minute...
    15:32:51
    File written
    Waiting 1 minute...
    15:33:51
    File written
    Waiting 1 minute...
    15:34:51
    File written
    Waiting 1 minute...
    The myFile.txt looks like this:

    15:31:51
    15:32:51
    15:33:51
    15:34:51
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. The Following User Says Thank You to JavaPF For This Useful Post:

    Dave (July 31st, 2009)

  6. #4
    Junior Member
    Join Date
    Jul 2009
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: HELP "code needed"

    Thank you very much for the help

  7. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: HELP "code needed"

    Quote Originally Posted by Dave View Post
    Thank you very much for the help
    No problem Dave. Has this solved your issue?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. #6
    Junior Member
    Join Date
    Jul 2009
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: HELP "code needed"

    i do have a couple of extra issues/ help with this

    1. will this coinside with the computer. if not how can i make this happen as this is really important

    2. i am also looking at adding a method that will not allow you to change the clock by more than 5 mins at a time. This is why i need the recordings to coincide with the computer clock. Also if it is possible i would like to add a rule that will allow the clock to change when the clocks naturally move forward or back (GMT)

    finally i am looking to add a password box in order to end the program and also ideally a password box that needs to be typed when i call for the list of times.

    I know this sounds like i am throwing alot out there however i am researching to see if java can be used as a computer clock monitor as part of a computer forensics research.

    Once again any help will be appreciated.

    Thankyou

    Dave

  9. #7
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: HELP "code needed"

    Quote Originally Posted by Dave View Post
    i do have a couple of extra issues/ help with this

    1. will this coinside with the computer. if not how can i make this happen as this is really important

    2. i am also looking at adding a method that will not allow you to change the clock by more than 5 mins at a time. This is why i need the recordings to coincide with the computer clock. Also if it is possible i would like to add a rule that will allow the clock to change when the clocks naturally move forward or back (GMT)

    finally i am looking to add a password box in order to end the program and also ideally a password box that needs to be typed when i call for the list of times.

    I know this sounds like i am throwing alot out there however i am researching to see if java can be used as a computer clock monitor as part of a computer forensics research.

    Once again any help will be appreciated.

    Thankyou

    Dave
    This is a very interresting question. It can probably be pulled of, but it will need some hardcore coding, beyond what java is capable of(if what I understanding from your questions is correct that is.)
    Im hoping one the sharp minds will look this over, and either correct me if im wrong, or show some magic.

    1. The Calendar gives you the time read of the computer clock, everytime you make a new instance. In other words, change the computer clock, changes the println respectivly.

    2. Here is what i understand. You want to "fix" your computer clock, so it cant be modified with more than x on one change. If that is the case, it cant be solved by java, atleast not java alone. Your clock i run in the os.
    Java is run in a virtual mashine (jvm), which isolates it from its environment (memory, os etc). Contact in and out of the jvm is tricky at best, impossible at worst. There is a framework(Java Native Interface) specializing in exactly this, but other than knowing how to use it, you also need to have a very good understanding of your os, to manipulate it in a way like this.
    In windows you can change pretty much anything if you know how, dont know to much about different linux destros, but im guessing its the same.

    3. If by password you mean "cant shut down the program without it" then no, cant be done in java (read 2).

  10. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: HELP "code needed"

    Ye, Java cannot interact with the clock to the level that you wish as stated by Johannes. But he is correct, JNI (Java Native Interface) wil allow you to communicate with C or C++ which can then work nicely with system operations and can do that sort of thing. Again you cannot stop the user from closing the program down as they can get at it from a system level. But once again JNI and C or C++ can be used to deal with this. If you are prepared to learn how to use the JNI and C or C++ if you are not already familiar then you are ok. Ofcourse since you are investigating if Java alone can do it then I have to say no.

    Also if you inlist the aid of C or C++ and system functions, you have just lost portability.

    Chris,
    This reminds me, I have a Mouse/Keyboard hook to finish writing in C++ using JNI D:

  11. #9
    Junior Member
    Join Date
    Aug 2009
    Location
    San Fransisco
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP "code needed"

    Never, never use Thread.Sleep() you will end up totally wrong. It waits more than given time and it can wait lot longer when your processor cycles are sucked by some other process. Look at this as well: Thread (Java 2 Platform SE v1.4.2), int)

    In fact I would rather suggest Timer, but never sleep. You should look here: Timer Schedule a task that executes once every second : TimerDevelopment ClassJava

    Also I posted 'DateTime is not thread safe' in tips section yesterday, please try to use joda time. I know you are just starting, but these are some good practices.

    Will try to post more when I am free. Good luck.

    Acumen,
    Lucid forums

  12. #10
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: HELP "code needed"

    Actually there are a number of ways to set the system time using Java, I had a discussion about this with a colleague a while back.

    1. Write or download a library for this with including native library files such as .dll .so etc. This will be limited to the operating system you have the native libraries for.

    2. Use Java runtime to set the time using a call to the operating system command, for instance in Windows XP you could use time

            try {
                Runtime.getRuntime().exec("time 12:00");
            } catch (IOException e) {
                e.printStackTrace();
            }

    You would of course have to write some code to check what operating system the program is run to select the correct command.

    // Json

  13. #11
    Junior Member
    Join Date
    Jul 2009
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: HELP "code needed"

    Thank you Json

    my java skills arnt the greatest. how would i actually do this

  14. #12
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: HELP "code needed"

    Either by using that code snippet I gave you in my previous post or you could try and find a sort of Java time library with natives for it or was your question regarding something else?

    // Json

  15. #13
    Junior Member
    Join Date
    Jul 2009
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: HELP "code needed"

    ok 1 last question

    with the code supplied by JavaPF I am looking at adding a method or something that will warn the user if there has been a sudden change of 5 minutes or more, this can be done as an error message.

    Once again thanks for all the help.

    Dave

  16. #14
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with method

    Look at the JOptionPane.showMessageDialog method. You'll have to add your own logic, but this will give a pop-up of the time change.
    JOptionPane.showMessageDialog(null,"Error: Time has been changed by more than 5 minutes");

  17. The Following User Says Thank You to helloworld922 For This Useful Post:

    Dave (August 26th, 2009)

  18. #15
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Help with method

    You basically need to take the previous recorded time and compare it with the latest time and work out the difference.

    I would use String replace on the time. For example 10:01:45, replace the : with "" so it looks like 100145.

    Do this with the next recorded time. Then take one away from the other. If you are left with a 500+ difference, the time has changed by 5 minutes or more.

    I've just got to figure out how to code this when I get some time and I'll add it to my original example.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  2. Replies: 2
    Last Post: March 23rd, 2010, 01:38 AM
  3. Replies: 4
    Last Post: June 18th, 2009, 09:23 AM
  4. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM

Tags for this Thread