While referring to a particular course I came across the following code to retrieve the current day:

int julianStartDay = Time.getJulianDay(System.currentTimeMillis(),   dayTime.gmtoff);
long dateTime;
dateTime = dayTime.setJulianDay(julianStartDay);
day = getReadableDateString(dateTime);

     private String getReadableDateString(long time){
          SimpleDateFormat shortenedDateFormat = new SimpleDateFormat("E MMM d");
            return shortenedDateFormat.format(time);
        }

My doubt is why are we using this julian method instead of just directly extracting the day from the Date class object which gives the current date and time.

Is this Android code? If so, just note that Time is full of problems and thus was deprecated in API level 22 in favour of GregorianCalendar. (Which in turn was replaced by the new date/time package in Java 8)

Yes this is an android code..So you are suggesting that I should use Date class instead of the Julian method here right?

I'm not familiar with the Android version of Java, but in "real" Java almost all the methods in Date were also deprecated around 1997 in favour of GregorianCalendar.
So if your Android version has the new Java 8 date/time package use that, if not use GregorianCalendar for anything date-related.

Using the Gregorian calendar for date computations is just so stupid! Any rational date class will use Julian dates (a floating point representation of date + time) which can be used easily in date arithmetic and provide accurate answers, even accounting for leap years, and if done right, leap seconds. No wonder I hate Java and Android (Dalvik)! I like Android devices (I have 3 Android phones, including a Nexus One), but programming them is brain-dead!

Ignoring personal bias for the moment...
GregorianCalendar's implementation does indeed use an epoch value (millisecs since some arbitrary start point) which is what most people mean when they say "Julian". The rest is a whole bunch of constants and methods that are necessary to support time zones, leap years etc.
So you could use your own epoch value, and write all the code for time zones etc, or you could use GregorianCalendar.

To OP, I guess you may have to go back to your professor and ask why this code is written the way it is. May also ask when was this code written? It looks deprecated...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.