1

I would like to to substract hour that is displayed in textview1 from hour that is displayed in textview2. Im setting the hour in textview by pressing buttons

    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String sdf = new SimpleDateFormat("HH:mm", Locale.US).format(new Date());
            textView.setText(sdf);
            button1.setVisibility(View.GONE);
            button2.setVisibility(View.VISIBLE);



        }
    });
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String sdf = new SimpleDateFormat("HH:mm", Locale.US).format(new Date());
            textView2.setText(sdf);
            button1.setVisibility(View.VISIBLE);
            button2.setVisibility(View.GONE);

now after clicking button2 i also want to substract hours and set result of substraction in textview3. I tried to get TextViews value and put it to strings, but i guess that's not the correct way to do this. If you guys could show me how to do this, please.

Shizzle
  • 53
  • 1
  • 13
  • 4
    Did you consider using java.time package instead of java.util.Date? – matt Jan 09 '18 at 13:06
  • 1
    Please for your own best search before asking. You will find a good answer much faster that way. This question has been asked and answered several times, so if you don’t like the link I provided, just find a better one. – Ole V.V. Jan 09 '18 at 13:07
  • 1
    Try going through this [link](https://stackoverflow.com/q/1555262/5772683). Make sure you do some search on SO as you'll get to an answer quickly that way – Piyush Bhasin Jan 09 '18 at 13:09
  • What is the desired result if the result of the subtraction is negative? Either because the times have been set in backward order or because midnight has been passed? – Ole V.V. Jan 09 '18 at 13:11
  • I figure that what you really need is a `Duration`. See for example [this answer](https://stackoverflow.com/a/34541535/5772882). – Ole V.V. Jan 09 '18 at 13:13
  • The option with midnight, and negative result will never happend because app will work only between 7:00 - 17:00 – Shizzle Jan 09 '18 at 13:13
  • @OleV.V. This solution is good but my project must be api lvl 18min. – Shizzle Jan 09 '18 at 13:47
  • @Shizzle, check if you can use ThreeTenABP, the Android Backport of the modern Java date and time API that includes `Duration`. I think you can at level 18. Much more in [this question: How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Jan 09 '18 at 13:49
  • If this is about Android, add that tag. – Basil Bourque Jan 09 '18 at 15:53
  • @OleV.V. I tried to use ThreeTenABP, like that: `Instant start = Instant.now()` then, want to put `start` value in textview by `textview.setText(start)` and there's an error "cannot resolve method (org.threeten.bt.Instant)" how can i solve this? Do i even good understand this Instant thing? this gets value from current system time? – Shizzle Jan 09 '18 at 21:09
  • It seems from what you are telling me that you are basically doing things correctly, so it’s hard to tell what detail is wrong. `Instant.now()` gets its values from the system clock, yes. There’s a p, not a t in `org.threeten.**bp**.Instant`, but that was probably just a typo in the comment. – Ole V.V. Jan 09 '18 at 21:19
  • Yes, it was typo. – Shizzle Jan 09 '18 at 21:32
  • I tried the same thing with Joda-Time, same problem. – Shizzle Jan 09 '18 at 21:50

1 Answers1

1

I recommend using the new Java 8 (and 9) libraries.

For illustration, see the method below, doing this pretty simply. You can construct a correct DateTimeFormatter for your exact purposes, but I utilise the standard ISO_TIME one for illustration.

public static LocalTime subtract2Hours(String timeString) {
    return LocalTime.parse(timeString, DateTimeFormatter.ISO_TIME).minusHours(2);
}

You can format a LocalTime to String using LocalTime.format(DateTimeFormatter), of which DateTimeFormatter.ISO_TIME is an example. Simply then provide that String back.

If you were to do the type conversions in one step, you could change the method signature. But if you're only going to use it once, you could probably in-line it all into one long chain using your IDE.

ifly6
  • 5,003
  • 2
  • 24
  • 47
  • 1
    Seems that the OP is doing Android development, so Java 8 Time API can't be used directly yet. This solution will work using [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). – Juan Carlos Mendoza Jan 09 '18 at 13:22
  • Oh, then perhaps try `DateUtils.addHours(date, -2)` from `org.apache.commons.lang3.time.DateUtils` – ifly6 Jan 09 '18 at 13:28
  • OR, based on the Source there, `Calendar c = Calendar.getInstance(); c.setTime(new SimpleDateFormat("HH:MM").parse(timeString)); c.add(Calendar.HOUR, -2); return c.getTime();` – ifly6 Jan 09 '18 at 13:36