Search code examples
javadatetimejulian-date

Java Joda-Time DateTime How to Create a Date from a Julian date


If I execute this statement using Joda-Time:

System.out.println(new DateTime(1387947600*1000L));

It prints out this date:

2013-12-24T23:00:00.000-06:00

What I am trying to create is this exact date but all I have is Julian date format 13359 in EST time zone. What I have tried is appending "20" to my julian date, giving me the String "2013359". Then I use the code:

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyDDD");   
DateTime test1 = formatter.parseDateTime(d2);

When I print out test1, it gives me:

2013-12-25T00:00:00.000-06:00

If I convert test1 to UTC time using

DateTime test2 = test1.withZoneRetainFields(DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC")));

And then print out test2, I get:

2013-12-25T00:00:00.000Z

This is not what I am looking for. How can I go from

"13359" in EST zone

to

2013-12-24T23:00:00.000-06:00

Anybody have any idea? I have spent weeks on this!


Solution

  • If I understand you, what you actually want is 11pm the day before the date you actually have. Note: Day 359 for a non-leap year (2013) is December 25.

    Try this:

    import org.joda.time.LocalDateTime;
    import org.joda.time.format.DateTimeFormat;
    import org.joda.time.format.DateTimeFormatter;
    
    public class JulianDateTester {
    
      public static void main(String[] args) {
        String julianDateString = "13359";
    
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyDDD");
        DateTime lcd = formatter.parseDateTime("20" + julianDateString);
        DateTime lcdWeWant = lcd.minusDays(1).hourOfDay().withMaximumValue();
    
        System.out.println("Input: " + julianDateString + ", formats as '" + lcd.toString());
        System.out.println("The date we want is '" + lcdWeWant.toString() + "'");
        System.out.println("The \"date\" we REALLY want is " + lcdWeWant.getMillis());
    
      }
    
    }
    

    Which outputs:

    Input: 13359, formats as '2013-12-25T00:00:00.000-06:00
    The date we want is '2013-12-24T23:00:00.000-06:00'
    The "date" we REALLY want is 1387947600000
    

    See here for Julian Day calendar from Nasa