개성있는 개발자 되기

Date.toGMTString() @Deprecated Alternatives 본문

Language/Java

Date.toGMTString() @Deprecated Alternatives

정몽실이 2020. 3. 17. 13:26

Date 형을 GMT 형식의 String 으로 바꾸는 메소드인 toGMTString()이 @Deprecated 되서 쓸수가 없다.

대체 방법은 SimpleDateFormat을 쓰는 것이다.

 

SimpleDateFormat sdf = new SimpleDateFormat();
sdf.setTimeZone(new SimpleTimeZone(0, "GMT"));
sdf.applyPattern("dd MMM yyyy HH:mm:ss z");
Date testDate = new Date();

// Print string using recommended method
System.out.println(sdf.format(testDate));
// Print string using deprecated method
System.out.println(testDate.toGMTString());

 


 

Date 형 데이터를 Json으로 송수신하기 위해 여러가지 Format을 알아봣다.

"\"\\/Date(1335205592410)\\/\""         .NET JavaScriptSerializer
"\"\\/Date(1335205592410-0500)\\/\""    .NET DataContractJsonSerializer
"2012-04-23T18:25:43.511Z"              JavaScript built-in JSON object
"2012-04-21T18:25:43-05:00"             ISO 8601

 

JSON 자체는 Date가 어떻게 기술되어야 하는지에 대한 정의는 없다. 하지만 자바스크립트는 있다.

 

자바스크립트는 Date 형을 ISO8601 형으로 쓴다.

 

2012-04-23T18:25:43.511Z

 

위와 같은 ISO8601형 Date 문자열은 자바스크립트에서 파싱이 바로 되기때문에, 이러한 형식을 권고한다.

const json = JSON.stringify(new Date());
const parsed = JSON.parse(json); //2015-10-26T07:46:36.611Z
const date = new Date(parsed); // Back to date object

타 파트와 데이터를 송수신하기 위해 굳이 GMT String을 쓰지는 않아도 된다고 생각해서, Date의 toString()으로 리턴되는 값 자체를 제공하고자 했다.

 

Date 객체의 toString()

Date d = new Date();
String s = d.toString();

toString()으로 변환된 String을 다시 Date형으로 변환

String param = "Tue Mar 17 11:20:15 KST 2020";

DateFormat parser = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.ENGLISH);
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

 

 

자바 Date Format 관련

 

http://www.java2s.com/Tutorials/Java/Java_Format/0030__Java_Date_Format_Symbol.htm

 

 

Java Format - Java Date Format Pattern

Java Format - Java Date Format Pattern SimpleDateFormat Date and time formats are specified by date and time pattern strings. Within format strings, unquoted letters from 'A' to 'Z' and from 'a' to 'z' are treated as format letters representing the compone

www.java2s.com

 

Comments