2

我想将Date+Time转换为毫秒。我正在使用此代码来转换它们。

final TimePicker time = (TimePicker) findViewById(R.id.timePicker1);
final DatePicker date = (DatePicker) findViewById(R.id.datePicker1);

final Calendar calendar = Calendar.getInstance();

calendar.set(date.getYear(), date.getMonth(),
                            date.getDayOfMonth(), time.getCurrentHour(),
                            time.getCurrentMinute(), 0);
final long startTime = calendar.getTimeInMillis();

但它没有给我正确的价值。例如,如果我想转换这个“ Sep 23 2000 02:45 AM ”,它会给我这个值“ 96965900504 ”。但这个值不像这些网站那样

http://www.ruddwire.com/handy-code/date-to-millisecond-calculators/

http://www.timestampconvert.com/?go1=true&m=09&d=23&y=2000&hours=02&min=45&sec=0&Submit=++++++Convert+to+timestamp+++++&offset=-5

然后我尝试我们time.getCurrentHour() + 5,因为我当前的位置是GMT+5,但它没有给我正确的结果。我怎样才能得到正确的日期+时间值到毫秒???

4

3 回答 3

2

如果您将值直接设置到日历中,您将获得正确的值:

Calendar c = Calendar.getInstance();
c.set(2000, 8, 23, 2, 45, 0);

System.out.println("TimeInMillis:" + c.getTimeInMillis());

Date e = new Date(c.getTimeInMillis());
System.out.println("Date:" + e);

结果将是:

TimeInMillis:969666300102
Date:Sat Sep 23 02:45:00 AST 2000

因此,请确保DatePickerandTimePicker返回正确的值。

于 2012-11-11T09:32:07.203 回答
1

好的,我只是将 TimeZone 添加到此代码中

TimeZone timezone = TimeZone.getTimeZone("GMT+05:00");
calendar.setTimeZone(timezone);
于 2012-11-11T12:06:21.493 回答
0

尝试这个。

 Calendar calendar = Calendar.getInstance();
      Date date = calendar.getTime();
      TimeZone tz = TimeZone.getTimeZone("your timezone");

      SimpleDateFormat formatter = 
             (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.LONG,
             DateFormat.LONG, Locale.getDefault());

      formatter.setTimeZone(tz);
      formatter.applyPattern("your pattern");
      formatter.parse("yourdate");
于 2012-11-11T09:22:38.850 回答