Search code examples
javadatejavafxtimezoneddatetime

How do I convert yyyy DDD HH:mm:ss into a ZonedDateTime>


I've been working on some code for a while and I can't seem to figure this bit out. I've got 5 spinners for each field of year, day of year, hour, minute, and second. My goal is to take those 5 parameters and use them to create a ZonedDateTime that can be sent out, but I have no clue where to go with this because of the difficulties of using the day of year format (which I can't change).


Solution

  • Given

    int yr = ...;
    int dayOfYear = ... ;
    int hour = ... ;
    int minute = ... ;
    int second =  ... ;
    

    (which you can get from your spinners)

    You can do

    LocalDate date = Year.of(yr).atDay(dayOfYear);
    LocalTime time = LocalTime.of(hour, minute, second);
    ZonedDateTime zdt = ZonedDateTime.of(date, time, ZoneId.systemDefault());