System.out.println(json.toString());
System.out.println(json.get("date"));
returns time in epoch time such as: 1609642292
> Task :Program:DateUtils.main()
{"date":1609642292}
1609642292
This is what I'm using to pull the date from the API
import java.io.InputStreamReader;
import java.net.URL;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.Date;
import org.json.JSONException;
import org.json.JSONObject;
public class DateUtils
{
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
// InputStream is = new URL(url).openStream();
try (var is = new URL(url).openStream()) {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
}
}
public static void main(String[] args) throws IOException, JSONException {
JSONObject json = readJsonFromUrl("https://Time.xyz/api/date"); //Don't want to post real API
System.out.println(json.toString());
System.out.println(json.get("date"));
}
}
In my other java file I'm trying to do something like
Calendar expiry = Calendar.getInstance();
expiry.set(2021,1,31,0,0) //When my program expires:year, month, date, hour, min
Calendar now = DateUtils.getAtomicTime();
//where DateUtils.getAtomicTime comes from this class that pulls current time from the National Institute of Standards and Technology
//https://www.rgagnon.com/javadetails/java-0589.html
if (now.after(expiry)) {
shutdown()
}else{
startProgram()
}
}
How can I change Calendar now - DateUtils.getatomicTime() to this new API
My problems: I don't know how to use what I have to check for the time and refer to it.
Like it prints the time properly, but now how do I use that println jsontostring and then use that to add it to that code slightly above to compare my Set Expiration Date with API date.
Please give me some advice. Thank you.