Skip to content Skip to sidebar Skip to footer

Convert Time To Millisecond And Find Out Time Period In Java Or Android

I have enter earlier time period just like this, 2012-01-18-11-42-24 in database, So How to convert this time in to millisecond, I have so confused, I have try many way but could

Solution 1:

Please take a look at the DateFormat and SimpleDateFormat classes. They have a parse(String string) method that will give you a Date object from its String representation. You can then use getTime() method to get the time as milliseconds and compare them.

Solution 2:

SimpleDateFormat outputFormat = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
 Date c_date=newDate(); //current date Date saved_date=(Date)outputFormat.parse("2012-01-18-11-42-24");   //saved date
 long diffInMs = c_date.getTime() -saved_date.getTime();  //diff between two in milisecond
 long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMs);  //millisecond to second convertion

you can either use as same as saved_date for the second one date instead of current date and than same way find time period between two

Solution 3:

use this code to get time in milliseconds

Date  date1 = newDate(datepicker.getYear() - 1900, datepicker.getMonth(), datepicker.getDayOfMonth(), timepicker.getCurrentHour()*3600000, timepicker.getCurrentMinute()*60000);
Log.i(TAG, date1.toString());

Solution 4:

Try this,

publiclongconvertDateToMillis(int day, int month, int year, int hour, int minute)
{
       Calendar c = Calendar.getInstance();
       c.set(year, month, day, hour, minute, 00);    
       return c.getTimeInMillis();    
}

Post a Comment for "Convert Time To Millisecond And Find Out Time Period In Java Or Android"