7

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 from the current time in milliseconds to yield time remaining until that future date.

Community
  • 1
  • 1
Edmund Rojas
  • 6,376
  • 16
  • 61
  • 92
  • 1
    It's often better to use a Date-time aware library (e.g. [Joda Time)](http://joda-time.sourceforge.net/0) than to convert to milliseconds and then to convert back: milliseconds may lose information across things like timezone differences, months without 30 days, leap years, etc. So depending on the level of accuracy needed... –  Dec 08 '11 at 06:52

6 Answers6

21

The simplest technique would be to use DateFormat:

String input = "Sat Feb 17 2012";
Date date = new SimpleDateFormat("EEE MMM dd yyyy", Locale.ENGLISH).parse(input);
long milliseconds = date.getTime();
long millisecondsFromNow = milliseconds - (new Date()).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();
Phil
  • 35,852
  • 23
  • 123
  • 164
  • Or, you could use SimpleDateFormat to do it in 2 lines of code. – dusktreader Jul 28 '12 at 07:58
  • 2
    @dusktreader, if you are going to down vote and provide a new suggestion, you should at least provide code. My answer is very well thought out and provides a lot of working code. This certainly does not deserve a down vote, and your comment is not productive. – Phil Jul 30 '12 at 16:20
  • 3
    This is a classic example of a poor coding practice I see all the time. This functionality is provided in a very simple and clean interface. Rolling your own is anti-productive. I downvoted because I honestly felt that this was a poor answer, and I even commented to tell you why (which I didn't need to do). Here's the code, even though this is a dead topic that's been closed for half a year: DateFormat format = new SimpleDateFormat("EEE MMM dd yyyy"); long millis = format.parse("Sat Feb 17 2012").getTime(); – dusktreader Aug 01 '12 at 23:10
4

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.

Artem
  • 4,347
  • 2
  • 22
  • 22
4
DateFormat format = new SimpleDateFormat("EEE MMM dd yyyy", Locale.US);
long futureTime = 0;
try {
    Date date = format.parse("Sat Feb 17 2012");
    futureTime = date.getTime();
} catch (ParseException e) {
    Log.e("log", e.getMessage(), e);
}

long curTime = System.currentTimeMillis();
long diff = futureTime - curTime;
nostra13
  • 12,377
  • 3
  • 33
  • 43
2

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,

    Date date = new GregorianCalendar(year, month, day).getTime();
    Date today = new Date();
    long diff = date.getTime() - today.getTime();
Neetesh
  • 917
  • 1
  • 6
  • 16
1

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

import java.util.Date;

public class Test {
    @SuppressWarnings("deprecation")
    public static void main(String[] args) {

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

    }

}
Noufal Panolan
  • 1,357
  • 2
  • 14
  • 27
1
    try {  String str_date="11-June-07";
    SimpleDateFormat formatter ; 
    Date date ; 
    formatter = new SimpleDateFormat("dd-MMM-yy");
    date = (Date) formatter.parse(str_date);  
    Log.i("test",""+date);
    } catch (Exception e)
    {System.out.println("Exception :"+e);  }  

    Date d = new Date();
    long time = d.getTime();
    long timeDiff = time - lastTime;

//timeDiff will contain your value.
//import these two,
//import java.text.SimpleDateFormat;
//import java.util.Date;
Nishant
  • 3,614
  • 1
  • 20
  • 26