Solved

How to change System DATE and TIME ?

Posted on 2001-09-01
5
277 Views
Last Modified: 2008-03-10
How we can change the current system's Date and time from a Java application ?

Thanks in advance !

:-)

c_sh.
0
Comment
Question by:c_sh
5 Comments
 
LVL 16

Expert Comment

by:Valeri
Comment Utility
Listening...
0
 

Expert Comment

by:muhotrepius
Comment Utility
One way i see is by using RunTime.exec(...) but that's OS dependant...
Or write a native library and use JNI.
0
 
LVL 4

Accepted Solution

by:
sdussinger earned 111 total points
Comment Utility
Try this. It'll work on both Windows and Linux (although on Linux you'll need to run it as root, since normal users can't change the date):

-----------------------------------------------------------

import java.lang.Process;
import java.lang.Runtime;
import java.text.*;
import java.util.*;

public class SetDate
{
  static Process p;
  static Runtime rt=Runtime.getRuntime();

  public static void main(String args[])
  {

      String sourceDate = "2001-01-01T00:00:00";

      SimpleDateFormat sourceFormat = new SimpleDateFormat ("yyyy-MM-dd'T'hh:mm:ss");
      SimpleDateFormat winDateFormat = new SimpleDateFormat ("MM/dd/yy");
      SimpleDateFormat winTimeFormat = new SimpleDateFormat ("hh:mm:ss");
      SimpleDateFormat linuxDateTimeFormat = new SimpleDateFormat ("MMddhhmmyy.ss");

      Date d = sourceFormat.parse (sourceDate, new ParsePosition (0));

      String osName = System.getProperty("os.name");

      try
      {

          if (osName.equalsIgnoreCase ("linux"))
          {
            String linuxDate = linuxDateTimeFormat.format (d);

            p = rt.exec ("date " + linuxDate);
          }
          else
          {

            String winDate = winDateFormat.format (d);
            String winTime = winTimeFormat.format (d);

            p = rt.exec("cmd /c date " + winDate);
            p = rt.exec ("cmd /c time " + winTime);
          }
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
  }
}

-----------------------------------------------------------

HTH

--Steve
0
 

Author Comment

by:c_sh
Comment Utility
Thanks to all of you for giving attention to this problem, and a special thanks for Steve.

Steve, your code showed me the right direction to move on. Thanks for the cooperation.


:-)
c_sh.
0
 
LVL 4

Expert Comment

by:sdussinger
Comment Utility
Glad I could help...

--Steve
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now