Question

I want to convert Date+Time into milliseconds. i am using this code to convert them.

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();

but it does't give me the correct value. for example if i want to convert this "Sep 23 2000 02:45 AM" it gives me this value "96965900504". but this value is not like that as in these sites

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

then i try to us time.getCurrentHour() + 5 because of my current location is GMT+5 but it does't gives me correct result. How can i get the correct value of date+time into milliseconds???

Was it helpful?

Solution 2

ok its done I just add TimeZone to this code

TimeZone timezone = TimeZone.getTimeZone("GMT+05:00");
calendar.setTimeZone(timezone);

OTHER TIPS

If you set values directly into the Calendar you will get correct value:

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

result will be:

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

So make sure the DatePicker and TimePicker returns correct values.

Try this.

 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");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top