MyCode
MyCode

Reputation: 289

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.

Upvotes: 0

Views: 1365

Answers (2)

WIll
WIll

Reputation: 45

The reason you are having this problem is because you are not setting new Date().setTime(65*1000); to a value.

do this:

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

        temp.setTime(65*1000);

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

Upvotes: 1

Matt Clark
Matt Clark

Reputation: 28609

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.

Upvotes: 2

Related Questions