JAVA2009. 11. 24. 22:18
Calendar, GregorianCalendar, dateFormat, SimpleDateFormat 을 이용한 시간 출력 Java

2009/01/21 19:41

작성자: 베레(lsj403)

/*
//년 월일 출력 방법.
import java.util.GregorianCalendar; //하위 클레스
import java.util.Calendar; //상위 클레스

class CalendarExample1 {
 public static void main(String[] args) {
  GregorianCalendar calendar = new GregorianCalendar();
  int year = calendar.get(Calendar.YEAR);
  int month = calendar.get(Calendar.MONTH); //0~11까지 나오므로 +1을 해야 1월에서 12 표현
  int date = calendar.get(Calendar.DATE);
  int amPm = calendar.get(Calendar.AM_PM); // 9 의 값을 가짐
  int hour = calendar.get(Calendar.HOUR);
  int min = calendar.get(Calendar.MINUTE);
  int sec = calendar.get(Calendar.SECOND);
  String sAmPm = amPm == Calendar.AM ? "오전" : "오후";
  System.out.printf("%d년 %d월 %d일 %s %d시 %d분 %d초",
    year, month, date, sAmPm, hour, min, sec);
  System.out.println();
 }//main
}//class
*/

/*
//타임존 설정 방법 /영국시로 설정
import java.util.*;

class CalendarExample1 {
 public static void main(String[] args) {
  TimeZone timeZone = TimeZone.getTimeZone("Europe/London"); //타임존클래스를 이용한객체 생성 및 런던의 타임존의 시간으로 설정
  
  calendar.setTimeZone(timeZone); //객체 참조 
  [static TimeZone / getTimeZone(String ID)   Gets the TimeZone for the given ID.]

  int year = calendar.get(Calendar.YEAR);
  int month = calendar.get(Calendar.MONTH); //0~11까지 나오므로 +1을 해야 1월에서 12 표현
  int date = calendar.get(Calendar.DATE);
  int amPm = calendar.get(Calendar.AM_PM); // 9 의 값을 가짐
  int hour = calendar.get(Calendar.HOUR);
  int min = calendar.get(Calendar.MINUTE);
  int sec = calendar.get(Calendar.SECOND);
  String sAmPm = amPm == Calendar.AM ? "오전" : "오후";
  System.out.printf("%d년 %d월 %d일 %s %d시 %d분 %d초",
    year, month, date, sAmPm, hour, min, sec);
  System.out.println();
 }//main
}//class
*/
/*
//DateFormat 클래스와SimpleDateFormat 클래스는 우리가 원하는 형식으로 출력할때 사용
//게시판에서 많이 사용함.
import java.util.*;
import java.text.*; //SimpleDateFormat 클래스가 속하는 패키지

class DateFormatExample1 {
 public static void main(String[] args) {
  GregorianCalendar calendar = new GregorianCalendar();
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy년 MM월 dd일 aa hh시 mm분 ss초");
  String str = dateFormat.format(calendar.getTime()); //GregorianCalendar 객체를 Date객체로 만들어서 format 메소드에 넘겨 줍니다.
  System.out.println(str);
 }//main
}//class
*/

/*
import java.util.*;
import java.text.*;

class DateFormatExample1 {
 public static void main(String[] args) {
  GregorianCalendar calendar = new GregorianCalendar();
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy년 MM월 dd일 aa hh시 mm분 ss초");
  dateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York")); //뉴욕에 해당하는 TimeZone객체를 가지고 SetTimeZone에 메소드를 호출
  String str = dateFormat.format(calendar.getTime()); //GregorianCalendar 객체를 Date객체로 만들어서 format 메소드에 넘겨 줍니다.
  System.out.println(str);
 }//main
}//class
*/


Posted by Tiwaz