根據出生日期計算寶寶的年齡,幾歲幾個月幾天!
專案需要網上找了幾個工具類計算都不準確!最後發現使用Calendar計算很方便。
public static String getAge(String date) { String[] data = date.split("-"); if (data.length < 3) return ""; Integer.valueOf(Log.d(TAG, data[0])); Integer.valueOf(Log.d(TAG, data[1])); Integer.valueOf(Log.d(TAG, data[2])); Calendar birthday = new GregorianCalendar(Integer.valueOf(data[0]), Integer.valueOf(data[1]), Integer.valueOf(data[2])); Calendar now = Calendar.getInstance(); int day = now.get(Calendar.DAY_OF_MONTH) - birthday.get(Calendar.DAY_OF_MONTH); int month = now.get(Calendar.MONTH) + 1 - birthday.get(Calendar.MONTH); int year = now.get(Calendar.YEAR) - birthday.get(Calendar.YEAR); //按照減法原理,先day相減,不夠向month借;然後month相減,不夠向year借;最後year相減。 if (day < 0) { month -= 1; now.add(Calendar.MONTH, -1);//得到上一個月,用來得到上個月的天數。 day = day + now.getActualMaximum(Calendar.DAY_OF_MONTH); } if (month < 0) { month = (month + 12) % 12; year--; } System.out.println("年齡:" + year + "歲" + month + "月" + day + "天"); StringBuffer tag = new StringBuffer(); if (year > 0) { tag.append(year + "歲"); } if (month > 0) { tag.append(month + "個月"); } if (day > 0) { tag.append(day + "天"); } if (year == 0 && month == 0 && day == 0) { tag.append("今日出生"); } return String.valueOf(tag); }
相關推薦
根據出生日期計算寶寶的年齡,幾歲幾個月幾天!
專案需要網上找了幾個工具類計算都不準確!最後發現使用Calendar計算很方便。 public static String getAge(String date) { Strin
根據出生日期計算現在年齡
//日期格式化 formatDatetime: function (datetimeString) {
Mysql 根據出生日期計算年齡
format birt div 轉換 to_days ref 社會 emp ear 最近因為業務要求需要根據出生日期計算年齡,在網上查了好多的方法,在這裏總結一下。 網上的計算方法好多都提到了格裏高利歷法,特意去查了下資料,普及點知識。 格裏高利歷是公歷的標準名稱,是一
PHP 根據出生日期計算年齡、生肖、星座
/** * 根據出生日期計算年齡、生肖、星座 * @param string $mydate = "2018-10-23" 日期 * @param string $symbol 符號 * @return $array * */ function birthday($mydate,$sym
sql 根據出生日期計算年齡
如表sample的欄位xm對應姓名,csrq對應出生日期要計算年齡 String sql = "select xm, to_char(sysdate,'YYYY') - to_char(csrq,'YYYY') nl from
php根據出生日期計算年齡函式
今天分享一個小功能函式,根據出生日期來計算當前年齡,精確到天,如果有興趣的可以再深一步判斷精確到時分秒。 程式碼如下: /** * 準備工作完畢 開始計算年齡函式 * @param $birth
【js】根據出生日期,計算週歲年齡
/*根據出生日期算出年齡*/ function jsGetAge(strBirthday) { var returnAge; var strBirthdayArr = strBirthday.split("-"); var
java根據出生日期和當前時間計算年齡
1、獲取系統時間計算年齡 public static int getAgeByBirthday(Date birthday) {Calendar cal = Calendar.getInstance();if (cal.before(birthday)) {throw ne
php根據出生日期獲取年齡,生日資料型別為date型
<span style="font-size:18px;"><strong> function age($birthday){ $birthday = strtotime($birthday);//int strtotime ( string
php根據出生日期獲取年齡
turn logs class php http www. www .html light /** * @param $birthday 出生年月日(1992-1-3) * @return string 年齡 */ function countage($birthd
根據出生日期獲取年齡
話不多說 直接貼程式碼 public function getAge($birthday){ //格式化出生時間年月日 $byear=date('Y',$birthday); $bmonth=date('m',$birthday);
iOS開發 根據某個日期計算是星期幾
在自己的專案中有這個需求,在網上找了下,沒有找到理想的結果,就東拼西湊的把大家的想法,集合在一起,加上自己的想法,實現了這個功能。 溫馨提示:測試了將來某個日期是星期幾,未測試過去的某個日期是星期幾 實現思路 大概的實現方式和大家分享下,有不對的地方
mysql中根據當前日期計算週一和週日,存在的問題
在資料統計分析的時候,有時候需要計算本週的一些資料情況,比如本週的訪問情況,已知條件是當前日期curdate(), 那麼如果需要計算本週的訪問情況的話,那麼就需要計算本週的週一的日期,本週的週日的日期,計算方法如下: select subdate(curdate(),da
Python學習筆記:出生日期轉化為年齡
在資料探勘專案中,有時候個體的出生日期包含資訊量過大,不適合作為一個有效資料進入模型演算法訓練,因此有必要把出生日期轉化為年齡age,age是一個很好的特徵工程指示變數。 import pandas as pd import numpy as np from pandas import Serie
mysql根據出生日期統計各年齡段男女人數
表格名稱 user, 欄位 id, sex ( M 表示男, F表示 女), birthday出生日期, 沒有 age欄位 其中 ROUND(DATEDIFF(CURDATE(), birthday)/365.2422) 是獲得 年齡 CASE WHEN的
根據給定日期計算當天星期(蔡勒公式)
蔡勒公式 #include<iostream> using namespace std; //蔡勒公式 給定年月日 算出此天是星期幾 int weekday(int year,int month,int day) { if(month ==
Java通過出生日期計算星座、(生肖)
Java根據生日得出是十二星座的哪一個星座 方法一: public static String constellation(int month, int day) { String constellation = "";
根據font-size計算rem尺寸,動態縮放頁面
<html><head><title>Rem頁面縮放案例</title><meta name="viewport" content="width=device-width,initial-scale=1.0,minim
java由出生日期算出年齡
String dataOfBirth = "1991-10-21";<span style="white-space:pre"> </span>//出生日期 try { //此處是獲得的年齡 int a
mysql根據出生日期統計年齡段
select nnd as '年齡段',count(*) as '人數' from( select case when (year(now())-year(birthday)-1) + ( DATE_FORMAT(birthday, '%m%d') &