-1

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'
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
BBMM
  • 11
  • 4
  • Yeah, I guess. Calculating the time 12 hours form now in Joda-Time foes not change the system clock of your phone. – Ole V.V. Sep 10 '21 at 11:08
  • Thanks for your link. Actually the link about changing time using "su date" cmd. I have tried to follow the cmd but my app just crash immediately after executing cmd. Note that i have grant superuser access to the app. – BBMM Sep 11 '21 at 01:35
  • 1
    The `java.util` Date-Time API is outdated and error-prone. It is recommended to stop using it completely and switch to the [modern Date-Time API](https://www.oracle.com/technical-resources/articles/java/jf14-Date-Time.html). Also, read this notice at the [home page of **Joda-Time**](https://www.joda.org/joda-time/): *Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.* I highly recommend you use [the solution given by Ole V.V.](https://stackoverflow.com/a/69140048/10819573). – Arvind Kumar Avinash Sep 12 '21 at 09:47
  • Did you see "It's possible and has been done. You need `android.permission.SET_TIME`. Afterward use the `AlarmManager` via `Context.getSystemService(Context.ALARM_SERVICE)` and its method `setTime()`."? – Ryan M Sep 20 '21 at 19:20

1 Answers1

1

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • thanks alot for your advice in detail. I have tried to use Clock.systemdefaultZone(), but my app was just stop working immediately( app crash). Im not sure how to solve this – BBMM Sep 15 '21 at 07:49
  • Sorry to read. I am wondering whether it could be a limitation in desugaring if you’re using that, I am far from sure though, it’s just a thought. I have tested my code on desktop Java only. I am no Android developer myself and have not had an opportunity to test on Android. – Ole V.V. Sep 15 '21 at 07:57