2010-06-26 570 views

回答

1

我發現這也許會幫助你:

select 
    case 
     when date(dob, '+' || 
      strftime('%Y', 'now') - strftime('%Y', dob) || 
      ' years') >= date('now') 
     then strftime('%Y', 'now') - strftime('%Y', dob) 
     else strftime('%Y', 'now') - strftime('%Y', dob) - 1 
    end 
    as age 
from t; 

http://www.mail-archive.com/[email protected]/msg20525.html

-1

這是非常難治的sqllite做......所以你更好retrive從數據庫中出生日期ND再加入這個邏輯

private Calendar mConvert_Date_To_Calendar(String dateToConvert) 
{ 
    // TODO Auto-generated method stub 
    try 
    { 
     Calendar calConversion = Calendar.getInstance(); 
     Date date1 = null; 
     try 
     { 
      date1 = new SimpleDateFormat("dd/MM/yy").parse(dateToConvert); 
     } 
     catch (ParseException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     calConversion.setTime(date1); 
     System.out.println("mConvert_Date_To_Calender date1: " + date1 
       + "caleder converted done:" + calConversion.getTime()); 
     return calConversion; 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
     Log.i(TAG, "" + e); 
     return null; 
    } 
} 


private Calendar myCurrentCalender() 
{ 
    try 
    { 
     Calendar myCurrentCal = Calendar.getInstance(); 
     myCurrentCal.set(myCurrentCal.get(Calendar.YEAR), myCurrentCal.get(Calendar.MONTH), 
       myCurrentCal.get(Calendar.DAY_OF_MONTH)); 
     return myCurrentCal; 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
     Log.e(TAG, "Unable to get current calendar!"); 
     return null; 
    } 
} 

public int daysBetween(Date d1, Date d2) 
{ 
    System.out.println("calculated age :" 
      + (((d2.getTime() - d1.getTime())/((24 * 1000 * 60 * 60))/365))); 

    return (int) (((d2.getTime() - d1.getTime())/(24 * 1000 * 60 * 60))/365); 
} 

第一個功能將以日曆格式計算出生日期,第二次將計算當前日期,第三次將查找兩者之間的差異。 這些函數被調用如下所示:

dateOfBirth = mConvert_Date_To_Calendar(age);

currentDate = myCurrentCalender();

candidateAge = daysBetween(dateOfBirth.getTime(),currentDate.getTime());

「候選人年齡」將有年齡。

+0

'日曆dateOfBirth; 日曆currentDate; int candidateAge;' 這些是這些變量的數據類型 – tejasvi 2012-04-27 09:00:39

0

要得到@Bruno Costa的工作答案,我必須在第4行的strftime差異計算周圍添加括號,並將「now」的比較反轉爲小於或等於:據我所知,計算正在進行無論計算日期是在今天之前還是之後。如果今天或更早計算的日期是那麼的生日已是今年年底或者是今天:

select 
    case 
     when date(dob, '+' || 
      (strftime('%Y', 'now') - strftime('%Y', dob)) || 
      ' years') <= date('now') 
     then strftime('%Y', 'now') - strftime('%Y', dob) 
     else strftime('%Y', 'now') - strftime('%Y', dob) - 1 
    end 
    as age 
from t; 

注意,在@Bruno科斯塔的回答解掛是未經檢驗的。

4

只是爲了澄清kai's answer(似乎編輯現在是非常不受歡迎的,及意見具有很強的大小限制和格式問題):

SELECT (strftime('%Y', 'now') - strftime('%Y', Birth_Date)) 
    - (strftime('%m-%d', 'now') < strftime('%m-%d', Birth_Date)); 

假設我們想整年:第一個日曆年生活中,這將是零。

對於第二日曆年,這將取決於日期 - 即它是:

  • strftime('%m-%d', 'now') >= strftime('%m-%d', Birth_Date) [ 「情況下」]
  • 和否則[「case B」];

(我認爲sqlite做兩個日期字符串的字典對比並返回一個整數值。)

對於其他任何日曆年內,這將是年內當前之間的數字 - 即strftime('%Y', 'now') - strftime('%Y', Birth_Date) - 1,再加上與上面相同。

在其它方面,我們減去1從strftime('%Y', 'now') - strftime('%Y', Birth_Date)僅當的相反「情況下」發生 - 即,當且僅當strftime('%m-%d', 'now') < strftime('%m-%d', Birth_Date),因此該公式。

10

您可以轉換日期以浮點數和減...

cast(strftime('%Y.%m%d', 'now') - strftime('%Y.%m%d', dob) as int) 

...其中doba valid SQLite time string

0

另一種解決方法是將年份中的日期%j除以365.0作爲年份的一部分。所以,你最終用:

select strftime('%Y', 'now') + strftime('%j', 'now')/365.2422) - (strftime('%Y', Birth_Date) + strftime('%j', Birth_Date)/365.2422); 

然後,您可以把結果爲一個整數:

select CAST(strftime('%Y', 'now') + strftime('%j', 'now')/365.2422) - (strftime('%Y', Birth_Date) + strftime('%j', Birth_Date)/365.2422) AS INT); 

或使用ROUND()四捨五入的結果:

select round(strftime('%Y', 'now') + strftime('%j', 'now')/365.2422) - (strftime('%Y', Birth_Date) + strftime('%j', Birth_Date)/365.2422)); 

我已經更新計算方法如下,在Robert的註釋中指出,它使用太陽年的正確小數天數。

與其他計算的區別在於,此計算的結果是可以轉換爲整數(以獲得確切的年數)或四捨五入(以獲得近似年數)的十進制數。

對於生日,大概的年數可能並不相關,但對於您購買的東西的年代更有意義。

+0

這是不準確的。您可以通過使用365.2422來稍微改進它,但它不符合年齡的文化概念。 – 2018-02-28 10:16:51

相關問題