-1

I have the following JSON entries in a .json file that I am reading in.

There are numerous JSON entries and each hold the same format. The initial two values specify the date and month:

[["1","12","06:26","08:03","12:09","13:46","14:17","16:05","17:42"]

So the initial "1" and "12" will equate to the 1st of December.

Can someone help me write a method where as soon as the JSON is read in, the device will use the device day/month to check which JSON array to display on the app screen?

Adam
  • 203
  • 1
  • 14
  • https://stackoverflow.com/help/how-to-ask – Chefes Feb 15 '18 at 22:17
  • Well, this is the worst way to receive data – Cătălin Florescu Feb 16 '18 at 09:18
  • https://github.com/AtifAbbAsi19/Date-Picker-Example-/blob/master/app/src/main/java/com/muhammadatif/datepicker/dateexample/utilities/Tools.java have a look at this example.! – Atif AbbAsi Feb 16 '18 at 12:16
  • 1
    @AtifAbbAsi Thanks. That example is using the outmoded classes `Calendar` and `Date` and, worse, the notoriously troublesome `SimpleDateFormat`. I recommend avoiding those and going for [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) instead. – Ole V.V. Feb 16 '18 at 12:19

2 Answers2

2

I’m no expert in JSON but I do know about days and months. So here’s how to check whether those two values from the JSON array denote today’s date according to your device:

    MonthDay today = MonthDay.now(ZoneId.systemDefault());
    try {
        if (MonthDay.of(Integer.parseInt("12"), Integer.parseInt("1")).equals(today)) {
            System.out.println("This is the array to display today");
        }
    } catch (NumberFormatException nfe) {
        System.out.println("Day or month from array was not a number: " + nfe.getMessage());
    }

I am using and recommending java.time, the modern Java date and time API.

ZoneId.systemDefault() is my attempt to get the device’s time zone setting. Beware, however, that I only get the JVM’s time zone setting, it’s not always the same. For example, the JVM setting may have been changed by other parts of your program or by another program running in the same JVM.

How to get those strings from each JSON array also depends on what library you are using for parsing your JSON.

Question: Can I use java.time on Android?

Yes, you can use java.time on Android. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new 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
0

First of all, to retrieve the current day and month, I would use the following code:

 Calendar instance = Calendar.getInstance();
 currentMonth = instance.get(Calendar.MONTH);
 currentDay = instance.get(Calendar.DAY);

 int month=currentMonth+1;

After this, you need to get the data out of the json string by creating a json array, then a json object and filter the specifically needed string (day and month) out of the string.

JSONArray arr = new JSONArray(result);
JSONObject jObj = arr.getJSONObject(0);
String day = jObj.getString("Day");
String month = jObj.getString("Month");

Now, put all dates you have in arrays. And then, simple compare all of the given dates with the current one.

Problem is, that you do not have prefixes like that:

[{"Date":"2012-1-4T00:00:00","keywords":null, ... }]

These are needed to tell where to extract the string from, so you should consider updating your JSON Output.

ProgFroz
  • 197
  • 4
  • 17
  • The `Calendar` class is so long outmoded. Please don’t teach the young ones to use it. Instead use [`java.time`, the modern Java date and time API (since 2014)](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Feb 16 '18 at 09:00