[code=Java] import java.util.Date; import java.text.SimpleDateFormat;public class Test16 { int bornYear = 0; int bornMonth = 0; int bornDay = 0;
public Test16( int bornYear, int bornMonth, int bornDay ) { this.bornYear = bornYear; this.bornMonth = bornMonth; this.bornDay = bornDay; }
public void countAge() { //get current date Date currentDate = new Date();
//format date form SimpleDateFormat m_oFormatOnlyYear = new SimpleDateFormat( "yyyy" ); SimpleDateFormat m_oFormatOnlyMonth = new SimpleDateFormat( "MM" ); SimpleDateFormat m_oFormatOnlyDay = new SimpleDateFormat( "dd" );
//current year String strOnlyYear = m_oFormatOnlyYear.format( currentDate ); int intOnlyYear = Integer.parseInt( strOnlyYear );
//TODO... //use intOnlyYear, intOnlyMonth, intOnlyDay that get from system //then process them with bornYear, bornMonth, bornDay that the //property this class has }
public static void main( String[] args ) {
} } code]
import java.util.Date; import java.text.SimpleDateFormat;public class Test16 { int bornYear = 0; int bornMonth = 0; int bornDay = 0;
public Test16( int bornYear, int bornMonth, int bornDay ) { this.bornYear = bornYear; this.bornMonth = bornMonth; this.bornDay = bornDay; }
public void countAge() { //get current date Date currentDate = new Date();
//format date form SimpleDateFormat m_oFormatOnlyYear = new SimpleDateFormat( "yyyy" ); SimpleDateFormat m_oFormatOnlyMonth = new SimpleDateFormat( "MM" ); SimpleDateFormat m_oFormatOnlyDay = new SimpleDateFormat( "dd" );
//current year String strOnlyYear = m_oFormatOnlyYear.format( currentDate ); int intOnlyYear = Integer.parseInt( strOnlyYear );
//TODO... //use intOnlyYear, intOnlyMonth, intOnlyDay that get from system //then process them with bornYear, bornMonth, bornDay that the //property this class has }
出生日期:2006-3-23
请看上边2个日期数字,可以看出
将他们的year month day互相减去计算应该可以
其中细节注意的是:该年2月是否是闰年,月份是否超出出生月份,日期是否超出出生日期
int startMonth = Calendar.FEBRUARY;
int startDay = 23;
Calendar start = Calendar.getInstance();
start.set(Calendar.YEAR, startYear);
start.set(Calendar.MONTH, startMonth);
start.set(Calendar.DAY_OF_MONTH, startDay);
Calendar now = Calendar.getInstance();
int nowYear = now.get(Calendar.YEAR);
int nowMonth = now.get(Calendar.MONTH);
int nowDay = now.get(Calendar.DAY_OF_MONTH);
int ageYear = nowYear - startYear;
int ageMonth = nowMonth - startMonth;
int ageDay = nowDay - startDay;
Calendar age = Calendar.getInstance();
age.set(Calendar.YEAR, ageYear);
age.set(Calendar.MONTH, ageMonth);
age.set(Calendar.DAY_OF_MONTH, ageDay);
StringBuilder builder = new StringBuilder();
builder.append((age.get(Calendar.YEAR)) + " sui ");
builder.append((age.get(Calendar.MONTH) + " yue "));
builder.append(age.get(Calendar.DATE) + " tian");
System.out.println(builder.toString());
Calendar cd = Calendar.getInstance();
cd.set(cd.get(Calendar.YEAR) - 2006, cd.get(Calendar.MONTH) - 1, cd
.get(Calendar.DAY_OF_MONTH) - 23);
System.out.println(String.valueOf(cd.get(Calendar.YEAR)) + "岁"
+ String.valueOf(cd.get(Calendar.MONTH)) + "月"
+ String.valueOf(cd.get(Calendar.DAY_OF_MONTH)) + "天");
}
import java.util.Date;
import java.text.SimpleDateFormat;public class Test16
{
int bornYear = 0;
int bornMonth = 0;
int bornDay = 0;
public Test16( int bornYear, int bornMonth, int bornDay )
{
this.bornYear = bornYear;
this.bornMonth = bornMonth;
this.bornDay = bornDay;
}
public void countAge()
{
//get current date
Date currentDate = new Date();
//format date form
SimpleDateFormat m_oFormatOnlyYear = new SimpleDateFormat( "yyyy" );
SimpleDateFormat m_oFormatOnlyMonth = new SimpleDateFormat( "MM" );
SimpleDateFormat m_oFormatOnlyDay = new SimpleDateFormat( "dd" );
//current year
String strOnlyYear = m_oFormatOnlyYear.format( currentDate );
int intOnlyYear = Integer.parseInt( strOnlyYear );
//current month
String strOnlyMonth = m_oFormatOnlyMonth.format( currentDate );
int intOnlyMonth = Integer.parseInt( strOnlyMonth );
//current day
String strOnlyDay = m_oFormatOnlyDay.format( currentDate );
int intOnlyDay = Integer.parseInt( strOnlyDay );
//System.out.println("OnlyYear:"+intOnlyYear);
//System.out.println("OnlyMonth:"+intOnlyMonth);
//System.out.println("OnlyMonth:"+intOnlyDay);
//TODO...
//use intOnlyYear, intOnlyMonth, intOnlyDay that get from system
//then process them with bornYear, bornMonth, bornDay that the
//property this class has
}
public static void main( String[] args )
{
}
}
code]
import java.util.Date;
import java.text.SimpleDateFormat;public class Test16
{
int bornYear = 0;
int bornMonth = 0;
int bornDay = 0;
public Test16( int bornYear, int bornMonth, int bornDay )
{
this.bornYear = bornYear;
this.bornMonth = bornMonth;
this.bornDay = bornDay;
}
public void countAge()
{
//get current date
Date currentDate = new Date();
//format date form
SimpleDateFormat m_oFormatOnlyYear = new SimpleDateFormat( "yyyy" );
SimpleDateFormat m_oFormatOnlyMonth = new SimpleDateFormat( "MM" );
SimpleDateFormat m_oFormatOnlyDay = new SimpleDateFormat( "dd" );
//current year
String strOnlyYear = m_oFormatOnlyYear.format( currentDate );
int intOnlyYear = Integer.parseInt( strOnlyYear );
//current month
String strOnlyMonth = m_oFormatOnlyMonth.format( currentDate );
int intOnlyMonth = Integer.parseInt( strOnlyMonth );
//current day
String strOnlyDay = m_oFormatOnlyDay.format( currentDate );
int intOnlyDay = Integer.parseInt( strOnlyDay );
//System.out.println("OnlyYear:"+intOnlyYear);
//System.out.println("OnlyMonth:"+intOnlyMonth);
//System.out.println("OnlyMonth:"+intOnlyDay);
//TODO...
//use intOnlyYear, intOnlyMonth, intOnlyDay that get from system
//then process them with bornYear, bornMonth, bornDay that the
//property this class has
}
public static void main( String[] args )
{
}
}