Search code examples
androiddatemilliseconds

Android Date SetTime not working?


I am trying to write a code to change the current milliseconds time. So , i am displaying the current setTime and then setting my custom time to Date , setTime method but , it's not changing.

MyCode :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    TextView txt1 = (TextView) findViewById(R.id.txt1);
    TextView txt2 = (TextView) findViewById(R.id.txt2);

    txt1.setTextSize(20);

    txt1.setTextColor(Color.BLACK);

    txt2.setTextSize(20);

    txt2.setTextColor(Color.BLACK);


    txt1.setText("Time 1 : "+new Date().getTime());

            new Date().setTime(65*1000);

    txt2.setText("Time 2 : "+new Date().getTime());

}

enter image description here

So , these are the above code and screenshots of my issue. In this case both Time 1 & Time 2 are remaining same but i think the Time 2 should change to 65 millisecond , but i don't know how to do it?

Please suggest me some solution.


Solution

  • Every time you call it, you are creating a new instance of Date(), try this:

    Date d = new Date();
    
    d.getTime();
    
    d.setTime(someTime);
    
    d.getTime();
    

    Every time you call new Date() you are creating a new instance with the current system time.