Skip to content Skip to sidebar Skip to footer

Converting A Date String Into Milliseconds In Java

Possible Duplicate: Calculate date/time difference in java how would a future date such as Sat Feb 17 2012 be converted into milliseconds in java that can then be subtracted fro

Solution 1:

The simplest technique would be to use DateFormat:

String input = "Sat Feb 17 2012";
Datedate = new SimpleDateFormat("EEE MMM dd yyyy", Locale.ENGLISH).parse(input);
long milliseconds = date.getTime();
long millisecondsFromNow = milliseconds - (newDate()).getTime();
Toast.makeText(this, "Milliseconds to future date="+millisecondsFromNow, Toast.LENGTH_SHORT).show();

A more difficult technique (that basically does what DateFormat does for you) involves parsing it yourself (this would not be considered best practice):

String input = "Sat Feb 17 2012";
String[] myDate = input.split("\\s+");
int year = Integer.parseInt(myDate[3]);
String monthString = myDate[1];
int mo = monthString.equals("Jan")? Calendar.JANUARY :
             monthString.equals("Feb")? Calendar.FEBRUARY :
             monthString.equals("Mar")? Calendar.MARCH :
             monthString.equals("Apr")? Calendar.APRIL :
             monthString.equals("May")? Calendar.MAY :
             monthString.equals("Jun")? Calendar.JUNE :
             monthString.equals("Jul")? Calendar.JULY :
             monthString.equals("Aug")? Calendar.AUGUST :
             monthString.equals("Sep")? Calendar.SEPTEMBER :
             monthString.equals("Oct")? Calendar.OCTOBER :
             monthString.equals("Nov")? Calendar.NOVEMBER :
             monthString.equals("Dec")? Calendar.DECEMBER : 0;
int day = Integer.parseInt(myDate[2]);
Calendar c = Calendar.getInstance();
c.set(year, mo, day);
long then = c.getTimeInMillis();
Time current_time = new Time();
current_time.setToNow();
long now = current_time.toMillis(false);
long future = then - now;
Date d = new Date(future);
//TODO use d as you need.
Toast.makeText(this, "Milliseconds to future date="+future, Toast.LENGTH_SHORT).show();

Solution 2:

Firts, you must parse you String to get its Date representation. Here are examples and some docs. Then you shoud call getTime() method of your Date.

Solution 3:

DateFormatformat=newSimpleDateFormat("EEE MMM dd yyyy", Locale.US);
longfutureTime=0;
try {
    Datedate= format.parse("Sat Feb 17 2012");
    futureTime = date.getTime();
} catch (ParseException e) {
    Log.e("log", e.getMessage(), e);
}

longcurTime= System.currentTimeMillis();
longdiff= futureTime - curTime;

Solution 4:

Pass year, month and day of the future date in the date of this code and variable diff will give the millisecond time till that date,

Datedate=new GregorianCalendar(year, month, day).getTime();
    Date today =newDate();
    long diff = date.getTime() - today.getTime();

Solution 5:

You can simply call the getTime() method of date object. please follow through the sample below

import java.util.Date;

publicclassTest {
    @SuppressWarnings("deprecation")
    publicstaticvoidmain(String[] args) {

        System.out.println(newDate("Sat Feb 17 2012").getTime());

    }

}

Post a Comment for "Converting A Date String Into Milliseconds In Java"