简体   繁体   中英

Date and GregorianCalendar millisecond handling

I'm a little confused of the handling between the java Date Class and GregorianCalendar in Android (2.1). In this example, the year, month, day, hour, minute values are coming from Date and Time pickers.

Date date = new Date(year, month, day, hour, minute);
GregorianCalendar senddate = new GregorianCalendar(year, month, day, hour, minute);

Log.v(LOG_TAG, senddate.getTimeInMillis() + "");
Log.v(LOG_TAG, date.getTime() + "");

output from the logging:

1297604340000
61255744740000

why is the date object so far off. what did i miss?

edit:

I ran my sample code again.

1297609440000
61255749840000
2011 - 1 - 13 - 16 - 4 (year - month - dayofmonth - hour - minute)

getting the values from:

int month = dpDate.getMonth();
int day = dpDate.getDayOfMonth();
int year = dpDate.getYear();

int hour = dpTime.getCurrentHour();
int minute = dpTime.getCurrentMinute();

Possibly the cause is the difference in the construction API, to be precise, the year field. For Date class the year field is the year minus 1900, where as for GregorianCalendar it's the actual year.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM