Joda DateTime Project 2/4

Key concepts

Instant

Joda Time에서 가장 많이 언급되는 개념.
1970-01-01(UTC) 로 부터 점진적으로 늘어나는 시간의
1/1000초(milliseconds) 단위의 숫자로 표현된다.

이 millisencond 기반의 Instant는 그 어떤 시간형태로도 변환이 가능하다.
DateTime 클래스의 Method를 통해서 Date와 Time의 모든 형태(년,월,주,일,분..등)의 정보를 얻을 수 있다.

DateTime dt = new DateTime();  // current time
int month = dt.getMonth();     // gets the current month
int month = dt.month().get();  // alternative way to get value
String monthStr = dt.month().getAsText();  // gets the month name

Interface ReadableInstant

Implements classes


  • DateTime - thread-safety
  • DateMidnight - start 00:00:00.000
  • MutableDateTime - not Thread-safety

DateTime 생성하는 방법들
// setup date object for midday on Christmas 2004 (ISO year 2004)
DateTime dt = new DateTime(2004, 12, 25, 12, 0, 0, 0);
// get the year, 2004
int year = dt.getYear();
// get the day of week index 1 (Monday) to 7 (Sunday)
int dow = dt.getDayOfWeek();
// get the text, such as 'Tuesday'
String dowStr = dt.dayOfWeek().getAsText();

JDK와 연동이 된다. 달(Month)의 경우 0-11 쓰는 JDK와 달리 DateTime 은 1-12를 사용한다.
// construct DateTime from JDK Date
Date jdkDate = new Date();
DateTime dt = new DateTime(jdkDate);

// construct Calendar from DateTime (could also construct a Date)
GregorianCalendar cal = dt.toGregorianCalendar();

/** Basic of date. */
logger.info("\n\n ========== Basic of date ===========");
DateTime dt = new DateTime();
/** Now is 2012-07-30T13:11:22.805+09:00*/
DateTimeUtils.setCurrentMillisFixed(dt.now().getMillis());
logger.info("Now {}", dt.now());

/** This day is 30 */
logger.info("This day is {}", dt.getDayOfMonth() );

/** This Month is 7 */
logger.info("This Month is {}", dt.getMonthOfYear());

/** End of year 12 */
logger.info("End of year {}", dt.getYearOfCentury());

/** End of year 2012 */
logger.info("End of year {}", dt.getYearOfEra());
Referred from http://joda-time.sourceforge.net 버젼 2.1

댓글

가장 많이 본 글