如何从Timestamp(长格式)中检索日月和年 [英] how to retrieve day month and year from Timestamp(long format)

查看:346
本文介绍了如何从Timestamp(长格式)中检索日月和年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个时间戳对象中检索日期和月份长的数字:

I need to retrieve day year and month from a timestamp object as long numbers:

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
    }

born_date 是一个时间戳对象。

是否可以这样做?

提前感谢。 p>

Thanks in advance.

推荐答案

long timestamp = bornDate.getTime();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timestamp);
return cal.get(Calendar.YEAR);

您需要的每个属性都有日历字段。

There are calendar fields for each property you need.

或者您可以使用 joda-time

DateTime dateTime = new DateTime(bornDate.getDate());
return datetime.getYear();

这篇关于如何从Timestamp(长格式)中检索日月和年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆