// 시스템의 밀리초 구하기(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 : 컴퓨터 정리하다가 우연히 찾은 메모를 올려봅니다.
제가 왕초보일때 회사에 한 선배가 저한테 직접 작성해준 기초적이면서도 아주 유용한것들입니다.
제목 | 글쓴이 | 날짜 | 조회 |
---|---|---|---|
2003-09-20 |
11856 |
||
2003-09-20 |
11405 |
||
2003-09-20 |
20538 |
||
지구인 |
2010-08-27 |
19337 |
|
지구인 |
2009-09-07 |
13658 |
|
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 |