将date4j DateTime转换为日历或日期

时间:2014-11-10 22:21:10

标签: android datetime calendar

我正在尝试将DateTime从date4j库转换为Calendar实例或Date实例,任何工作。目前我必须将其转换为Calendar实例的代码是

    Calendar Qurancal = Calendar.getInstance();
    Qurancal.set(Calendar.DAY_OF_WEEK, dateTime.getWeekDay());
    Qurancal.set(Calendar.YEAR, dateTime.getYear());
    Qurancal.set(Calendar.MONTH, dateTime.getMonth());
    Qurancal.set(Calendar.DAY_OF_YEAR, dateTime.getDayOfYear());
    Qurancal.set(Calendar.DAY_OF_MONTH, dateTime.getStartOfDay().getDay());
    Qurancal.set(Calendar.HOUR, dateTime.getHour());
    Qurancal.set(Calendar.MINUTE, dateTime.getMinute());
    Qurancal.set(Calendar.SECOND, dateTime.getSecond()); 

但这并不完美,因为目前所有日期都显示相差1或2天

1 个答案:

答案 0 :(得分:0)

固定

从上面的代码中删除此行修复了所有内容

 Qurancal.set(Calendar.DAY_OF_MONTH, dateTime.getStartOfDay().getDay());