Solved

How to change System DATE and TIME ?

Posted on 2001-09-01
5
278 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
ID: 6447458
Listening...
0
 

Expert Comment

by:muhotrepius
ID: 6447530
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
ID: 6447744
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
ID: 6449775
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
ID: 6451140
Glad I could help...

--Steve
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

863 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

20 Experts available now in Live!

Get 1:1 Help Now