JodaTime

  • Java의 Date 와 Calendar 클래스의 불편함을 해소하기 위해 나온 라이브러리

Joda-Time-Android Github

Android 사용방법

  • Application 클래스 작성하기
    Application 을 상속받는 클래스를 작성한다.
    Application 을 상속받는 클래스의 onCreate에 Jodatime을 사용할 수 있도록 셋팅을 해준다.
    public class CustomApplication extends Application{

@Override
public void onCreate(){
super.onCreate();
JodaTimeAndroid.init(Context);
}
}
  • Manifest에 Application 클래스 등록하기
    <application
android:name="packge.CustomApplication"/>
...
</application>
  • 시간 클래스 가져오기
    //현재 시간 가져오기
DateTime nowTime = DateTime.now();
//특정 시간 가져오기
DateTime time = new DateTime("2017-06-26T09:48:53.000Z");
//UnixTime 으로 시간 가져오기
DateTime unixTime = new DateTime(unixTimeStapm*1000);
  • 시간 가져오기
    //년 가져오기
nowTime.year().get();
nowTime.get(DateTimeFieldType.year());
nowTime.getYear();

//월 가져오기
nowTime.monthOfYear().get();
nowTime.get(DateTimeFieldType.monthOfYear());
nowTime.getMonthOfYear();

//일 가져오기
nowTime.dayOfMonth().get();
nowTime.get(DateTimeFieldType.dayOfMonth());
nowTime.getDayOfMonth();

//시 가져오기
nowTime.hourOfDay().get();
nowTime.get(DateTimeFieldType.hourOfDay());
nowTime.getHourOfDay();

//분 가져오기
nowTime.minuteOfHour().get();
nowTime.get(DateTimeFieldType.minuteOfHour());
nowTime.getMinuteOfHour();

//초 가져오기
nowTime.secondOfMinute().get();
nowTime.get(DateTimeFieldType.secondOfMinute());
nowTime.getSecondOfMinute();

  • 기타 시간 가져오기
    //기원 전 후 가져오기
nowTime.era().get();

//세기 가져오기
nowTime.centuryOfEra().get();

//세기에 포함된 년 가져오기
nowTime.yearOfCentury().get();

//기원 후 년 가져오기
nowTime.yearOfEra().get();

//년도 가져오기
nowTime.weekyear().get();

//해당년도 몇번째 주?
nowTime.weekOfWeekyear().get();

//그 년에 몇번째 일
nowTime.dayOfWeek().get();

//그 주에 몇번째 일
nowTime.dayOfYear().get();

//그 날의 몇 분째
nowTime.minuteOfDay().get();

//그 날의 몇 초째
nowTime.secondOfDay().get();

  • 가져오고 싶은 패턴대로 가져오기
    DateTimeFormat에 Pattern을 원하는 입맛대로 등록(패턴은 아래 표 참조)
    DateTime 객체의 toString메서드에 Format을 넣어 반환 받기
    원하는 Format의 시간을 구할 수 있음.
    ※응용하면 Format으로 년 월 일 시 분 초 (AM/PM) 등도 따로 분리할 수 있음
    DateTimeForamt ff = DateTimeFormat.forPattern("yyyy.MM.dd aa hh:mm:ss");
now.toString(ff);
  • 패턴 표

symbol