简体   繁体   English

Scala 中的儒略到公历日期转换

[英]Julian to Gregorian date conversion in Scala

I need to convert Julian date (YYYYDDD) to Gregorian date (YYYYMMDD) in Scala.我需要在 Scala 中将儒略日期(YYYYDDD)转换为公历日期(YYYYMMDD)。 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结果是:Mon Jan 22 00:00:00 CST 2018

I need help to get output in "YYYYMMDD" format我需要帮助以“YYYYMMDD”格式输出

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))

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM