如何在Android中获得朱利安日(或类似)?

时间:2015-11-24 14:55:12

标签: java android jodatime android-calendar

我需要将一天存储为int,然后定期将其与今天的int进行比较 if(today - trialStartDay > 30)...

我在研究中遇到了朱利安日,这似乎是一个很好的标准,但我不知道如何在Android / Java中获得它。我正在看日历/日期/ JodaTime,我只是感到困惑。这看起来应该很简单。有什么建议?感谢。

2 个答案:

答案 0 :(得分:0)

不使用第三方库的一种可能性:

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

今天24-11-2014输出将是:

  

2457351

答案 1 :(得分:0)

包含多个示例的代码的一部分:

(如果你不明白,请在评论中提问我)

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