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:
Character | Description | Example |
---|---|---|
G | Era designator | AD |
y | Year in four digits | 2001 |
M | Month in year | July or 07 |
d | Day in month | 10 |
h | Hour in A.M./P.M. (1~12) | 12 |
H | Hour in day (0~23) | 22 |
m | Minute in hour | 30 |
s | Second in minute | 55 |
S | Millisecond | 234 |
E | Day in week | Tuesday |
D | Day in year | 360 |
F | Day of week in month | 2 (second Wed. in July) |
w | Week in year | 40 |
W | Week in month | 1 |
a | A.M./P.M. marker | PM |
k | Hour in day (1~24) | 24 |
K | Hour in A.M./P.M. (0~11) | 10 |
z | Time zone | Eastern Standard Time |
' | Escape for text | Delimiter |
" | Single quote | ` |
Date and Time Conversion Characters:
Character | Description | Example |
---|---|---|
c | Complete date and time | Mon May 04 09:51:52 CDT 2009 |
F | ISO 8601 date | 2004-02-09 |
D | U.S. formatted date (month/day/year) | 02/09/2004 |
T | 24-hour time | 18:05:19 |
r | 12-hour time | 06:05:19 pm |
R | 24-hour time, no seconds | 18:05 |
Y | Four-digit year (with leading zeroes) | 2004 |
y | Last two digits of the year (with leading zeroes) | 04 |
C | First two digits of the year (with leading zeroes) | 20 |
B | Full month name | February |
b | Abbreviated month name | Feb |
m | Two-digit month (with leading zeroes) | 02 |
d | Two-digit day (with leading zeroes) | 03 |
e | Two-digit day (without leading zeroes) | 9 |
A | Full weekday name | Monday |
a | Abbreviated weekday name | Mon |
j | Three-digit day of year (with leading zeroes) | 069 |
H | Two-digit hour (with leading zeroes), between 00 and 23 | 18 |
k | Two-digit hour (without leading zeroes), between 0 and 23 | 18 |
I | Two-digit hour (with leading zeroes), between 01 and 12 | 06 |
l | Two-digit hour (without leading zeroes), between 1 and 12 | 6 |
M | Two-digit minutes (with leading zeroes) | 05 |
S | Two-digit seconds (with leading zeroes) | 19 |
L | Three-digit milliseconds (with leading zeroes) | 047 |
N | Nine-digit nanoseconds (with leading zeroes) | 047000000 |
P | Uppercase morning or afternoon marker | PM |
p | Lowercase morning or afternoon marker | pm |
z | RFC 822 numeric offset from GMT | -0800 |
Z | Time zone | PST |
s | Seconds since 1970-01-01 00:00:00 GMT | 1078884319 |
Q | Milliseconds since 1970-01-01 00:00:00 GMT | 1078884319047 |
There are other useful classes related to Date and time. For more details, you can refer to Java Standard documentation.