I want to change datetime in my android app using joda Datetime using my below code. steps:
- get current date of system
- set system hours
- get current date and check the result.
There was no error pop-up while running this app. But the time was not change.
Is there anything that im missing? Do I need to get more permission in Manifest.xml?
Note that the app will be run on a rooted emulator.
My file as below:
MainActivity.java;
private void settime1(){
List<String> text = new ArrayList<>();
DateTime now = DateTime.now();
Toast.makeText(MainActivity.this,now.toString(), Toast.LENGTH_LONG).show();
text.add("Now: " + now);
text.add("Now + 12 hours: " + now.plusHours(12));
Date currentTime = Calendar.getInstance().getTime();
Toast.makeText(MainActivity.this, currentTime.toString(), Toast.LENGTH_LONG).show();
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.us_bbteam">
buid.gradle
dependencies {
api 'joda-time:joda-time:2.10.9:no-tzdb'
implementation "androidx.startup:startup-runtime:1.0.0"
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.navigation:navigation-fragment:2.3.5'
implementation 'androidx.navigation:navigation-ui:2.3.5'
implementation 'com.google.android.things:androidthings:1.0'
implementation 'net.danlew:android.joda:2.10.9.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
java.time.Clock
Consider using java.time, the modern Java date and time API, for your date and time work. java.time also offers a
Clock
that you can configure to give you a different time from the real current time.When I ran this just now — 6:38 AM in my time zone — I got:
If you need an old-fashioned
Date
object for an API that you cannot afford to upgrade to java.time just now, draw anInstant
from the clock and convert:Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
org.threeten.bp
with subpackages.Links
java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).