0

Good morning, i would want to know how to do something like this in android.

if(6PM are already passed){
 //do somethin
}else{
//somethin else
}

Thanks in advance.

Chaosfire
  • 4,818
  • 4
  • 8
  • 23

4 Answers4

3

You could do something like this:

  1. Modern java.time API
LocalTime now = LocalTime.now(ZoneId.of("required timezone"));
if (now.getHour() >= 18) {
    //do something
} else {
    //do something else
}
  1. Legacy Calendar
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Europe/Rome"));
int hour = calendar.get(Calendar.HOUR_OF_DAY);

Keep in mind, using the legacy API is cumbersome, error prone and generally discouraged, it's just that bad. I would recommend to research how to do desugaring.

Chaosfire
  • 4,818
  • 4
  • 8
  • 23
  • thanks for the reply but it give me this error Failed resolution of: Ljava/time/ZoneId; My timezone is LocalTime now = LocalTime.now(ZoneId.of("Europe/Rome")); – AlbertoDev2 Sep 10 '22 at 09:08
  • 1
    @AlbertoDev2 Probably your API version is too low, check [this](https://stackoverflow.com/questions/52510370/java-lang-noclassdeffounderror-failed-resolution-of-ljava-time-localdate-erro) and also [this](https://stackoverflow.com/questions/55550936/call-requires-api-level-26-current-min-is-23-java-time-instantnow). – Chaosfire Sep 10 '22 at 09:16
  • yeah i know. but i need it to API 23 because my phone has android 6 is there another way to do it for lower API versions..? – AlbertoDev2 Sep 10 '22 at 09:19
  • 1
    You can use `java.time` on lower API version through [desugaring](https://developer.android.com/studio/write/java8-support-table). – Chaosfire Sep 10 '22 at 09:31
  • do you have an example? – AlbertoDev2 Sep 10 '22 at 09:33
  • 1
    @AlbertoDev2 I can't help with that, sorry. I don't do android stuff, so my knowledge about desugaring is to the extent - i know it exists. Check the update how to get hour using the legacy API, but keep in mind, it's just bad, it's better to do research on desugaring, android docs should have examples. – Chaosfire Sep 10 '22 at 10:00
1

To use java.time in low Android versions easily, you can use

1. ThreeTenABP

Put this in your build.gradle:

implementation 'com.jakewharton.threetenabp:threetenabp:1.4.1'

Then in your Application class:

@Override
public void onCreate() {
  super.onCreate();
  AndroidThreeTen.init(this);
}

That's all, now use java.time normally.

2. Or, as their github README suggests, use the new desugaring library

cmak
  • 576
  • 1
  • 4
  • 15
0

if other solutions didn't work for you. You can try this

import java.util.Calendar;
import java.util.Date;

Date currentTime = Calendar.getInstance().getTime();
if(currentTime.getHours() >= 18){
     // Above 6pm
} else {
     // Below 6pm
}
Kamal
  • 335
  • 3
  • 10
0

Use this function, you can extend it with dates if you want after:

public static void checkTimings() {
    String pattern = "HH:mm";
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);

    try {
        Date date1 = sdf.parse(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + ":" + Calendar.getInstance().get(Calendar.MINUTE));
        Date date2 = sdf.parse("18:00");
        if (date1.before(date2)){
            //before 6pm
        } else {
            //after 6pm
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
}
Mokhtar Abdelhalim
  • 232
  • 1
  • 3
  • 9
  • 2
    Consider not using the old and troublesome classes `SimpleDateFormat`, `Date` and `Calendar`. Use java.time, it is so much nicer to work with. See the answer by Chaosfire. And if for API levels under 26 additionally use core library desugaring to make java.time available to you. – Ole V.V. Sep 10 '22 at 12:00
  • no problem, If you want to check date, you need just to change the pattern and get current date – Mokhtar Abdelhalim Sep 10 '22 at 14:52