在Java中将整数日期/时间转换为unix时间戳?

发布于 2024-10-11 21:06:51 字数 946 浏览 5 评论 0原文

这主要适用于android,但也可以在Java中使用。我有这些侦听器:

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;
        }
    };

如何将 int 年、月、日、小时、分钟; 转换为 unix 时间戳?如果没有 Date 类,这可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愿与i 2024-10-18 21:06:51

好的,那么就使用 Calendar,因为无论如何它都优于 Date:

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 在调用 getTimeMillis() 之前不会执行任何计算,并且被设计为比 Date 更高效。

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.

我三岁 2024-10-18 21:06:51

我假设您想避免对象开销,因此我不建议使用任何 Date 或 Calendar 类;相反,您可以直接计算该值。

Unix 时间或 POSIX 时间是一种描述时间点的系统,定义为自 1970 年 1 月 1 日午夜协调世界时 (UTC) 以来经过的秒数,不包括闰秒。 (维基百科文章

计算从 1970 年 1 月 1 日到所选年/月/日的天数并将其乘以 24 * 3600,然后加上 小时 * 3600 + 分钟 * 60 以获取从 1970-01-01 00:00 到所选日期的秒数和时间。

有众所周知的算法来计算日期之间的天数。

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.

魂ガ小子 2024-10-18 21:06:51

由于我还想考虑夏令时以及当地时区,因此在搜索了几个小时所有可能的解决方案后,对我有用的方法如下:

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文