Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to change System DATE and TIME ?

Posted on 2001-09-01
5
Medium Priority
?
287 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 444 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

Industry Leaders: 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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

715 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