주간 베스트 월간 베스트 3개월 베스트 베스트 게시물
꽃배달 한국, 중국 전지역배송

[java] 날자관련 소스

신규가입 | 2010.02.20 17:53:21 댓글: 0 조회: 4591 추천: 0
분류웹 프로그래밍 https://life.moyiza.kr/itstudy/1760243

// 시스템의 밀리초 구하기(1000 1)        

long currentTime = System.currentTimeMillis();

System.out.println(currentTime.toString());  

 

 

//현재 시각을 가져오기.  

Date today = new Date(); 

System.out.println(today);

 

 

 

//포맷을 지정해서 날짜 구하기                                 

SimpleDateFormat formatter =                                  

     new SimpleDateFormat("yyyy.MM.dd HH:mm:ss",Locale.KOREA);

Date currentTime = new Date();                                

String dTime = formatter.format(currentTime);                 

System.out.println(dTime);          

 

 

 

//Date Calendar로 맵핑하기        

Date d = new Date();               

Calendar c = Calendar.getInstance();

c.setTime(d);                      

 

 

//날짜를 n일만큼 이동시키기 1          

Date date = new Date();                

long currDate = date.getTime();         

currDate = currDate + (24*60*60*1000)* n

 

 

 

//날짜를 n일만큼 이동시키기2                            

//오늘 날짜를 기준으로                                  

Calendar cal = Calendar.getInstance ( );                

//2달 전....                                             

cal.add ( cal.MONTH, -2 );                              

//2일 전....                                            

cal.add ( cal.DAY_OF_MONTH, -2 );                       

//2년 후                                                

cal.add ( Calendar.YEAR, 2 );                           

//출력하기                                              

System.out.println ( cal.get ( cal.YEAR ) );            

System.out.println ( cal.get ( cal.MONTH ) + 1 );       

System.out.println ( cal.get ( Calendar.DAY_OF_MONTH ) );

 

 

 

//해당하는 달의 마지막 일 구하기1                  

GregorianCalendar today = new GregorianCalendar ( );

int endDay = today.getActualMaximum                

                ((today.DAY_OF_MONTH));             

System.out.println(endDay);                        

 

 

//해당하는 달의 마지막 일 구하기2                  

Calendar cal = Calendar.getInstance ( );           

cal.set ( 2008, 0, 1 );//월은 0부터 시작           

int endDay = cal.getActualMaximum ( Calendar.DATE );

System.out.println(endDay);                        

 

 

//요일 구하기                                             

Calendar cal= Calendar.getInstance ( );                   

//day_of_week 1이면 일요일, 2이면 월요일.... 7이면 토요일

int day_of_week = cal.get ( Calendar.DAY_OF_WEEK );       

 

 

 

날짜와 시간이 유효한지 검사하기

//날짜 유효 검사1                                    

try {                                                

    DateFormat df =                                  

        DateFormat.getDateInstance (DateFormat.SHORT);

    df.setLenient ( false );                         

    Date dt2 = df.parse ( dt );                      

}catch (ParseException e){                           

                                                     

}catch (IllegalArgumentException e) {                

                                                     

}                                                    

 

 

 

//날짜 유효 검사2                                          

String result = "";                                         

dateValue = dateValue.replaceAll("-", "");                 

SimpleDateFormat formatter =                               

     new SimpleDateFormat("yyyyMM", java.util.Locale.KOREA);

 

                                                 

//일자, 시각해석을 엄밀하게 실시할지 설정함     

//true일 경우는 엄밀하지 않는 해석, 디폴트      

formatter.setLenient ( false );                 

Date formatDate = null;                         

try {                                           

        formatDate = formatter.parse(dateValue);

}catch (java.text.ParseException e){            

        return "ERROR";                         

}                                               

 

 

 

//두 날짜 비교하기                                

Date date1 = new Date();                          

Date date2 = new Date();                          

.                                                 

.                                                 

long ldate1 = date1.getTime();                     

long ldate2 = date2.getTime();                    

                                                  

long diff;                                        

diff = ldate2 - ldate1;                           

//결과값이 몇시간, 몇일 차이나는지 확인하기 위해선.

Calendar cal= Calendar.getInstance ( );           

cal.setTimeInMillis(diff);   

 

p.s : 컴퓨터 정리하다가 우연히 찾은 메모를 올려봅니다.

제가 왕초보일때 회사에 한 선배가 저한테 직접 작성해준 기초적이면서도 아주 유용한것들입니다.

추천 (0) 선물 (0명)
IP: ♡.122.♡.2
3,006 개의 글이 있습니다.
제목 글쓴이 날짜 조회
관리자
2003-09-20
11856
관리자
2003-09-20
11405
관리자
2003-09-20
20538
지구인
2010-08-27
19337
지구인
2009-09-07
13658
SOLIDH
2010-01-29
15615
엔죠라이프
2004-10-07
16711
자유의검
2010-03-15
2330
자유의검
2010-03-14
2545
자유의검
2010-03-14
1618
자유의검
2010-03-14
1401
자유의검
2010-03-14
1322
자유의검
2010-03-14
1371
엔죠라이프
2010-03-11
1107
커피한잔76
2010-03-09
3993
rblueberry
2010-03-08
2342
2010-03-01
3449
2010-02-28
1869
2010-02-28
1497
2010-02-27
1342
2010-02-27
1357
2010-02-27
1366
2010-02-27
1424
2010-02-27
3588
엔죠라이프
2010-02-26
2285
신규가입
2010-02-25
9902
2010-02-20
2180
신규가입
2010-02-20
4591
모이자 모바일