관리 메뉴

HAMA 블로그

Java Time/ Date / Calendar example 본문

Java

Java Time/ Date / Calendar example

[하마] 이승현 (wowlsh93@gmail.com) 2015. 5. 13. 10:41

// 간단한 현재 날짜/시간 얻기


import java.util.Date;

  

public class DateDemo {

   public static void main(String args[]) {

       

   Date date = new Date();  

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

   }

}


Mon May 04 09:51:52 CDT 2009




// 날짜/시간  데이터 포맷팅 


import java.util.*;

import java.text.*;


public class DateDemo {

   public static void main(String args[]) {


      Date dNow = new Date( );

      SimpleDateFormat ft =  new SimpleDateFormat (

"E yyyy.MM.dd 'at' hh:mm:ss a zzz");


      System.out.println("Current Date: " + ft.format(dNow));

   }

}


Current Date: Sun 2004.07.18 at 04:14:09 PM PDT




// Printf 를 이용한 데이터 포맷팅 !


import java.util.Date;


public class DateDemo {


  public static void main(String args[]) {

     // Instantiate a Date object

     Date date = new Date();


     // display time and date using toString()

     String str = String.format("Current Date/Time : %tc", date );


     System.out.printf(str);

  }

}

Current Date/Time : Sat Dec 15 16:37:57 MST 2012




//Calendar 를 통한  시간 비교 ( 현재 시간이  과거에 기록된 시간에  비해 몇초 지났나? ) 

public boolean isAdjustedTarget(int reforceTimeSec){
if(isAdjusted == false)  // 수요조절된적이 없으면 조절대상이 됨. 
return true;
Calendar currentDate = Calendar.getInstance();
System.out.println(currentDate.get(Calendar.YEAR));
System.out.println(currentDate.get(Calendar.MONTH) + 1);
System.out.println(currentDate.get(Calendar.DATE));
System.out.println(currentDate.get(Calendar.HOUR));
System.out.println(currentDate.get(Calendar.MINUTE));
System.out.println(currentDate.get(Calendar.SECOND));
if((currentDate.getTimeInMillis() - endAdjustedTime.getTimeInMillis())  < (reforceTimeSec * 1000))
return false;
return true;
}






Simple DateFormat format codes:


CharacterDescriptionExample
GEra designatorAD
yYear in four digits2001
MMonth in yearJuly or 07
dDay in month10
hHour in A.M./P.M. (1~12)12
HHour in day (0~23)22
mMinute in hour30
sSecond in minute55
SMillisecond234
EDay in weekTuesday
DDay in year360
FDay of week in month2 (second Wed. in July)
wWeek in year40
WWeek in month1
aA.M./P.M. markerPM
kHour in day (1~24)24
KHour in A.M./P.M. (0~11)10
zTime zoneEastern Standard Time
'Escape for textDelimiter
"Single quote`



Date and Time Conversion Characters:

CharacterDescriptionExample
cComplete date and timeMon May 04 09:51:52 CDT 2009
FISO 8601 date2004-02-09
DU.S. formatted date (month/day/year)02/09/2004
T24-hour time18:05:19
r12-hour time06:05:19 pm
R24-hour time, no seconds18:05
YFour-digit year (with leading zeroes)2004
yLast two digits of the year (with leading zeroes)04
CFirst two digits of the year (with leading zeroes)20
BFull month nameFebruary
bAbbreviated month nameFeb
mTwo-digit month (with leading zeroes)02
dTwo-digit day (with leading zeroes)03
eTwo-digit day (without leading zeroes)9
AFull weekday nameMonday
aAbbreviated weekday nameMon
jThree-digit day of year (with leading zeroes)069
HTwo-digit hour (with leading zeroes), between 00 and 2318
kTwo-digit hour (without leading zeroes), between 0 and 2318
ITwo-digit hour (with leading zeroes), between 01 and 1206
lTwo-digit hour (without leading zeroes), between 1 and 126
MTwo-digit minutes (with leading zeroes)05
STwo-digit seconds (with leading zeroes)19
LThree-digit milliseconds (with leading zeroes)047
NNine-digit nanoseconds (with leading zeroes)047000000
PUppercase morning or afternoon markerPM
pLowercase morning or afternoon markerpm
zRFC 822 numeric offset from GMT-0800
ZTime zonePST
sSeconds since 1970-01-01 00:00:00 GMT1078884319
QMilliseconds since 1970-01-01 00:00:00 GMT1078884319047


There are other useful classes related to Date and time. For more details, you can refer to Java Standard documentation.

'Java' 카테고리의 다른 글

Java ArrayList 객체 정렬  (0) 2015.05.14
자바 스케쥴링 & 타이머 방법들  (0) 2015.05.14
Java Time/ Date / Calendar example  (0) 2015.05.13
Java Time,Data 클래스의 문제점과 JAVA 8  (0) 2015.05.13
자바 List 순회  (0) 2015.05.12
자바에서 Map 순회  (0) 2015.05.12
0 Comments
댓글쓰기 폼