将整数日期/时间转换为 Java 中的 unix 时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4674174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert integer dates/times to unix timestamp in Java?
提问by Mohit Deshpande
This mainly applies to android, but could be used in Java. I have these listeners:
这主要适用于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;
}
};
How could I convert int year, month, day, hour, minute;
to a unix timestamp? Is this possible without the Date
class?
如何转换int year, month, day, hour, minute;
为unix时间戳?没有Date
课这可能吗?
回答by Jerry Brady
Okay, use Calendar then, since that's preferred to Date anyway:
好的,然后使用 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 won't do any computations until getTimeMillis()
is called and is designed to be more efficient than Date
.
Calendar 不会进行任何计算,直到getTimeMillis()
被调用并且被设计为比Date
.
回答by Stephen P
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.
我假设您想避免对象开销,所以我不建议使用任何 Date 或 Calendar 类;相反,您可以直接计算该值。
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)
Unix 时间或 POSIX 时间是一种用于描述时间点的系统,定义为自 1970 年 1 月 1 日协调世界时 (UTC) 午夜以来经过的秒数,不包括闰秒。(维基百科文章)
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.
计算从 1970 年 1 月 1 日到所选年/月/日的天数24 * 3600
,然后乘以,然后相加hour * 3600 + minute * 60
得到从 1970-01-01 00:00 到所选日期和时间的秒数。
There are well known algorithmsfor calculating the days between dates.
有众所周知的算法来计算日期之间的天数。
回答by zeeshan
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;