Joda DateTime Project 4/4

Joda DateTime Project

Interval

1/1000초 단위의 Instant 들 간의 시간 차이. 물론, TimeZone 기반입니다.

Duration

TimeZone을 가지지 않는 시간의 길이 (1/1000)단위
Interval로 부터 정보를 얻을 수 있다.

Period

TimeZone을 가지지 않는 시간의 길이 (1/1000)단위
특정 필드로 표현된다.
필드 (Period, Years, Months, Weeks, Days, Hours, Minutes, Seconds)

사용예)

기본 생성
DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
DateTime end = new DateTime(2005, 1, 1, 0, 0, 0, 0);

// duration in ms between two instants
Duration dur = new Duration(start, end);

// calc will be the same as end
DateTime calc = start.plus(dur);

Periods를 사용하는 방법
/** Compare to Othe Time */
logger.info("\n\n ========== Expression of Intervals ===========");
DateTime aTime = new DateTime("2005-11-05");
DateTime bTime = new DateTime();
logger.info("aTime is {}", aTime);  // return  aTime is 2005-11-05T00:00:00.000+09:00
logger.info("bTime is {}", bTime);  // return bTime is 2012-08-01T15:32:20.699+09:00

int dayIntervals = Days.daysBetween(aTime, bTime).getDays();
int weekIntervals = Weeks.weeksBetween(aTime, bTime).getWeeks();
int monthIntervals = Months.monthsBetween(aTime, bTime).getMonths();

logger.info("day Intervals is {}", dayIntervals);  // return day Intervals is 2461
logger.info("week Intervals is {}", weekIntervals);  // return week Intervals is 351
logger.info("month Intervals is {}", monthIntervals);  // return month Intervals is 80

Interval interval = new Interval(aTime, bTime);
logger.info("duration {}, period {} from Instance" ,interval.toDuration().getStandardDays(),
interval.toPeriod(PeriodType.days()).getDays() );

// return duration 2461, period 2461 from Instance

서로다른 TimeZone의 경우는?
/** Expression of Zone */
logger.info("\n\n ========== Expression of Zone ===========");

DateTime minTimeOfDay = dt.hourOfDay().withMinimumValue()
 .minuteOfDay().withMinimumValue()
 .secondOfDay().withMinimumValue()
 .millisOfDay().withMinimumValue();

logger.info("Min Time Of Day is {}",  minTimeOfDay);  
// return Min Time Of Day is 2012-08-01T00:00:00.000+09:00

DateTimeZone dtz = DateTimeZone.forID("Europe/London");
minTimeOfDay.toDateTime(dtz);
DateTime minTimeOfDayInNewWork = minTimeOfDay.toDateTime(dtz);

logger.info("Min Time Of Day in Europe/London Zone is {}",  minTimeOfDayInNewWork);  
// return Min Time Of Day in Europe/London Zone is 2012-07-31T16:00:00.000+01:00

dtz = DateTimeZone.forID("Asia/Taipei");
minTimeOfDay.toDateTime(dtz);

DateTime minTimeOfDayInTaiPei = minTimeOfDay.toDateTime(dtz);
logger.info("Min Time Of Day in Asia/Taipei Zone is {}",  minTimeOfDayInTaiPei);  
// return Min Time Of Day in Asia/Taipei Zone is 2012-07-31T23:00:00.000+08:00
logger.info("Diffences In Another timezone {}",  Days.daysBetween(minTimeOfDayInNewWork, minTimeOfDayInTaiPei).getDays() );  // return 0;

Referred from http://joda-time.sourceforge.net 버젼 2.1

댓글

가장 많이 본 글