현재 시각을 컨솔에 보여주는 간단한 애플리케이션의 Java 언어 소스 코드이다.
UTC란 1970년 1월 1일 0시 0분 0초를 기준으로 하여 경과된 초 단위의 총 시간을 의미한다.
* UTC(Universal Time  Coordinated, 협정세계시, 協定世界時)


  1. /*
  2.  *  Filename: TestCTimeApp.java
  3.  *
  4.  *  Compile: javac -d . TestCTimeApp.java
  5.  *
  6.  *  Execute: java TestCTimeApp
  7.  */
  8. import java.util.Calendar;
  9. import java.util.GregorianCalendar;
  10. public class TestCTimeApp {
  11.    static String[] weekNames = {
  12.                   "일", "월", "화", "수", "목", "금", "토"
  13.               };
  14.    public static void main(String[] args) {
  15.         Calendar now = new GregorianCalendar();
  16.         // 1970년 1월 1일 0시 0분 0초부터 시작하여 현재까지의 초
  17.         System.out.println("UTC: " + (now.getTime().getTime() / 1000L) + "초");
  18.         // 현재 시각 표시: 200x년 x월 xx일 (x요일) xx시 xx분 xx초
  19.         System.out.print(now.get(Calendar.YEAR) + "년 ");
  20.         System.out.print((1 + now.get(Calendar.MONTH)) + "월 ");
  21.         System.out.print(now.get(Calendar.DAY_OF_MONTH) + "일 ");
  22.         System.out.print("(" + weekNames[now.get(Calendar.DAY_OF_WEEK)] + "요일) ");
  23.         System.out.print(now.get(Calendar.HOUR_OF_DAY) + "시 ");
  24.         System.out.print(now.get(Calendar.MINUTE) + "분 ");
  25.         System.out.println(now.get(Calendar.SECOND) + "초");
  26.         // 1월 1일은 1, 1월 2일은 2
  27.         System.out.print("올해 몇 번째 날: " + now.get(Calendar.DAY_OF_YEAR) + ", ");
  28.         // 0 이면 서머타임 없음
  29.         System.out.println("서머타임 적용 여부: " + (now.get(Calendar.DST_OFFSET) == 0 ? "안함" : "함"));
  30.     }
  31. }



컴파일> javac -d . TestCTimeApp.java

실행> java TestCTimeApp
UTC: 1206323913초
2008년 3월 24일 (화요일) 10시 58분 33초
올해 몇 번째 날: 84, 서머타임 적용 여부: 안함




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,