-2

I need to display the arabic time in the below format:

If the time falls under the time :

  • 6:00 am to 11:59 am then use صباحًا (a.m)
  • 12:00 to 18:00 ظهرًا (noon)
  • 18:00 24: 00 مساءً (evening)
  • 24:00 منتصف الليل (midnight)

Is there a way to display time in this format using java API ?

I am currently getting this : م 04:00

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Coder17
  • 767
  • 4
  • 11
  • 30
  • 4
    Hint: you are expected to do prior research. Like: have you seen https://stackoverflow.com/questions/19923498/java-date-time-in-arabic ... esp. the second answer? SO is not a replacement for you using a search engine first. – GhostCat May 14 '18 at 10:15

1 Answers1

1

First put your texts into an array:

private static final String[] arabicTimeOfDay = { "منتصف الليل", "صباحًا", "ظهرًا", "مساءً" };

Then use your array like this:

    LocalTime time = LocalTime.now(ZoneId.of("Europe/Kiev"));
    int quarterOfDay = time.getHour() / 6;
    System.out.println("" + time + ' ' + arabicTimeOfDay[quarterOfDay]);

Running just now (13:48 in Kyiv) it printed:

13:48:55.590 ظهرًا

Dividing the hour of day by 6 as I do is probably a bit rigid and coarse-grained. You may want to set up cut-over times instead and check against each to find the proper interval and then use the text for that interval. This would allow to change the evening to begin an hour earlier or later, for example. Use LocalTime.isBefore and LocalTime.isAfter for comparing times of day.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161