0

I need to store a day as an int and then periodically compare it to today as an int.
if(today - trialStartDay > 30)...

I've come across the Julian Day in my research and that seems like a fine standard to use, but I'm not sure how to get it in Android/Java. I'm looking at Calendar/Date/JodaTime and am just getting confused. This seems like it should be very simple. Any advice? Thanks.

NSouth
  • 5,067
  • 7
  • 48
  • 83
  • consider this SO: http://stackoverflow.com/a/32097281/1961779. It gives a simple and elegant solution – Adrian B. Nov 24 '15 at 15:21
  • It seems you just need to determine the difference in days between a given calendar date and today. There are at least 3 external libraries available on Android which can do this, Joda-Time included. For this purpose, you don't need the concept of Julian days although there is support in those external libs, too. Just look/google for duration/period-support, for example Joda-Time-class `Days`. – Meno Hochschild Nov 24 '15 at 17:25

2 Answers2

1

one possibility without using third part libraries:

@Test
public void julianDateTest() {
  Date lDate = new Date();
  Calendar lCal = Calendar.getInstance();
  lCal.setTime(lDate);
  int lYear = lCal.get(Calendar.YEAR);
  int lMonth = lCal.get(Calendar.MONTH) + 1;
  int lDay = lCal.get(Calendar.DATE);
  int a = (14 - lMonth) / 12;
  int y = lYear + 4800 - a;
  int m = lMonth + 12 * a - 3;
  Integer lJulianDate = lDay + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045;
  Assert.assertTrue(lJulianDate>0);
}

for today 24-11-2014 the output would be:

2457351

nano_nano
  • 12,351
  • 8
  • 55
  • 83
  • I'll give that a try. I just wish there was a simpler way. If I'm performing this check often (to see whether a trial has expired), I don't want it to slow anything down. I doubt this would impact performance... – NSouth Nov 24 '15 at 15:12
  • that code isn't slow. the operations are realy basic. I would move that code to an utility class which is static. – nano_nano Nov 24 '15 at 15:16
  • @StefanBeike Thanks Stefan, your peachy answer works for me – ucMedia Oct 15 '19 at 11:42
0

A part of code with multiple examples :

(Ask me in comment if you don't understand)

public static String getFriendlyDayString(Context context, long dateInMillis) {
    // The day string for forecast uses the following logic:
    // For today: "Today, June 8"
    // For tomorrow:  "Tomorrow"
    // For the next 5 days: "Wednesday" (just the day name)
    // For all days after that: "Mon Jun 8"

    Time time = new Time();
    time.setToNow();
    long currentTime = System.currentTimeMillis();
    int julianDay = Time.getJulianDay(dateInMillis, time.gmtoff);
    int currentJulianDay = Time.getJulianDay(currentTime, time.gmtoff);

    // If the date we're building the String for is today's date, the format
    // is "Today, June 24"
    if (julianDay == currentJulianDay) {
        String today = context.getString(R.string.today);
        int formatId = R.string.format_full_friendly_date;
        return String.format(context.getString(
                formatId,
                today,
                getFormattedMonthDay(context, dateInMillis)));
    } else if ( julianDay < currentJulianDay + 7 ) {
        // If the input date is less than a week in the future, just return the day name.
        return getDayName(context, dateInMillis);
    } else {
        // Otherwise, use the form "Mon Jun 3"
        SimpleDateFormat shortenedDateFormat = new SimpleDateFormat("EEE MMM dd");
        return shortenedDateFormat.format(dateInMillis);
    }
compte14031879
  • 1,531
  • 14
  • 27