Mark M.
Mark M.

Reputation: 23

Julian to Gregorian date conversion in Scala

I need to convert Julian date (YYYYDDD) to Gregorian date (YYYYMMDD) in Scala. I got this far:

import java.text.SimpleDateFormat;
val format1 = new java.text.SimpleDateFormat("yyyyddd")
println(format1.parse("2018022"))

The result is: Mon Jan 22 00:00:00 CST 2018

I need help to get output in "YYYYMMDD" format

Upvotes: 2

Views: 988

Answers (2)

TechGuide4U
TechGuide4U

Reputation: 1

You can use below functions to convert the same. I'am using the same function on my website.

function julian2d($indate) {
    $year = substr($indate,0,2);
    $day = ltrim(substr($indate,strlen($indate)-3,3),'0');    /* Day part with leading zeroes stripped    */
    if ($year == 70 && $day == 1) $outdate = 1;
    else if ($year == 70) $outdate = ($day-1)*24*60*60;
    else if ($year >= 0 && $year <= 99 && $day > 0 && $day <= 366) {
        $outdate = strtotime('01-Jan-' . $year);        /* Date on Jan 1 of the year    */
        if ($outdate >= 0) {
            $outdate = $outdate + ($day-1)*24*60*60;
        } else $outdate = FALSE;
    } else $outdate = FALSE;
    return $outdate;
}

Upvotes: 0

jwvh
jwvh

Reputation: 51271

See if this does it for you.

import java.text.SimpleDateFormat

val format1 = new SimpleDateFormat("yyyyddd")
new SimpleDateFormat("yyyyMMdd").format(format1.parse("2018022"))
//res0: String = 20180122

Or this, which demonstrates the relationships a little better.

val jDate: String = "2018022"
val gDate: String = new SimpleDateFormat("yyyyMMdd").format(
                      new SimpleDateFormat("yyyyddd").parse(jDate))

Upvotes: 2

Related Questions