Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting a date string into milliseconds in java [duplicate]

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.

like image 847
Edmund Rojas Avatar asked Dec 08 '11 06:12

Edmund Rojas


People also ask

How do you convert String to milliseconds in java?

Here's an example of what you want/need to do (assuming time zone is not involved here): String myDate = "2014/10/29 18:10:45"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = sdf. parse(myDate); long millis = date. getTime();

How do you convert date objects to milliseconds?

A simple solution is to get the timedelta object by finding the difference of the given datetime with Epoch time, i.e., midnight 1 January 1970. To obtain time in milliseconds, you can use the timedelta. total_seconds() * 1000 .

How do you parse a date to milliseconds?

Approach : First declare variable time and store the milliseconds of current date using new date() for current date and getTime() Method for return it in milliseconds since 1 January 1970. Convert time into date object and store it into new variable date. Convert the date object's contents into a string using date.


4 Answers

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();
like image 85
Phil Avatar answered Oct 23 '22 22:10

Phil


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.

like image 24
Artem Avatar answered Oct 23 '22 22:10

Artem


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;
like image 4
nostra13 Avatar answered Oct 24 '22 00:10

nostra13


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();
like image 2
Neetesh Avatar answered Oct 23 '22 22:10

Neetesh