Change Datetime by joda does not work- no error message and Time remains unchanged

149 Views Asked by At

I want to change datetime in my android app using joda Datetime using my below code. steps:

  1. get current date of system
  2. set system hours
  3. 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'
}
1

There are 1 best solutions below

2
On

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.

    Clock offsetClock
            = Clock.offset(Clock.systemDefaultZone(), Duration.ofHours(12));
    
    ZonedDateTime offsetNow = ZonedDateTime.now(offsetClock);
    System.out.println(offsetNow);

When I ran this just now — 6:38 AM in my time zone — I got:

2021-09-11T18:38:00.356242+02:00[Europe/Copenhagen]

If you need an old-fashioned Date object for an API that you cannot afford to upgrade to java.time just now, draw an Instant from the clock and convert:

    Date offsetOldfashionedDate = Date.from(Instant.now(offsetClock));
    System.out.println(offsetOldfashionedDate);

Sat Sep 11 18:38:00 CEST 2021

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.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links