将joda DateTime转换为java.time.LocalDate

时间:2015-12-03 21:25:09

标签: datetime

我有一个简单的问题:如何从joda DateTime转换为LocalDate?

我试过这样但没有成功:

final LocalDate startDate = LocalDate.ofEpochDay(start.getMillis());

2 个答案:

答案 0 :(得分:10)

public class DateUtils {

    /**
     * Convert {@link java.time.LocalDate} to {@link org.joda.time.DateTime}
     */
    public DateTime toDateTime(LocalDate localDate) {
        return new DateTime(DateTimeZone.UTC).withDate(
                localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth()
        ).withTime(0, 0, 0, 0);
    }

    /**
     * Convert {@link org.joda.time.DateTime} to {@link java.time.LocalDate}
     */
    public LocalDate toLocalDate(DateTime dateTime) {
        DateTime dateTimeUtc = dateTime.withZone(DateTimeZone.UTC);
        return LocalDate.of(dateTimeUtc.getYear(), dateTimeUtc.getMonthOfYear(), dateTimeUtc.getDayOfMonth());
    }
}

答案 1 :(得分:-1)

    LocalDateTime demoDateTime = Instant.ofEpochMilli(DateTime.now().getMillis())
            .atZone(ZoneId.systemDefault())
            .toLocalDateTime();
    demoDateTime.toLocalDate();
    demoDateTime.toLocalTime();

用毫秒来转换。