Varsha B.
Varsha B.

Reputation: 41

Convert a Time String to long value?

I am trying to get long value of a string.

final long date = System.currentTimeMillis();
Date dt = new Date(date);
SimpleDateFormat sdf = new SimpleDateFormat(" HH:mm aa");
String time1 = sdf.format(dt);
 try {
 dt = sdf.parse(time1); 
} catch (ParseException e) 
{  
e.printStackTrace();  
}
long millis = dt.getTime();
Log.d("Current time", millis + "");

Here I get negative value of millis(current time). Please help me out of this.

Upvotes: 1

Views: 106

Answers (2)

nickcen
nickcen

Reputation: 1692

i have try, but the output didn't a negative value. after the back and forth conversion, you have drop out the year, month and day value. so the dt you get back from parse method has been set to 1970-01-04.

Upvotes: 0

Sandeep
Sandeep

Reputation: 2593

 public static long getLongDate(String d) { 
    long dateInLong = 0;
    try {

        DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
        Date date = formatter.parse(d);
        dateInLong = date.getTime();


    } catch (Exception e) {
        Log.d(Constants.TAG, "" + e.getMessage());
    }

    return dateInLong;

}

try this.

Upvotes: 3

Related Questions