이번달의 첫날과 마지막날 알아내기  

 

5월의 시작하는 날과 마지막날을 알아내는 것을 가정해본다.

var startDayOfMay = new Date(2012, 4, 1);

var endDayOfMay = new Date(2012, 5, 0);

console.log("startDayOfMonth : " + startDayOfMay + ", endDayOfMonth : " + endDayOfMay);


>> 결과

 startDayOfMonth : Tue May 01 2012 00:00:00 GMT+0900 (KST), endDayOfMonth : Thu May 31 2012 00:00:00 GMT+0900 (KST)



   생성한 Date 객체에서 월(Month)의 값을 꺼내면 어떤 값이 나올까?  
 

var today = new Date();

console.log("Date : " today + ", Month : " +today.getMonth());

>> 결과

Date : Tue May 15 2012 16:26:13 GMT+0900 (KST), Month : 4


출력된 Date 문자열은 분명 5월(May)를 나타내고 있는데, Date 객체에서 추출한 Month는 4가 나타났다.

이렇게 나타나는 이유는 자바스크립트의 Date 객체에서 월Month 가 0부터 시작하기 때문이다.

0 = 1월, 1 = 2월, 2 = 3월, ..., 10 = 11월, 11 = 12월.


확인하는 방법은 간단하다.

var modifyDate = new Date(2012, 0, 1); 
console.log("Date : " modifyDate + ", Month : " + modifyDate.getMonth());
>> 결과 
Date : Sun Jan 01 2012 00:00:00 GMT+0900 (KST), Month : 0

var modifyDate = new Date(2012, 11, 1); 
console.log("Date : " modifyDate + ", Month : " + modifyDate.getMonth());
>> ru
Date : Sat Dec 01 2012 00:00:00 GMT+0900 (KST), Month : 11


 

 왜 0부터 시작할까?  

 

참고 : http://www.evotech.net/blog/2007/07/javascript-date-object/

위의 사이트로 가보면, 마지막 Date.prototype... 코드 부분에 월Month을 배열Array로 처리하는 것을 확인할 수 있는데, 컴퓨터에서 사용하는 배열의 순서Index는 0부터 시작한다는 것을 생각해보면 어느정도 유추해볼 수 있을 듯 하다. getMonth()를 하면 객체가 속한 달의 순서Index값을 내놓는 것이라 생각해볼 수 있을 것 같다.


저작자 표시
Posted by 허니몬


위 현상은 IE에서는 나타나지 않는다. 크롬브라우저에서만 나타난다.

 

 문제가 생기는 이유 : 

 

응답헤더에 ContentType 이외에 파일정보를 Header에 추가하는 코드 때문에 나타는 증상이다.  Internet Explore에서는 다운로드에 대한 파일정보를 헤더에 넣어줘도 이상이 없었지만, 크롬에서는 그것을 취약점 공격을 위한 수단으로 판단한 것으로 보인다. 

예제 코드 : 

HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(StrutsStatics.HTTP_RESPONSE); response.setContentType("application/octet-stream; charset=utf-8"); try { //다운로드되는 파일의 정보를 헤더에 추가하는 코드 response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(item.getName(), "utf-8") + ";"); } catch (UnsupportedEncodingException ignored) { // do nothing }


 

 해결방법 : application/x-download 를 활용

 

해결 코드 :

HttpServletResponse response = (HttpServletResponse) ActionContext.getContext() .get(StrutsStatics.HTTP_RESPONSE); response.setContentType("application/x-download"); try { HttpServletRequest request = (HttpServletRequest) ActionContext.getContext() .get(StrutsStatics.HTTP_REQUEST); LOG.debug("User-Agent : " + request.getHeader("User-Agent")); if(request.getHeader("User-Agent").contains("Firefox")) { response.setHeader("Content-Disposition", "attachment;filename=\"" + new String(item.getName().getBytes("UTF-8"), "ISO-8859-1") + "\";"); } else { response.setHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(item.getName(), "utf-8") + "\";"); } } catch (UnsupportedEncodingException ignored) { // do nothing } response.setHeader("Content-Transfer-Encoding", "binary"); LOG.debug("Content-Disposition : " + response.getHeader("Content-Disposition")); LOG.debug("Content Type : " + response.getContentType()); File file = new File(item.getPath()); FileInputStream fileIn = null; ServletOutputStream outstream = null; try { fileIn = new FileInputStream(file); outstream = response.getOutputStream(); byte[] outputByte = new byte[8192]; while (fileIn.read(outputByte, 0, 8192) != -1) { outstream.write(outputByte, 0, 8192); } outstream.flush(); } catch (FileNotFoundException e) { LOG.error(e); } catch (IOException e) { LOG.error(e); } finally { try { fileIn.close(); } catch (IOException e) { } try { outstream.close(); } catch (IOException e) { } }



인터넷을 뒤져봤지만, 쉼표(,)를 다른 문자로 대체하면 된다는 해결책 외에는 딱히 방법이 없었다.

구글을 돌아디나다가 검색해서 찾은 해결책을 기록한다.

출처 : http://stackoverflow.com/questions/2405568/java-webapp-adding-a-content-disposition-header-to-force-browsers-save-as-beh

익스플로러8, 크롬(18.0.1025.151), 파이어폭스(11)에서 정상 동작합니다.

저작자 표시
Posted by 허니몬

IP주소 문자열을 가지는 목록 List<String> ipList 내부에 있는 IP주소를  정렬하는 방법

사용하는 클래스 java.util.Collections(class), java.util.Comparator(Interface)

정렬하는데 사용식 : 

Collections.sort(ipList, new IpListSortByIp());


IpListSortByIp.java

public class IpListSortByIp implements Comparator<String> {

        @Override
        public int compare(String o1, String o2) {
            try {
                if (InetAddress.getByName(o1).hashCode() > InetAddress.getByName(o2).hashCode()) {
                    return 1;
                } else if (InetAddress.getByName(o1).hashCode() < InetAddress.getByName(o2).hashCode()) {
                    return -1;
                }
            }
            catch (UnknownHostException e) {
                //Exception 처리
            }
            return 0;
        }
    }


간단히 정의를 한다면,

문자열 IP주소를 java.net.InetAddress 객체로 변형하여 그 객체가 가지고 있는 HashCode를 비교하여 정렬하는 방식이다. 

저작자 표시
Posted by 허니몬

  인증되지 않은 애플리케이션을 실행해서 '안드로이드 마켓(최근 'play store'로 명칭 변경)'으로 이동하여 동일한 애플리케이션을 업데이트하려고 하면 위와 같은 팝업이 뜬다. 제거를 눌러서 폰에 설치되어 있는 애플리케이션을 제거하고, 설치하면 간단하게 처리가 된다.


위와 같은 상황이 뜬 것은, 안드로이드앱 업데이트 확인절차(안드로이드 마켓 말고 서비스를 제공하는 운영서버에 등록된 앱버전을 확인)를 걸쳐서 마켓으로 이동을 했을 때 나타나는 현상이다.

현재 구글마켓에서 등록된 앱버전을 확인할 수 있는 방법은 없어보인다.

Posted by 허니몬

JavaScript sort 참고 : http://www.w3schools.com/jsref/jsref_sort.asp
  
var testArray = [
    {"id" : 1, "total" : 3}, {"id" : 2, "total" : 20}, {"id" : 3, "total" : 12}, {"id" : 4, "total" : 9}, {"id" : 3, "total" : 24}
];
function custonSort(a, b) {
  if(a.total == b.total){ return 0} return  a.total > b.total ? 1 : -1;
}
testArray.sort(custonSort);
console.log(testArray);
<< 정렬 결과 >>
[
Object
  1. id1
  2. total3
  3. __proto__Object
,
Object
  1. id4
  2. total9
  3. __proto__Object
,
Object
  1. id3
  2. total12
  3. __proto__Object
,
Object
  1. id2
  2. total20
  3. __proto__Object
,
Object
  1. id3
  2. total24
  3. __proto__Object
]

간단하게 예제를 만들어봤다.

참고사이트 : http://stackoverflow.com/questions/881510/json-sorting-question

난 전혀 다른 곳을 파고 있었던 것이다. 


저작자 표시
Posted by 허니몬
이전버튼 1 2 3 4 5 ... 37 이전버튼