Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 is a timestamp object.

Is it possible to do that?

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
488 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

Alternatively you can use joda-time:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...