-1

So I have this code...

SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:minutes");


poaTimeDataText.setText(timeFormatter.format(getIntent().getLongExtra(
                ShiftLogsFragment.EXTRA_POA_TIME, 0)));

Now instead of displaying for example 02:40, I want it to display like 2 hours 40 minutes. However, I have tried, changing the pattern to "HH hours mm minutes" but the compiler does not like that. How can I get the desired result?

EDIT: Would be nice if it accounts for singular or plural like hours or hour, minute (if 0 or 1) or minutes (if 2 or more)

Harry
  • 7
  • 4
  • Put `minutes` in single quotes. – shmosel Apr 01 '19 at 22:14
  • Use `new SimpleDateFormat("H 'hours' m 'minutes'")`, but that won't change the words to singular, e.g. you might get `1 hours 1 minutes` instead of `1 hour 1 minute`, and it won't eliminate 0 values, so you might get `3 hours 0 minutes` or `0 hours 42 minutes` – Andreas Apr 01 '19 at 22:22
  • It seems you are using `SimpleDateFormat` for formatting a duraion, an amount of time. Don’t do that, it’s not what it’s for, and it will give you all sorts of trouble. In fact, consider not using it at all since it is notoriously troublesome and also long outdated. Possible duplicate of [Formatting a Duration in Java 8 / jsr310](https://stackoverflow.com/questions/20827047/formatting-a-duration-in-java-8-jsr310) and/or [Why can't I get a duration in minutes or hours in java.time?](https://stackoverflow.com/questions/24491243/why-cant-i-get-a-duration-in-minutes-or-hours-in-java-time) – Ole V.V. Apr 02 '19 at 02:45

2 Answers2

0

I would suggest using strings.xml with SimpleDateFormat. This will give you more flexibility down the road with translations.

First, add this to your strings.xml.

<string name="hours">%1$s Hours %2$s Minutes</string>

Now, when you fetch this String, you can insert your values.

Long hours = 3;
Long minutes = 55;

SimpleDateFormat hourFormatter = new SimpleDateFormat("HH");
SimpleDateFormat minuteFormatter = new SimpleDateFormat("mm");

String text = getString(R.string.hours, 
      hourFormattter.format(hours), 
      minuteFormatter.format(minutes)); // 03 Hours 55 Minutes

poaTimeDataText.setText(text);  

Down the road, if you decide to add translations, change your wording, you can just add more to your strings.xml instead of updating this code.

advice
  • 5,778
  • 10
  • 33
  • 60
0

Create a String variable and assign your string to it like this

String myString = timeFormatter.format(getIntent().getLongExtra(ShiftLogsFragment.EXTRA_POA_TIME, 0))

Then split your string like this

String[] separated = myString.split(":");

Now you have each variable alone

separated[0]; // this will contain "hours"
separated[1]; // this will contain "minutes"

Define two variables

String hours = "";
String minutes = "";

Make some tests

if (separated[0].equals("0") || separated[0].equals("1")) {
   hours = "hour";
} else {
   hours = "hours";
}

if (separated[1].equals("0") || separated[1].equals("1")) {
   minutes = "minute";
} else {
   minutes = "minutes";
}

Finally set this strings to your textView like this

poaTimeDataText.setText(separated[0] + hours + separated[1] + minutes);
Amine
  • 2,241
  • 2
  • 19
  • 41