1

I'm a beginner in Scala working on Spark. I have a directory with files with 7 digit Julian date (for 09/30/2016-MM/DD/YYYY, it is 2016274) as their names (these arrive hourly into this folder), which should be converted to a normal date so that I could check with a particular date, decide if I should collect it or not.

I have succeeded to pull the Julian date from the name of the file but failed to change the Julian date to normal date.

Can someone help me here please?

Siva
  • 33
  • 2
  • 6
  • you can check this, it is based on Java, but still you can include this code in Scala http://stackoverflow.com/questions/3017954/convert-a-julian-date-to-regular-calendar-date – Shankar Sep 26 '16 at 09:25
  • @Siva What exactly do you mean by “Julian date”? The meaning is overloaded, used differently by different folks. Please edit your Question to provide exact copies of example strings. – Basil Bourque Sep 26 '16 at 21:07
  • @Basil, I meant a 7 digit date. I edited my question however. – Siva Sep 29 '16 at 09:30
  • @Siva Add example data along with an explanation of how you interpret the value. Explain what you mean by Julian date. That term is used and abused by different people. – Basil Bourque Sep 29 '16 at 15:02

2 Answers2

0

Julian Date becomes a complex concept when you consider the history and reforms associated with it. And that is the problem with using the Julian date implementation that is provided with Java, as we don't know which variation it implements and we may need some other definition of Julian Date.

So... look at this article and decide which version of Julian-Day you require. Once you have decided your version of Julian Date, its pretty easy to map it Gregorian Calendar.

If we consider the most original Julian Date,

  1. Julian Date starts on 12:00, November 24, 4714 BC of Gregorian Calendar.
  2. As you should have noticed, a Julian Day starts on noon ie. 12:00 Hrs.

And now you can implement it like following,

import java.time.{ZonedDateTime, LocalDateTime}

def julianDateToZonedDateTime(julianDate: Double): ZonedDateTime = {
  val julianDayNumber = math.floor(julianDate).toLong
  val julianDayFraction = julianDate - julianDayNumber
  val julianDayFractionToNanoSeconds = math.floor(julianDayFraction * 24 * 60 * 60 * math.pow(10, 9)).toLong

  val bcEraDateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:z:G")
  val julianDateStartDate = ZonedDateTime.parse("4714-11-24 12:00:00:GMT:BC", bcEraDateFormat)

  val zonedDateTime = julianDateStartDate.plusDays(julianDayNumber).plusNanos(julianDayFractionToNanoSeconds)

  zonedDateTime
}

val yourJulianDate: Double = 2456293.520833

val zonedDateTime = julianDateToZonedDateTime(yourJulianDate)
// 2013-01-01T00:29:59.971206486Z[GMT]

This Wikipedia article claims Julian Date - 2456293.520833 to be 2013-01-01T00:30:00[GMT].

Now, either this article has approximated it to closest second or our calculation has diverged by 0.0288 seconds due the divergence between both the calendars.

Finally, you can decide on whether to add some round-off (if you want) or use the exact value.

sarveshseri
  • 13,738
  • 28
  • 47
0

tl;dr

LocalDate.parse( 
    "2016274" , 
    DateTimeFormatter.ofPattern( "uuuuDDD" ) 
)

Day-of-year

I am guessing that 2016274 means a year and a day-of-year. By day-of-year I mean 1-365 or 1-366 for January 1 to December 31. In this case, the two hundred and seventy-fourth day of the year 2016 which is September 30, 2016.

Some people call a day-of-year number “julian” though that seems to me to be a misunderstanding of the astonomers’ Julian Day.

I do not know Scala syntax. But I can show you Java syntax for using the java.time classes built into the Java platform.

The DateTimeFormatter class defines D for day-of-year in a formatting pattern. I will use triple DDD to pad with leading zero for numbers of one or two digits.

String input = "2016274" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuDDD" );
LocalDate ld = LocalDate.parse( input , f );

2016-09-30

Try this code live at IdeOne.com.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154