-1

I'm quite new to Java and Android Studio, and I'm trying to write a program where when a button is pressed during a specified time period of a day, an event occurs (such as displaying a textview). I am currently trying to display the time for the user to see, but the code does not seem to keep running, and only reflect the time when I last updated my app, and not displaying the time in real time.

Calendar calender = Calendar.getInstance();
System.out.println("Current time => "+calender.getTime());
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String formattedDate = df.format(calender.getTime());
dateandtimeTextView.setText("" +formattedDate);

Is there a way to have the time update continually like a normal clock?

  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Dec 07 '19 at 13:28

2 Answers2

1

Read the clock when you need the time

You need to read the system clock when the user presses the button, not once and for all when your app is launched. So put your call to ZonedDateTime.now()or Calendar.getInstance() or which means you are using for that inside the event handler that handles the button click and displays the time to the user.

The date/time object that you get from your call gets the time of when it was created and keeps that time. This is probably why you saw the time when you last updated your app. To get a new time on the next button press, you need to create a new date/time object.

java.time and ThreeTenABP

BTW the Calendar class is poorly designed and long outdated. Here⁄s the modern way to get the current time:

    DateTimeFormatter formatter = DateTimeFormatter
            .ofLocalizedDateTime(FormatStyle.MEDIUM)
            .withLocale(Locale.getDefault(Locale.Category.FORMAT));

    ZonedDateTime dateTime = ZonedDateTime.now(ZoneId.systemDefault());

    String formattedDate = dateTime.format(formatter);
    System.out.println(formattedDate);

The exact output from my snippet depends not only on the time, on the device locale too. In one instance I got the following output from running in English locale:

Dec 7, 2019 2:38:04 PM

You may create the formatter once and for all, but you need to create dateTime every time. Instead of Calendar and SimpleDateFormat I am using java.time, the modern Java date and time API. It is so much nicer to work with.

To check whether we’re within some specified time period of the day, use dateTime.toLocalTime() and compare the result to LocalTime objects representing start and end of that period. LocalTime has methods isBefore and isAfter. You need one or both of those for the comparisons.

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 use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I tried using the code you provided, but I still face the problem where whenever I launch the app, it displays the time when it launches. I want it to display the time as it passes, like a clock.. Also I was using the calander function because the older posts similar to my questions used that, the posts must have been there for a while now. – confusedcoder Dec 10 '19 at 07:01
  • That has been asked and answered a number of times before. See for example [Android: How do I display an updating clock in a TextView](https://stackoverflow.com/questions/7363119/android-how-do-i-display-an-updating-clock-in-a-textview). Search for more. – Ole V.V. Dec 10 '19 at 20:53
0

Try these two lines to get current date and time.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
String currentDateandTime = sdf.format(new Date());

You also can refer to this link: https://www.tutorialspoint.com/get-current-time-and-date-on-android

baitmbarek
  • 2,440
  • 4
  • 18
  • 26
  • 1
    Thanks for wanting to contribute. Your format pattern is different from the one in the question. Is there any other difference? Do you seriously think that this will solve the problem asked about, displaying the time in real time? Asking because I’m pretty convinced that it will not. – Ole V.V. Dec 07 '19 at 14:15
  • ya ,i want to try to solve your problem,but my mistake on understanding the your question so sorry. – Rahane Akoliya Dec 07 '19 at 14:20
  • You are using terrible date-time classes that were supplanted years ago by the modern *java.time* classes defined in JSR 310. – Basil Bourque Dec 07 '19 at 16:06