자바(Java)
자바 서버로부터 날짜 읽어서 사용
xemaker
2023. 1. 5. 17:19
package navermap;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class NowDateFromServer {
public final static String DUE_STR="20230204";
public final static String USER_ID="ksd";
public final static String GET_SET_DATE_URL="http://localhost/navermap/getSettingDate.php";
public final static String GET_DATE_URL="http://localhost/navermap/getNowDate.php";
public final static String LOG_URL="http://localhost/navermap/insertLog.php";
public static boolean isBefore() throws Exception {
boolean rst=false;
//String nowStr="20230105";
URL url = new URL(GET_DATE_URL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
// optional default is GET
con.setRequestProperty("User-Agent", "Mozilla/5.0");
// add request header
int responseCode = con.getResponseCode();
System.out.println("HTTP 응답 코드 : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
} in.close(); // print result
System.out.println("HTTP body : " + response.toString());
JSONParser jsonParser = new JSONParser();
String jsonInfo=response.toString();
System.out.println("jsonInfo="+jsonInfo);
JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonInfo);
System.out.println("jsonObject="+jsonObject);
String nowStr = (String) jsonObject.get("nowDate");
System.out.println("nowStr="+nowStr);
//String dueStr="20230204";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate nowDate=LocalDate.parse(nowStr, formatter);
LocalDate dueDate=LocalDate.parse(DUE_STR, formatter);
System.out.println(nowDate);
System.out.println(dueDate);
rst=nowDate.isBefore(dueDate);
//System.out.println(rst);
return rst;
}
public static boolean isBeforeByUserId(String user_id) throws Exception {
boolean rst=false;
//String nowStr="20230105";
String settingUrl = GET_SET_DATE_URL+"?user_id="+user_id;
URL url = new URL(settingUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
// optional default is GET
con.setRequestProperty("User-Agent", "Mozilla/5.0");
// add request header
int responseCode = con.getResponseCode();
System.out.println("HTTP 응답 코드 : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
} in.close(); // print result
System.out.println("HTTP body : " + response.toString());
JSONParser jsonParser = new JSONParser();
String jsonInfo=response.toString();
System.out.println("jsonInfo="+jsonInfo);
JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonInfo);
System.out.println("jsonObject="+jsonObject);
String nowStr = (String) jsonObject.get("nowDate");
System.out.println("nowStr="+nowStr);
//String dueStr="20230204";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate nowDate=LocalDate.parse(nowStr, formatter);
LocalDate dueDate=LocalDate.parse(DUE_STR, formatter);
System.out.println(nowDate);
System.out.println(dueDate);
rst=nowDate.isBefore(dueDate);
//System.out.println(rst);
return rst;
}
public static void log() throws Exception {
String logUrl=LOG_URL+"?user_id="+USER_ID;
System.out.println("logUrl="+logUrl);
URL url = new URL(logUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = con.getResponseCode();
System.out.println("HTTP 응답 코드 : " + responseCode);
}
public static void main(String[] args) throws Exception {
boolean b = NowDateFromServer.isBefore();
boolean b2 = NowDateFromServer.isBeforeByUserId("aa");
System.out.println("isBefore="+b);
System.out.println("isBeforeByUserId="+b2);
log();
}
}