Solved

How to change System DATE and TIME ?

Posted on 2001-09-01
5
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

623 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