Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert joda DateTime to java.time.LocalDate

Tags:

datetime

I would have one just simple question: How to convert from joda DateTime to LocalDate?

I have tried it like this but with no success:

final LocalDate startDate = LocalDate.ofEpochDay(start.getMillis());
like image 341
quma Avatar asked Dec 03 '15 21:12

quma


1 Answers

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());
    }
}
like image 127
user8332750 Avatar answered Oct 27 '22 10:10

user8332750