我需要从时间戳对象中检索日期和月份作为长数字:
public long getTimeStampDay()
{
String iDate = new SimpleDateFormat("dd/MM/yyyy")
.format(new Date(born_date.getDate()));
.....
return day; //just the day
}
public long getTimeStampMonth()
{
String iDate = new SimpleDateFormat("dd/MM/yyyy")
.format(new Date(born_date.getDate()));
.....
return month; //just month
}
public long getTimeStampYear()
{
String iDate = new SimpleDateFormat("dd/MM/yyyy")
.format(new Date(born_date.getDate()));
.....
return year; //just year
}
Run Code Online (Sandbox Code Playgroud)
born_date 是一个时间戳对象.
有可能吗?
提前致谢.
Boz*_*zho 53
long timestamp = bornDate.getTime();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timestamp);
return cal.get(Calendar.YEAR);
Run Code Online (Sandbox Code Playgroud)
您需要的每个属性都有日历字段.
或者你可以使用joda-time:
DateTime dateTime = new DateTime(bornDate.getDate());
return datetime.getYear();
Run Code Online (Sandbox Code Playgroud)
对这个广为接受的线程进行一点更新,但现在已经有点过时了。
过去,Java 8 引入了日期和时间 API,这使得提取更加干净。
我们假设bornDate是 的实例LocalDateTime。
bornDate.getDayOfMonth(); // extracts day of month
bornDate.getMonth(); // extracts month
bornDate.getYear(); // extracts year
Run Code Online (Sandbox Code Playgroud)
Oracle 参考:Java SE 8 日期和时间
| 归档时间: |
|
| 查看次数: |
62871 次 |
| 最近记录: |