你不是有Birthday类了吗,比如有个Person的对象P
PgetBirthday()getYear();
你在定义Person类时必须定义birthday的get()方法,在定义Birthday时必须定义get()方法,前提你的属性都是private的
如果是公有的那好说了PBirthdayyear;
没有写toString方法,自己去写吧,即然你定义了一个Birthday类,那么它就跟String,int一样可以拿出来当类型使用的啊
public Person(){
private String name;
private Birthday birthday;
Person(String name,Birthday birthday){
thisname=name;
thisbirthday=birthday;}
public int getName(){
return thisname;
}
public void setName(String name){
thisname=name;
return name;
}
public int getBirthday(){
return thisbirthday;
}
public void setName(Birthday birthday){
thisbirthday=birthday;
return birthday;
}
}
可以使用javautilCalendar来取得相关日期信息,下面给个示例供参考:
Java代码:
import javatextParseException;
import javatextSimpleDateFormat;
import javautilCalendar;
public class DateDemo {
static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String[] args) throws ParseException {
String date = "2011-04-25 22:28:30";
Systemoutprintln(date);
Systemoutprintln("年份:" + getYear(date));
Systemoutprintln("月份:" + getMonth(date));
Systemoutprintln("日期:" + getDay(date));
Systemoutprintln("月初日期是: " + getMinMonthDate(date));
Systemoutprintln("月末日期是: " + getMaxMonthDate(date));
}
/
获取日期年份
@param date
@return
@throws ParseException
/
public static int getYear(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
return calendarget(CalendarYEAR);
}
/
获取日期月份
@param date
@return
@throws ParseException
/
public static int getMonth(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
return (calendarget(CalendarMONTH) + 1);
}
/
获取日期号
@param date
@return
@throws ParseException
/
public static int getDay(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
return calendarget(CalendarDAY_OF_MONTH);
}
/
获取月份起始日期
@param date
@return
@throws ParseException
/
public static String getMinMonthDate(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
calendarset(CalendarDAY_OF_MONTH, calendargetActualMinimum(CalendarDAY_OF_MONTH));
return dateFormatformat(calendargetTime());
}
/
获取月份最后日期
@param date
@return
@throws ParseException
/
public static String getMaxMonthDate(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
calendarset(CalendarDAY_OF_MONTH, calendargetActualMaximum(CalendarDAY_OF_MONTH));
return dateFormatformat(calendargetTime());
}
}
以上就是关于java中如何获取生日中的年份全部的内容,包括:java中如何获取生日中的年份、如何用java去 *** 作“日期”函数,取得月初和月末、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)