4

This mainly applies to android, but could be used in Java. I have these listeners:

int year, month, day, hour, minute;
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                year = year;
                month = monthOfYear;
                day = dayOfMonth;
            }
        };
// the callback received when the user "sets" the time in the dialog
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
    new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            hour = hourOfDay;
            minute = minute;
        }
    };

How could I convert int year, month, day, hour, minute; to a unix timestamp? Is this possible without the Date class?

3
  • 1
    Why don't you want to use the date class? Commented Jan 12, 2011 at 21:22
  • To avoid the Date object creation as stated here: stackoverflow.com/questions/732034/getting-unixtime-in-java Commented Jan 12, 2011 at 21:29
  • Not possible without also having user's locale! (which I suppose you do in android, for but the general java extension, you don't have enough information in the sample to do it.) Commented Jan 12, 2011 at 21:36

3 Answers 3

20

Okay, use Calendar then, since that's preferred to Date anyway:

int componentTimeToTimestamp(int year, int month, int day, int hour, int minute) {

    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, day);
    c.set(Calendar.HOUR, hour);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);

    return (int) (c.getTimeInMillis() / 1000L);
}

Calendar won't do any computations until getTimeMillis() is called and is designed to be more efficient than Date.

Sign up to request clarification or add additional context in comments.

4 Comments

Would c.set(year, month, day, hour, minute, 0); work too? Since Calendar also has a method that takes a year, month, day of month, hour of day, minute, and second.
Not only would it work, it's more efficient, so yes setting them all with the same method call would be better still.
What about GregorianCalendar - would that be more efficient than Calendar?
@JerryBrady How is it more efficient?
1

I'm assuming you want to avoid object overhead so I'm not suggesting any Date or Calendar classes; rather, you can calculate the value directly.

Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds. (Wikipedia article)

Calculate the number of days from Jan 1 1970 to the chosen year/month/day and multiply that by 24 * 3600, then add hour * 3600 + minute * 60 to get the number of seconds from 1970-01-01 00:00 to the chosen date and time.

There are well known algorithms for calculating the days between dates.

Comments

0

Since I wanted to account for daylight saving as well, along with local timezone, so after searching for hours all the possible solutions, what worked for me was as follows:

int gmtOffset = TimeZone.getDefault().getRawOffset();
        int dstOffset = TimeZone.getDefault().getDSTSavings();
        long unix_timestamp = (System.currentTimeMillis() + gmtOffset + dstOffset) / 1000;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.