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

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

java - how to retrieve day month and year from Timestamp(long format)

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

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

1 Answer

0 votes
by (71.8m points)
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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...