How to convert Date (no time) to Unix Timetamp in Joda Time 2.4

edited August 2014 in How To...

I am trying to convert a ISO8601 year'W'week to Unix TimeStamp and have been able to at least convert year'W'week to week starting date and week end date, but haven not figured out how to convert date in to Unix Timetamp. I am using Joda 2.4.

Here is my code:

int year = 2012;
int weekNumber = 8;
LocalDate weekStartDate = new LocalDate().withWeekOfWeekyear(weekNumber).withYear(year);
LocalDate weekEndDate = new LocalDate().withWeekOfWeekyear(weekNumber + 1).withYear(year);
println (weekStartDate+" - "+weekEndDate);

Can anyone help me out?

Answers

  • edited August 2014 Answer ✓

    LocalDate isn't the right object for this task: it doesn't represent a moment in time. After some searches, I had a better result with DateTime:

    import org.joda.time.DateTime;
    
    int year = 2012;
    int weekNumber = 8;
    DateTime weekStartDate = new DateTime().withWeekOfWeekyear(weekNumber).withYear(year);
    DateTime weekEndDate = new DateTime().withWeekOfWeekyear(weekNumber + 1).withYear(year);
    println(weekStartDate+" - "+weekEndDate);
    println(weekStartDate.getMillis());
    exit();
    
  • Thanks for the help!

Sign In or Register to comment.